diff --git a/combination-sum/Zioq.js b/combination-sum/Zioq.js new file mode 100644 index 000000000..cb019a87f --- /dev/null +++ b/combination-sum/Zioq.js @@ -0,0 +1,44 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function(candidates, target) { + let result = []; + + function find_combination(index, target, current) { + if (target === 0) { + result.push([...current]); + return; + } + + for (let i = index; i < candidates.length; i++) { + // Only proceed if current number doesn't exceed target + if (candidates[i] <= target) { + // Include current number in combination + current.push(candidates[i]); + + // Recursive call with: + // - same index i (allowing reuse of same number) + // - reduced target by current number + find_combination(i, target - candidates[i], current); + + // Backtrack: remove the last added number to try other combinations + current.pop(); + } + } + } + + find_combination(0, target, []); + return result; +}; + +/* + + + +*/ + +console.log(combinationSum([2,3,6,7], 7)) + + diff --git a/product-of-array-except-self/Zioq.js b/product-of-array-except-self/Zioq.js new file mode 100644 index 000000000..ccb304bbd --- /dev/null +++ b/product-of-array-except-self/Zioq.js @@ -0,0 +1,36 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function(nums) { + let result = Array.from({length: nums.length}, () => 1) // Initialize return array + + // Iterate left to right + let left = 1; + for( let i =0 ; i=0; i-- ) { + result[i] *= right; + right *= nums[i]; + } + + // console.log(result) + return result +}; + +/* + Time Complexity: O(n): Loop the nth nums array length + Space Complexity: O(1) +*/ + + + +console.log(productExceptSelf([1,2,3,4])) +console.log(productExceptSelf([-1,1,0,-3,3])) + + diff --git a/reverse-bits/Zioq.js b/reverse-bits/Zioq.js new file mode 100644 index 000000000..20f3d5ae6 --- /dev/null +++ b/reverse-bits/Zioq.js @@ -0,0 +1,19 @@ +/** + * @param {number} n - a positive integer + * @return {number} - a positive integer + */ +var reverseBits = function(n) { + let result = 0; //Initial value + for (let i=0; i < 32; i++) { //The loop iterates 32 times, as the input n is a 32-bit unsigned integer + result = (result << 1) | (n & 1); // Shift the result to the left by 1 bit OR it with the least significant bit of n. + n >>= 1; // Shifts the bits of n one place to the right, effectively "removing" the processed LSB. + } + return result >>> 0; +}; +/* + Time Complexity: O(1), because we always loop exactly 32 times, regardless of the input. + Space Complexity: O(1), because we use a constant amount of space. +*/ + + + diff --git a/two-sum/Zioq.js b/two-sum/Zioq.js new file mode 100644 index 000000000..592da9b53 --- /dev/null +++ b/two-sum/Zioq.js @@ -0,0 +1,30 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + // Initialize object to save remained value with index + let remain_with_index_obj = {} + + for ( let i =0; i