diff --git a/Challenge-1/3 Sum.ts b/Challenge-1/3 Sum.ts new file mode 100644 index 0000000..baaa6e4 --- /dev/null +++ b/Challenge-1/3 Sum.ts @@ -0,0 +1,30 @@ +function threeSum(nums: number[]): number[][] { + const sortedNums = nums.toSorted((a,b) => a - b) + let prevFirst = null + let result = [] + for(let i=0; i< nums.length ; i++) { + let first = sortedNums[i] + if(prevFirst === first){ + continue + } + let j,k + j = i + 1 + k = nums.length - 1 + while(j 0) { + end += 1; + profit = Math.max(profit, difference); + } else { + start = end; + end += 1; + } + } + return profit; +} diff --git a/Challenge-1/Contains duplicate.ts b/Challenge-1/Contains duplicate.ts new file mode 100644 index 0000000..c87b5cf --- /dev/null +++ b/Challenge-1/Contains duplicate.ts @@ -0,0 +1,10 @@ +function containsDuplicate(nums: number[]): boolean { + const freqCounter = new Map() + for(let i = 0; i< nums.length; i++){ + if(freqCounter.get(nums[i])){ + return true + } + freqCounter.set(nums[i], 1) + } + return false +}; diff --git a/Challenge-1/Find Minimum in Rotated Sorted Array.ts b/Challenge-1/Find Minimum in Rotated Sorted Array.ts new file mode 100644 index 0000000..1b8aaf9 --- /dev/null +++ b/Challenge-1/Find Minimum in Rotated Sorted Array.ts @@ -0,0 +1,24 @@ +function findMin(nums: number[]): number { + let l,r,m + l= 0; + r = nums.length -1 + let res = nums[0] + do { + m = Math.floor((l + r) / 2) + let left = nums[l] + let right = nums[r] + let curr = nums[m] + //check for sorted array + if(left < right) { + return Math.min(res, left) + } + res = Math.min(curr, res) + if(left <= curr) { + l = m + 1 + } else if(right >= curr) { + r = m - 1 + } + + }while(l<=r); + return res +}; diff --git a/Challenge-1/Maximum Product Subarray.ts b/Challenge-1/Maximum Product Subarray.ts new file mode 100644 index 0000000..b6c2473 --- /dev/null +++ b/Challenge-1/Maximum Product Subarray.ts @@ -0,0 +1,14 @@ +function maxProduct(nums: number[]): number { + let currMin = nums[0] + let currMax = nums[0] + let maxProd = nums[0] + + for(let i = 1; i< nums.length; i++){ + let temp = currMax * nums[i] + currMax = Math.max(nums[i],temp, currMin * nums[i]) + currMin = Math.min(nums[i],temp, currMin * nums[i]) + maxProd = Math.max(currMax, maxProd) + } + + return maxProd +}; diff --git a/Challenge-1/Maximum Subarray.ts b/Challenge-1/Maximum Subarray.ts new file mode 100644 index 0000000..00eaae8 --- /dev/null +++ b/Challenge-1/Maximum Subarray.ts @@ -0,0 +1,10 @@ +function maxSubArray(nums: number[]): number { + let currSum = nums[0] + let maxSum = currSum + for(let i = 1; i< nums.length; i++){ + currSum = Math.max(nums[i], (currSum + nums[i])) + maxSum = Math.max(maxSum, currSum) + } + return maxSum + +}; diff --git a/Challenge-1/Product of Array Except Self.ts b/Challenge-1/Product of Array Except Self.ts new file mode 100644 index 0000000..e5fa25a --- /dev/null +++ b/Challenge-1/Product of Array Except Self.ts @@ -0,0 +1,19 @@ +function productExceptSelf(nums: number[]): number[] { + let output = [] + for(let i = 0; i < nums.length ; i++){ + let previous = i === 0 ? 1 : output[i -1] + //prefix + if(i === 0 ){ + output.push(previous) + continue + } + output.push(previous * nums[i-1]) + } + + let postFix = nums.at(-1) + for(let i = nums.length - 2; i > -1 ; i--){ + output[i] = postFix * output[i] + postFix *= nums[i] + } + return output +}; diff --git a/Challenge-1/Search in Rotated Sorted Array.ts b/Challenge-1/Search in Rotated Sorted Array.ts new file mode 100644 index 0000000..d491204 --- /dev/null +++ b/Challenge-1/Search in Rotated Sorted Array.ts @@ -0,0 +1,27 @@ +function search(nums: number[], target: number): number { + let l,r,m + l= 0; + r = nums.length -1 + + while(l<=r){ + m = Math.floor((l + r) / 2) + if(nums[m] === target) { + return m + } + if(nums[l] <= nums[m]){ + if(target <= nums[m] && target >= nums[l]){ + r = m - 1 + } else { + l = m + 1 + } + + } else { + if(target >= nums[m] && target <= nums[r]){ + l = m + 1 + } else { + r = m - 1 + } + } + } + return -1 +}; diff --git a/Challenge-1/Sum of Two Integers.ts b/Challenge-1/Sum of Two Integers.ts new file mode 100644 index 0000000..2cf1794 --- /dev/null +++ b/Challenge-1/Sum of Two Integers.ts @@ -0,0 +1,7 @@ +function getSum(a: number, b: number): number { + if(b === 0) { + return a + } + const carry = (a & b) << 1 + return getSum((a ^ b), carry) +}; diff --git a/Challenge-1/readme.md b/Challenge-1/readme.md new file mode 100644 index 0000000..1f6c3e5 --- /dev/null +++ b/Challenge-1/readme.md @@ -0,0 +1,2 @@ +Muhammed Zulfiker A +muhammedzulfiker\*\*\*\*@gmail.com diff --git a/Challenge-1/two-sum.ts b/Challenge-1/two-sum.ts new file mode 100644 index 0000000..7eb8805 --- /dev/null +++ b/Challenge-1/two-sum.ts @@ -0,0 +1,13 @@ +function twoSum(nums: number[], target: number): number[] { + const map: Map = new Map(); + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (map.has(complement)) { + return [map.get(complement)!, i]; + } + map.set(nums[i], i); + } + return []; +} + +twoSum([2, 7, 11, 15], 9); // [0, 1]