Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Jul 18, 2023
1 parent 99b6983 commit 8692451
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 0 deletions.
12 changes: 12 additions & 0 deletions leetcode/problems/c/decode-xored-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

int* decode(int* encoded, int encodedSize, int first, int* returnSize) {
int* arr = (int*)malloc(sizeof(int) * (encodedSize + 1));
*returnSize = encodedSize + 1;
arr[0] = first;
for (int i = 0; i < encodedSize; i++)
arr[i + 1] = arr[i] ^ encoded[i];
return arr;
}
10 changes: 10 additions & 0 deletions leetcode/problems/c/xor-operation-in-an-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

int xorOperation(int n, int start) {
int i, res = start;
for (i = 1; i < n; i++)
start += 2, res ^= start;
return res;
}
19 changes: 19 additions & 0 deletions leetcode/problems/cpp/decode-xored-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include "vector"
#include "iostream"
using namespace std;

class Solution {
public:
vector<int> decode(vector<int>& encoded, int first) {
int size = encoded.size();
vector<int> result(size + 1);
result[0] = first;
for (int i = 0; i < size; i++)
result[i + 1] = result[i] ^ encoded[i];
return result;
}
};
13 changes: 13 additions & 0 deletions leetcode/problems/cpp/xor-operation-in-an-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

class Solution {
public:
int xorOperation(int n, int start) {
int i, res = start;
for (i = 1; i < n; i++)
start += 2, res ^= start;
return res;
}
};
14 changes: 14 additions & 0 deletions leetcode/problems/java/decode-xored-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

class Solution {
public int[] decode(int[] encoded, int first) {
int size = encoded.length;
int[] result = new int[size + 1];
result[0] = first;
for (int i = 0; i < size; i++)
result[i + 1] = result[i] ^ encoded[i];
return result;
}
}
14 changes: 14 additions & 0 deletions leetcode/problems/java/xor-operation-in-an-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)


class Solution {
public int xorOperation(int n, int start) {
int i, res = start;
for (i = 1; i < n; i++){
start += 2; res ^= start;
}
return res;
}
}
16 changes: 16 additions & 0 deletions leetcode/problems/js/decode-xored-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

/**
* @param {number[]} encoded
* @param {number} first
* @return {number[]}
*/
var decode = function (encoded, first) {
let result = [first]
let size = encoded.length
for (let i = 0; i < size; i++)
result.push(result[i] ^ encoded[i])
return result
}
15 changes: 15 additions & 0 deletions leetcode/problems/js/xor-operation-in-an-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

/**
* @param {number} n
* @param {number} start
* @return {number}
*/
var xorOperation = function (n, start) {
let i, res = start
for (i = 1; i < n; i++)
start += 2, res ^= start
return res
}
10 changes: 10 additions & 0 deletions leetcode/problems/py/decode-xored-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# by @codeAbinash
# Time : O(n)
# Space : O(1)

class Solution:
def decode(self, encoded: List[int], first: int) -> List[int]:
decoded = [first]
for i in range(len(encoded)):
decoded.append(encoded[i] ^ decoded[i])
return decoded
10 changes: 10 additions & 0 deletions leetcode/problems/py/xor-operation-in-an-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# by @codeAbinash
# Time : O(n)
# Space : O(1)

class Solution:
def xorOperation(self, n: int, start: int) -> int:
res = start;
for i in range(1,n):
res ^= start + 2*i
return res
11 changes: 11 additions & 0 deletions leetcode/problems/ts/decode-xored-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

function decode(encoded: number[], first: number): number[] {
let arr = [first];
let size = encoded.length
for (let i = 0; i < size; i++)
arr.push(encoded[i] ^ arr[i])
return arr
}
10 changes: 10 additions & 0 deletions leetcode/problems/ts/xor-operation-in-an-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

function xorOperation(n: number, start: number): number {
let res = start
for (let i = 1; i < n; i++)
start += 2, res ^= start
return res
}

0 comments on commit 8692451

Please sign in to comment.