From be58c8f1f2c9a1edda921a715156a1ab67fdaa1b Mon Sep 17 00:00:00 2001 From: Raunak Raj Date: Sun, 8 Oct 2023 01:58:24 +0530 Subject: [PATCH 1/9] A new Approach to to solve the median of two sorted arrays in java --- .../New approach[Arraylist]/Solution.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java diff --git a/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java new file mode 100644 index 0000000..cd6969f --- /dev/null +++ b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +public class Solution{ + public double findMedianSortedArrays(int[] nums1, int[] nums2) { + ArrayList list = new ArrayList<>(); //ArrayList Initialization + + //Add Elements from `nums1` to `list` + for(int i:nums1){ + list.add(i); + } + //Add Elements from `nums2` to `list` + for(int j:nums2){ + list.add(j); + } + + double sum=0; //Initialize a Sum Variable + + for(int i=0;i Date: Sun, 8 Oct 2023 01:58:39 +0530 Subject: [PATCH 2/9] A new Approach to to solve the median of two sorted arrays in java --- .../New approach[Arraylist]/sol.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/sol.md diff --git a/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/sol.md b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/sol.md new file mode 100644 index 0000000..77fc24b --- /dev/null +++ b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/sol.md @@ -0,0 +1,9 @@ +### `findMedianSortedArrays` Function Explanation + +1. Create an `ArrayList` called `list` to merge both sorted arrays into a single list. +2. Iterate through the elements of `nums1`, and for each element, add it to the `list`. +3. Iterate through the elements of `nums2`, and for each element, add it to the `list`. This effectively combines both input arrays into a single merged list. +4. Create a variable `sum` to store the sum of all elements in the merged list. Initialize it to 0. +5. Iterate through the merged `list`, and for each element, add its value to the `sum`. +6. Calculate the average (mean) of all elements in the merged list by dividing the `sum` by the total number of elements in the list. +7. Return the calculated average (mean) as the median value of the merged sorted arrays. \ No newline at end of file From f7b2674c4c84528c61a087f38acdec300fbf6ed3 Mon Sep 17 00:00:00 2001 From: Raunak Raj Date: Sun, 8 Oct 2023 02:01:37 +0530 Subject: [PATCH 3/9] Simple Solution to solve Reverse words in a String --- .../Solution.java | 24 +++++++++++++++++++ Medium/151. Reverse Words in a String/sol.md | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Medium/151. Reverse Words in a String/Solution.java create mode 100644 Medium/151. Reverse Words in a String/sol.md diff --git a/Medium/151. Reverse Words in a String/Solution.java b/Medium/151. Reverse Words in a String/Solution.java new file mode 100644 index 0000000..79c9f60 --- /dev/null +++ b/Medium/151. Reverse Words in a String/Solution.java @@ -0,0 +1,24 @@ +public class Solution { + public String reverseWords(String s) { + String [] words = s.split(" "); + + //StringBuilder to store the result. + StringBuilder result = new StringBuilder(); + + int end = words.length - 1; + + for(int i = 0; i<= end; i++){ + // Check if the current word is not empty. + if(!words[i].isEmpty()) { + // Insert the current word at the beginning of the 'result'. + result.insert(0, words[i]); + if(i < end) { + // Add a space before the current word if it's not the last word. + result.insert(0, " "); + } + } + } + + return result.toString(); + } +} diff --git a/Medium/151. Reverse Words in a String/sol.md b/Medium/151. Reverse Words in a String/sol.md new file mode 100644 index 0000000..e38688c --- /dev/null +++ b/Medium/151. Reverse Words in a String/sol.md @@ -0,0 +1,24 @@ +### Simplest and Easy Approach to Understand and solve a problem + +1. Split the input string 's' into an array of words using space as the delimiter, and store them in the 'words' array. +2. Create an empty StringBuilder called 'result' to store the reversed sentence. +3. Initialize the 'end' variable to the index of the last word in the 'words' array. +4. Iterate through the 'words' array in a forward order. +5. Inside the loop, check if the current word is not empty. +6. If the current word is not empty, insert it at the beginning of the 'result' StringBuilder. +7. If the current word is not the last word, add a space character before it to separate words. +8. Repeat steps 5-7 for all words in the 'words' array. +9. Convert the 'result' StringBuilder to a string using the toString() method. +10. Return the reversed sentence as the result. + +Example 1: + +Input: s = "the sky is blue" + +Output: "blue is sky the" + +Example 2: + +Input: s = " hello world " + +Output: "world hello" \ No newline at end of file From 03b9e19552bff6dc6dd6dc7c04695bb138c0477d Mon Sep 17 00:00:00 2001 From: Raunak Raj Date: Sun, 8 Oct 2023 02:02:59 +0530 Subject: [PATCH 4/9] Optimised solution to Factorial Trailing Zeroes --- .../Solution.java | 14 +++++++++ Medium/172. Factorial Trailing Zeroes/sol.md | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 Medium/172. Factorial Trailing Zeroes/Solution.java create mode 100644 Medium/172. Factorial Trailing Zeroes/sol.md diff --git a/Medium/172. Factorial Trailing Zeroes/Solution.java b/Medium/172. Factorial Trailing Zeroes/Solution.java new file mode 100644 index 0000000..16bae3f --- /dev/null +++ b/Medium/172. Factorial Trailing Zeroes/Solution.java @@ -0,0 +1,14 @@ +public class Solution { + public int trailingZeroes(int n) { + + int zero=0; + + for(int i=5 ; i<=n ; i=i*5){ + zero = zero + n/i; + } + + return zero; + + } + +} diff --git a/Medium/172. Factorial Trailing Zeroes/sol.md b/Medium/172. Factorial Trailing Zeroes/sol.md new file mode 100644 index 0000000..9603c76 --- /dev/null +++ b/Medium/172. Factorial Trailing Zeroes/sol.md @@ -0,0 +1,31 @@ +### Simplest, Easy and Optimized java Solution + +Algorithm +1. Initialize a variable zero to 0. This variable will be used to keep track of the number of trailing zeroes. +2. Start a loop with i initialized to 5. The loop will continue as long as i is less than or equal to n. +3. Inside the loop, calculate the number of factors of 5 in n. You can do this by dividing n by i and adding the result to the zero variable. This step accounts for the number of trailing zeroes caused by the multiples of 5 in the factorial of n. +4. Multiply i by 5 in each iteration of the loop to check for the next power of 5. +5. Once the loop is finished, return the value of zero, which represents the number of trailing zeroes in the factorial of n. + +Example 1: + +Input: n = 3 + +Output: 0 + +Explanation: 3! = 6, no trailing zero. + +Example 2: + +Input: n = 5 + +Output: 1 + +Explanation: 5! = 120, one trailing zero. + +Example 3: + + +Input: n = 0 + +Output: 0 \ No newline at end of file From 3f6dccc0193ed7f69d9385604b1f229e51fdaf27 Mon Sep 17 00:00:00 2001 From: Raunak Raj Date: Sun, 8 Oct 2023 02:04:09 +0530 Subject: [PATCH 5/9] Simple solution to Anagram II --- .../Solution.java | 26 +++++++++++++++++++ .../sol.md | 19 ++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/Solution.java create mode 100644 Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/sol.md diff --git a/Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/Solution.java b/Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/Solution.java new file mode 100644 index 0000000..b51740e --- /dev/null +++ b/Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/Solution.java @@ -0,0 +1,26 @@ +public class Solution { + public int minSteps(String s, String s1) { + int[] freq1 = new int[26]; + int[] freq2 = new int[26]; + + // Count the frequency of characters in the first string + for (char c : s.toCharArray()) { + freq1[c - 'a']++; + } + + // Count the frequency of characters in the second string + for (char c : s1.toCharArray()) { + freq2[c - 'a']++; + } + + int delete = 0; + + // Calculate the difference in character frequencies + for (int i = 0; i < 26; i++) { + delete += Math.abs(freq1[i] - freq2[i]); + } + return delete; + + } + +} diff --git a/Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/sol.md b/Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/sol.md new file mode 100644 index 0000000..26f8094 --- /dev/null +++ b/Medium/2186. Minimum Number of Steps to Make Two Strings Anagram II/sol.md @@ -0,0 +1,19 @@ +### Easiest solution to understand the solution + +1. Create two arrays of 26 Characters +2. Count the frequency of characters in the first string +3. Count the frequency of characters in the second string +4. Calculate the difference in character frequencies +5. Return the difference Value + +Example 1: + +Input: s = "leetcode", t = "coats" + +Output: 7 + +Example 2: + +Input: s = "night", t = "thing" + +Output: 0 \ No newline at end of file From 12f8d14e29811d833d60ab48e1ceebd0654c4929 Mon Sep 17 00:00:00 2001 From: Raunak Raj Date: Sun, 8 Oct 2023 02:05:05 +0530 Subject: [PATCH 6/9] Solution to find Duplicates in array --- .../New Approach/Solution.java | 13 +++++++++++++ .../287. Find Duplicate Number/New Approach/sol.md | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Medium/287. Find Duplicate Number/New Approach/Solution.java create mode 100644 Medium/287. Find Duplicate Number/New Approach/sol.md diff --git a/Medium/287. Find Duplicate Number/New Approach/Solution.java b/Medium/287. Find Duplicate Number/New Approach/Solution.java new file mode 100644 index 0000000..33d01d8 --- /dev/null +++ b/Medium/287. Find Duplicate Number/New Approach/Solution.java @@ -0,0 +1,13 @@ +import java.util.Arrays; +public class Solution { + public int findDuplicate(int[] nums) { + Arrays.sort(nums); + + for(int i=1;i Date: Sun, 8 Oct 2023 12:28:01 +0530 Subject: [PATCH 7/9] Changed the code with understanding Solution --- .../New approach[add two array]/Solution.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Hard/4. Median of Two Sorted Arrays/New approach[add two array]/Solution.java diff --git a/Hard/4. Median of Two Sorted Arrays/New approach[add two array]/Solution.java b/Hard/4. Median of Two Sorted Arrays/New approach[add two array]/Solution.java new file mode 100644 index 0000000..b5e5c39 --- /dev/null +++ b/Hard/4. Median of Two Sorted Arrays/New approach[add two array]/Solution.java @@ -0,0 +1,43 @@ +import java.util.Arrays; +public class Solution{ + public double findMedianSortedArrays(int[] nums1, int[] nums2) { + // Get the lengths of input arrays nums1 and nums2 + int a = nums1.length; + int b = nums2.length; + + // Calculate the total length of the merged array + int c = a + b; + + // Create a new integer array to hold the merged elements + int[] arr = new int[c]; + + // Copy elements from nums1 into arr + for (int i = 0; i < a; i++) { + arr[i] = nums1[i]; + } + + // Copy elements from nums2 into arr, starting from position a + for (int i = 0; i < b; i++) { + arr[a + i] = nums2[i]; + } + + // Sort the merged array in ascending order + Arrays.sort(arr); + + // Check if the length of the merged array is even or odd + if (c % 2 == 0) { + // Calculate the indices of the two middle elements + int mid1 = (c - 1) / 2; + int mid2 = mid1 + 1; + + // Calculate the median as the average of the two middle elements + return (double) (arr[mid1] + arr[mid2]) / 2; + } else { + // Calculate the index of the middle element + int mid = (c - 1) / 2; + + // Return the middle element as the median + return arr[mid]; + } + } +} \ No newline at end of file From fb0ec059baa107880cb396ef2cc70f5c9758ed84 Mon Sep 17 00:00:00 2001 From: Raunak Raj Date: Sun, 8 Oct 2023 12:30:35 +0530 Subject: [PATCH 8/9] Good question to practice --- .../New Approach/Solution.java | 23 +++++++++++++++++++ .../New Approach/sol.md | 16 +++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Medium/34. First and Last Position of Element in an Array/New Approach/Solution.java create mode 100644 Medium/34. First and Last Position of Element in an Array/New Approach/sol.md diff --git a/Medium/34. First and Last Position of Element in an Array/New Approach/Solution.java b/Medium/34. First and Last Position of Element in an Array/New Approach/Solution.java new file mode 100644 index 0000000..569faef --- /dev/null +++ b/Medium/34. First and Last Position of Element in an Array/New Approach/Solution.java @@ -0,0 +1,23 @@ +public class Solution { + public int[] searchRange(int[] nums, int target) { + + int n=nums.length; + int arr[] =new int[2]; + int arr1[] =new int[]{-1,-1}; + arr[0] = -1; arr[1] = -1; + for (int i = 0; i < n; i++) { + if (target != nums[i]) + continue; + if (arr[0] == -1) + arr[0] = i; + arr[1] = i; + } + if (arr[0] != -1) { + return arr; + + } + else + return arr1; + } + +} diff --git a/Medium/34. First and Last Position of Element in an Array/New Approach/sol.md b/Medium/34. First and Last Position of Element in an Array/New Approach/sol.md new file mode 100644 index 0000000..e72915e --- /dev/null +++ b/Medium/34. First and Last Position of Element in an Array/New Approach/sol.md @@ -0,0 +1,16 @@ +### `searchRange` Function Explanation + +1. Get the length of the input array `nums`. +2. Initialize an array to store the search range. +3. Initialize an array with [-1, -1] (default return value). +4. Initialize the start index of the search range as -1. +5. Initialize the end index of the search range as -1. +6. Start iterating through the input array. +7. If the current element is not the target, continue to the next element. +8. If the current element is not the target, continue to the next iteration. +9. If the start index of the search range is -1, set it to the current index. +10. Update the end index of the search range to the current index when a match is found. +11. If the start index is not -1, return the search range. +12. Return the search range containing the start and end indices of the target. +13. If no match was found, return the default [-1, -1] array. +14. Return the default search range [-1, -1] to indicate no match. \ No newline at end of file From c8e465b91a95bde2d8f7a59f1c13a09b13c261c6 Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Mon, 16 Oct 2023 08:39:56 +0800 Subject: [PATCH 9/9] fix: Java Solution for Problem 4 --- .../New approach[Arraylist]/Solution.java | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java index cd6969f..8ea97b3 100644 --- a/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java +++ b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java @@ -1,27 +1,42 @@ import java.util.ArrayList; public class Solution{ public double findMedianSortedArrays(int[] nums1, int[] nums2) { - ArrayList list = new ArrayList<>(); //ArrayList Initialization - - //Add Elements from `nums1` to `list` - for(int i:nums1){ - list.add(i); - } - //Add Elements from `nums2` to `list` - for(int j:nums2){ - list.add(j); - } + int a = nums1.length; + int b = nums2.length; - double sum=0; //Initialize a Sum Variable + // Calculate the total length of the merged array + int c = a + b; - for(int i=0;i