diff --git a/Challenge-1/two-sum.js b/Challenge-1/two-sum.js new file mode 100644 index 0000000..ff191e2 --- /dev/null +++ b/Challenge-1/two-sum.js @@ -0,0 +1,15 @@ +var twoSum = function (nums, target) { + let obj = {} + for (i = 0; i < nums.length; i++) { + let diff = target - nums[i] + if (obj[diff] != null) { + return [obj[diff], i] + } else { + obj[nums[i]] = i + console.log(obj) + } + } +}; +const nums = [2, 7, 11, 15]; +const target = 9; +console.log(twoSum(nums, target)); \ No newline at end of file diff --git a/Challenge-10/Search in Rotated Sorted Array.js b/Challenge-10/Search in Rotated Sorted Array.js new file mode 100644 index 0000000..38b71d4 --- /dev/null +++ b/Challenge-10/Search in Rotated Sorted Array.js @@ -0,0 +1,47 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ + +function getPivot(arr) { + var s = 0, e = arr.length - 1; + while (s < e) { + var mid = Math.floor(s + (e - s) / 2) + + if (arr[mid] >= arr[0]) { + s = mid + 1 + } else { + e = mid + } + } + return s; +} + +function binarySearch(arr, start, end, key) { + var s = start, e = end; + + while (s <= e) { + var mid = Math.floor(s + (e - s) / 2) + if (arr[mid] == key) { + return mid + } + if (key > arr[mid]) { + s = mid + 1; + } else { + e = mid - 1 + } + } + return -1; +} +var search = function (arr, target) { + var n = arr.length + var pivot = getPivot(arr) + + if (target >= arr[pivot] && target <= arr[n - 1]) { + return binarySearch(arr, pivot, n - 1, target) + } else { + return binarySearch(arr, 0, pivot - 1, target) + } + +}; \ No newline at end of file diff --git a/Challenge-11/3sum.js b/Challenge-11/3sum.js new file mode 100644 index 0000000..5408783 --- /dev/null +++ b/Challenge-11/3sum.js @@ -0,0 +1,37 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + let n = nums.length; + const ans = []; + + nums.sort((a, b) => a - b); + + for (let i = 0; i < n - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + + let j = i + 1, k = n - 1; + + while (j < k) { + let sum = nums[i] + nums[j] + nums[k]; + + if (sum < 0) { + j++; + } else if (sum > 0) { + k--; + } else { + ans.push([nums[i], nums[j], nums[k]]); + // Skip duplicate values for 'j' + while (j < k && nums[j] === nums[j + 1]) j++; + // Skip duplicate values for 'k' + while (j < k && nums[k] === nums[k - 1]) k--; + + j++; + k--; + } + } + } + + return ans; +}; \ No newline at end of file diff --git a/Challenge-12/Container With Most Water.js b/Challenge-12/Container With Most Water.js new file mode 100644 index 0000000..c128357 --- /dev/null +++ b/Challenge-12/Container With Most Water.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} height + * @return {number} + * https://leetcode.com/problems/container-with-most-water/ + */ + +var maxArea = function (h) { + let left = 0 + let right = h.length - 1 + let maxWater = 0 + while (left < right) { + let minH = Math.min(h[left], h[right]) + maxWater = Math.max(maxWater, minH * (right - left)) + if (h[left] < h[right]) left++ + else right-- + + } + + + + + return maxWater +}; diff --git a/Challenge-13/Number of 1 Bits.js b/Challenge-13/Number of 1 Bits.js new file mode 100644 index 0000000..11e8903 --- /dev/null +++ b/Challenge-13/Number of 1 Bits.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} height + * @return {number} + * https://leetcode.com/problems/number-of-1-bits/ + */ + +var hammingWeight = function (n) { + var count = 0; + while (n != 0) { + if (n & 1) { + count++; + } + n = n >> 1; + } + return count; + +}; diff --git a/Challenge-14/Counting Bits.js b/Challenge-14/Counting Bits.js new file mode 100644 index 0000000..0e28b9c --- /dev/null +++ b/Challenge-14/Counting Bits.js @@ -0,0 +1,18 @@ +/** + * @param {number} n + * @return {number[]} + * https://leetcode.com/problems/counting-bits/ + */ +var countBits = function (n) { + const ans = [] + for (var i = 0; i <= n; i++) { + const binary = i.toString(2); + let count = 0 + console.log("binary", binary) + for (let i = 0; i < binary.length; i++) { + if (binary[i] === '1') count++; + } + ans.push(count) + } + return ans +}; \ No newline at end of file diff --git a/Challenge-15/Missing Number.js b/Challenge-15/Missing Number.js new file mode 100644 index 0000000..bfefc88 --- /dev/null +++ b/Challenge-15/Missing Number.js @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function (nums) { + let n = nums.length; + let expectedSum = (n * (n + 1)) / 2; + let actualSum = nums.reduce((acc, num) => acc + num, 0); + return expectedSum - actualSum; + +}; \ No newline at end of file diff --git a/Challenge-16/Reverse Bits.js b/Challenge-16/Reverse Bits.js new file mode 100644 index 0000000..f64e68a --- /dev/null +++ b/Challenge-16/Reverse Bits.js @@ -0,0 +1,14 @@ +/** + * @param {number} n - a positive integer + * @return {number} - a positive integer + * https://leetcode.com/problems/reverse-bits/ + */ + +var reverseBits = function (n) { + let result = n.toString(2) + result = result.padStart(32, '0'); + let reversedString = result.split('').reverse().join(''); + let ans = parseInt(reversedString, 2) + return ans +}; +console.log(reverseBits(43261596)) // 964176192 \ No newline at end of file diff --git a/Challenge-17/Climbing Stairs.js b/Challenge-17/Climbing Stairs.js new file mode 100644 index 0000000..a7da067 --- /dev/null +++ b/Challenge-17/Climbing Stairs.js @@ -0,0 +1,27 @@ +/** + * @param {number} n + * @return {number} + * https://leetcode.com/problems/climbing-stairs/submissions/713423865/ + */ +function numberOfWays(n, memo) { + if (n == 0) + return 1; + if (n < 0) { + return 0 + }; + if (n == 1) + return 1 + + if (memo[n] != -1) + return memo[n] + + return memo[n] = numberOfWays(n - 1, memo) + numberOfWays(n - 2, memo) +} +var climbStairs = function (n) { + var cur = 0 + var memo = new Array(n + 1).fill(-1) + var ans = numberOfWays(n, memo) + + + return ans +}; \ No newline at end of file diff --git a/Challenge-18/Coin Change.js b/Challenge-18/Coin Change.js new file mode 100644 index 0000000..b19339b --- /dev/null +++ b/Challenge-18/Coin Change.js @@ -0,0 +1,33 @@ +/** + * @param {number[]} coins + * @param {number} amount + * @return {number} + * https://leetcode.com/problems/coin-change/description/ + */ +function ANS(arr, index, T, dp) { + + if (index == 0) { + if (T % arr[0] == 0) return T / arr[0]; + else return 1e9; + } + if (dp[index][T] !== -1) return dp[index][T] + var notTaken = 0 + ANS(arr, index - 1, T, dp) + + var taken = 1e9; + if (arr[index] <= T) + taken = 1 + ANS(arr, index, T - arr[index], dp); + + return dp[index][T] = Math.min(notTaken, taken); +} +var coinChange = function (arr, target) { + var n = arr.length + var dp = [] + for (var i = 0; i <= n; i++) { + dp[i] = new Array(target + 1).fill(-1) + } + + var ans = ANS(arr, n - 1, target, dp) + if (ans >= 1e9) return -1; + return ans + +}; \ No newline at end of file diff --git a/Challenge-2/best-time-to-buy-and-sell-stock.js b/Challenge-2/best-time-to-buy-and-sell-stock.js new file mode 100644 index 0000000..d2c1390 --- /dev/null +++ b/Challenge-2/best-time-to-buy-and-sell-stock.js @@ -0,0 +1,11 @@ +function maxProfit(prices) { + let minPrice = Infinity; + let maxProfit = 0; + + for (let price of prices) { + minPrice = Math.min(minPrice, price); + maxProfit = Math.max(maxProfit, price - minPrice); + } + + return maxProfit; +} diff --git a/Challenge-3/contain-duplicate.js b/Challenge-3/contain-duplicate.js new file mode 100644 index 0000000..bdb4543 --- /dev/null +++ b/Challenge-3/contain-duplicate.js @@ -0,0 +1,13 @@ +var containsDuplicate = function (nums) { + const obj = {} + for (let i = 0; i < nums.length; i++) { + if (obj[nums[i]]) { + return true + } + obj[nums[i]] = true + } + + return false + +}; +containsDuplicate([1, 2, 3, 1]); \ No newline at end of file diff --git a/Challenge-4/Sum-of-Two-Integers.js b/Challenge-4/Sum-of-Two-Integers.js new file mode 100644 index 0000000..f1bf196 --- /dev/null +++ b/Challenge-4/Sum-of-Two-Integers.js @@ -0,0 +1,9 @@ +var getSum = function (a, b) { + while (b !== 0) { + let carry = (a & b) << 1; + a = a ^ b; + b = carry; + } + return a; +}; +getSum(1, 2); \ No newline at end of file diff --git a/Challenge-5/Product of Array Except Self.js b/Challenge-5/Product of Array Except Self.js new file mode 100644 index 0000000..a8339e9 --- /dev/null +++ b/Challenge-5/Product of Array Except Self.js @@ -0,0 +1,31 @@ +var productExceptSelf = function (nums) { + const arr = [] + const prefixArr = [] + const suffixArr = [] + for (let i = 0; i < nums.length; i++) { + if (i === 0) { + prefixArr[i] = 1 + + + } else { + prefixArr[i] = prefixArr[i - 1] * nums[i - 1] + } + + } + for (var i = nums.length - 1; i >= 0; i--) { + if (i === nums.length - 1) { + suffixArr[i] = 1 + } else { + suffixArr[i] = suffixArr[i + 1] * nums[i + 1] + } + } + for (let i = 0; i < nums.length; i++) { + arr[i] = prefixArr[i] * suffixArr[i] + + + }; + return arr +} + +console.log(productExceptSelf([1, 2, 3, 4])); + diff --git a/Challenge-8/Product-sub-array.js b/Challenge-8/Product-sub-array.js new file mode 100644 index 0000000..2cf2dac --- /dev/null +++ b/Challenge-8/Product-sub-array.js @@ -0,0 +1,12 @@ +var maxProduct = function (arr) { + let res = arr[0]; + let currMin = 1 + let currMax = 1 + for (var i = 0; i < arr.length; i++) { + let temp = currMax + currMax = Math.max(arr[i], arr[i] * currMax, arr[i] * currMin) + currMin = Math.min(arr[i], arr[i] * temp, arr[i] * currMin) + res = Math.max(res, currMax) + } + return res +}; \ No newline at end of file diff --git a/Challenge-9/Find Minimum in Rotated Sorted Array.js b/Challenge-9/Find Minimum in Rotated Sorted Array.js new file mode 100644 index 0000000..1a517b1 --- /dev/null +++ b/Challenge-9/Find Minimum in Rotated Sorted Array.js @@ -0,0 +1,20 @@ +var findMin = function (nums) { + var s = 0, e = nums.length - 1; + var res = nums[0]; + + while (s <= e) { + if (nums[s] < nums[e]) { + res = nums[s]; + break + } + var mid = Math.floor((s + e) / 2); + res = Math.min(res, nums[mid]); + if (nums[s] <= nums[mid]) { //left sort + s = mid + 1; //got to right + } else { + e = mid; + } + } + return res ; + +}; \ No newline at end of file diff --git a/README.md b/README.md index d922b9d..6eac5ef 100644 --- a/README.md +++ b/README.md @@ -1,148 +1,2 @@ -# GitHub Guide for Submitting Challenge Solutions - -Welcome to the GitHub repository for coding challenges! This guide will walk you through the process of creating a branch under your name and uploading your solution files step by step. If you are new to Git and GitHub, follow these instructions carefully. - ---- - -## πŸš€ Setting Up Git and GitHub - -### 1️⃣ Install Git -If you haven't installed Git yet, download and install it from [Git's official website](https://git-scm.com/downloads). - -### 2️⃣ Create a GitHub Account -If you don’t have a GitHub account, create one at [GitHub](https://github.com/). - -### 3️⃣ Fork the Repository -1. Go to the repository URL shared by your instructor. -2. Click on the **Fork** button in the top-right corner to create a copy in your GitHub account. - -### 4️⃣ Clone the Repository Locally -1. Open **Terminal** (Linux/macOS) or **Command Prompt/Powershell** (Windows). -2. Run the following command, replacing `your-username` with your GitHub username: - - ```sh - git clone https://github.com/WeCode-Community-Dev/blind-75-challenge.git - ``` - -3. Navigate into the repository folder: - - ```sh - cd blind-75-challenge - ``` - ---- - -## 🌿 Creating a Branch Under Your Name -Each student must create a separate branch with their name before making any changes. - -1. Check the current branches: - - ```sh - git branch - ``` - -2. Create a new branch with your name (make sure this is unique - add some unique number or characters to make sure it is not already there): - - ```sh - git checkout -b your-branch-name - ``` - -3. Verify the branch was created and switched to it: - - ```sh - git branch - ``` - - Your name should now be highlighted, meaning you are on that branch. - ---- - -## πŸ” Setting Upstream and Pulling Latest Changes -Before working on a new challenge, ensure your repository is up to date. - -1. Add the original repository as the upstream remote (only required once): - - ```sh - git remote add upstream https://github.com/WeCode-Community-Dev/blind-75-challenge.git - ``` - -2. Fetch the latest changes: - - ```sh - git fetch upstream - ``` - -3. Merge the latest changes into your branch: - - ```sh - git merge upstream/main - ``` - ---- - -## ✏️ Adding Your Challenge Solution -Upload only the solution file for each challenge (e.g., `solution.py`, `solution.cpp`, `solution.java`). -you can name the file .py or .cpp or .java etc. -example: two-sum.py or two-sum.cpp or two-sum.java as per the programming language you choose - -1. Navigate to the folder for the challenge you are working on. -2. Place your solution file inside the appropriate challenge folder. - -Example structure: - -``` -repository-name/ -β”‚-- Challenge-1/ -β”‚ β”‚-- solution.py # Your file -β”‚-- Challenge-2/ -β”‚ β”‚-- solution.cpp # Another file -``` - ---- - -## πŸ“€ Committing and Pushing Your Code - -Replace the "challenge-1" with problem name - -1. Add the file you modified: - - ```sh - git add Challenge-1/solution.py - ``` - -2. Commit your changes with a meaningful message: - - ```sh - git commit -m "Added solution for Challenge 1" - ``` - -3. Push your branch to your GitHub repository: - - ```sh - git push origin your-branch-name - ``` - ---- - -## πŸ”„ Creating a Pull Request (Not Required) -Once you've pushed your changes, you need to create a **Pull Request (PR)**. - -1. Go to your GitHub repository. -2. Switch to your branch (`your-branch-name`). -3. Click on **Compare & pull request**. -4. Add a meaningful **title** and **description**. -5. Click **Create pull request**. - -Your code will now be reviewed and merged by the instructor! πŸŽ‰ - ---- - -## ❗ Important Rules -βœ… Always create a branch under your name. -βœ… Upload only the solution file (no unnecessary files or folders). -βœ… Keep your repository updated by pulling the latest changes. -βœ… Use meaningful commit messages. -βœ… Create a pull request for every challenge. - -Happy coding! πŸš€ - +name : Prathyu Prasad +emial : prathyuprasad116@gmail.com \ No newline at end of file