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..8ea97b3 --- /dev/null +++ b/Hard/4. Median of Two Sorted Arrays/New approach[Arraylist]/Solution.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; +public class Solution{ + public double findMedianSortedArrays(int[] nums1, int[] 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]; + } + } +} 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 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 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 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 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 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