From 16117eecaa6b410ce85925f83a022a11cff548f2 Mon Sep 17 00:00:00 2001 From: shweta0656 Date: Tue, 5 Nov 2024 00:02:43 -0600 Subject: [PATCH] Completed Competitive Coding 1 --- FindMissingElement.java | 37 ++++++++++ Problem1.java | 1 - Problem2.java | 1 - .../BinarySearchLCMissingElement.java | 71 +++++++++++++++++++ .../FindMissingElementinArrayQuestion | 36 ++++++++++ .../LinearSearchLCMissingElement.java | 66 +++++++++++++++++ 6 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 FindMissingElement.java delete mode 100644 Problem1.java delete mode 100644 Problem2.java create mode 100644 leetcodeSimilarQuestionAndSoln/BinarySearchLCMissingElement.java create mode 100644 leetcodeSimilarQuestionAndSoln/FindMissingElementinArrayQuestion create mode 100644 leetcodeSimilarQuestionAndSoln/LinearSearchLCMissingElement.java diff --git a/FindMissingElement.java b/FindMissingElement.java new file mode 100644 index 00000000..9f2a11cc --- /dev/null +++ b/FindMissingElement.java @@ -0,0 +1,37 @@ +/* +Time Complexity: O(log n) +Space Complexity: O(1) + +Find Missing Number in a sorted array +https://youtu.be/LwmckBrlrRs + */ +public class FindMissingElement +{ + public static int search(int[] arr, int size) { + int low = 0, high = size - 1, mid = 0; + + while(high-low>=2) //If the element is missing between a range, the difference will be 2 or more + { + mid = low + ((high - low) / 2); + int lowIndexDiff = arr[low]-low; + int midIndexDiff = arr[mid]-mid; + + if(midIndexDiff != lowIndexDiff) { + high = mid; + } + else{ + low = mid; + } + } + + return (arr[low] + arr[high])/2; + } + + public static void main(String[] args) + { + int[] arr = {3,4,5,6,8}; + int size = arr.length; + + System.out.println("Missing Number is: "+ search(arr, size)); + } +} diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem1.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Problem2.java b/Problem2.java deleted file mode 100644 index 8b137891..00000000 --- a/Problem2.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/leetcodeSimilarQuestionAndSoln/BinarySearchLCMissingElement.java b/leetcodeSimilarQuestionAndSoln/BinarySearchLCMissingElement.java new file mode 100644 index 00000000..20839e39 --- /dev/null +++ b/leetcodeSimilarQuestionAndSoln/BinarySearchLCMissingElement.java @@ -0,0 +1,71 @@ +/* +Time Complexity: O(log n) +Space Complexity: O(1) + */ +package leetcodeSimilarQuestionAndSoln; + +public class BinarySearchLCMissingElement +{ + public int missingElement(int[] nums, int k) + { + int low = 0, high = nums.length - 1; + while (low <= high) { + int mid = low + (high - low) / 2; + if (nums[mid] - nums[0] - mid < k) { + low = mid + 1; + } else { + high = mid-1; + } + } + /* + So the missing number will be: + nums[right] + k minus number of elements missing before index right. + nums[right] + k - (nums[right] - nums[0] - right) = k - (nums[0] - right) = k + nums[0] + right + */ + return k+nums[0]+high; + } +} +/* +Leetcode Soln Explanation(not mine): + +This question is similar to https://leetcode.com/problems/kth-missing-positive-number/ + +The key to understanding this question is to notice that we expect a strictly increasing sequence from the first number in the given array, +otherwise some numbers are missing. In the kth missing positive number question, the strictly increasing sequence starts from 1. + +For this question, we are given an example array: [4,7,9,10] - a perfect sequence without any missing elements would be [4, 5, 6, 7] +and, the first missing element will 1 more than the last element in that sequence. + +However, if there are missing elements between the elements in the array, we need to account for that. The number of positive integers +missing before each index is equal to nums[idx] - idx - nums[0]. This is because, if there are no missing elements, +nums[idx] = nums[0] + idx + 0 (zero missing elements). +If there are k missing elements, then nums[idx] = nums[0] + idx + k. + +To find k, we move the elements to the left of the equation thus giving us: k = nums[idx] - nums[0] - idx; + +Let's try a few examples from the given array: [4, 7, 9, 10] +The number of missing elements before 4 (at idx 0) = nums[0] - nums[0] - 0 = 4 - 4 - 0 = 0 +The number of missing elements before 7 (at idx 1) = nums[1] - nums[0] - 1 = 7 - 4 - 1 = 2 -> 5, 6 +The number of missing elements before 9 (at idx 2) = nums[2] - nums[0] - 2 = 9 - 4 - 2 = 3 -> 5, 6, 8 +The number of missing elements before 10 (at idx 3) = nums[3] - nums[0] - 3 = 10 - 4 - 3 = 3 -> 5, 6, 8 + +To find the kth missing element, we need to find range of indices in which k missing elements can be found. +Let's say we want the third missing element, k = 3. +Let's apply binary search on the sorted array, start with the full range of the sorted array: left = 0, right = nums.length - 1 = 3; + +Find the mid = left + (right - left) / 2 = 0 + (3 - 0) / 2 = 1; +The number of missing elements before index 1 is 2 from the above calculation is 2. We need the 3th missing element, so we need to search +further on the right side of the array so that we can get more missing elements. + +Update the left = mid + 1 = 2; +Calculate the mid again = left + (right - left) / 2 = 2 + (3 - 2) / 2= 2 +The number of missing elements before index 2 is 3 from the above calculation. Since we have k elements, we move to the left half. +Update right = mid - 1 = 1 +Now right is smaller than left so we break out of the loop. + +The kth missing element is between nums[right] and nums[right + 1]. That is, between 7 and 9. The kth missing number can therefore +be calculated through the equation below: + +nums[right] + k minus number of elements missing before index right. +nums[right] + k - (nums[right] - nums[0] - right) = k - (nums[0] - right) = k + nums[0] + right + */ \ No newline at end of file diff --git a/leetcodeSimilarQuestionAndSoln/FindMissingElementinArrayQuestion b/leetcodeSimilarQuestionAndSoln/FindMissingElementinArrayQuestion new file mode 100644 index 00000000..4c45e1a1 --- /dev/null +++ b/leetcodeSimilarQuestionAndSoln/FindMissingElementinArrayQuestion @@ -0,0 +1,36 @@ +https://leetcode.com/problems/missing-element-in-sorted-array/description/ + +1060. Missing Element in Sorted Array {Medium} + +[THIS QUESTION IS SIMILAR NOT COMPLETELY SAME] + +Given an integer array nums which is sorted in ascending order and all of its elements are unique and given also an integer k, +return the kth missing number starting from the leftmost number of the array. + + +Example 1: + +Input: nums = [4,7,9,10], k = 1 +Output: 5 +Explanation: The first missing number is 5. +Example 2: + +Input: nums = [4,7,9,10], k = 3 +Output: 8 +Explanation: The missing numbers are [5,6,8,...], hence the third missing number is 8. +Example 3: + +Input: nums = [1,2,4], k = 3 +Output: 6 +Explanation: The missing numbers are [3,5,6,7,...], hence the third missing number is 6. + + +Constraints: + +1 <= nums.length <= 5 * 104 +1 <= nums[i] <= 107 +nums is sorted in ascending order, and all the elements are unique. +1 <= k <= 108 + + +Follow up: Can you find a logarithmic time complexity (i.e., O(log(n))) solution? \ No newline at end of file diff --git a/leetcodeSimilarQuestionAndSoln/LinearSearchLCMissingElement.java b/leetcodeSimilarQuestionAndSoln/LinearSearchLCMissingElement.java new file mode 100644 index 00000000..cd5531bf --- /dev/null +++ b/leetcodeSimilarQuestionAndSoln/LinearSearchLCMissingElement.java @@ -0,0 +1,66 @@ +package leetcodeSimilarQuestionAndSoln; + +/* +Time Complexity: O(n) +The function iterates through the array once to calculate the missing numbers in each gap between consecutive elements. +The algorithm iterates through each gap between elements, which takes O(n) time. +Gap calculations: For each pair of consecutive elements, we calculate the number of missing numbers, which is a constant-time operation. + +Space Complexity: O(1) +The algorithm only uses a constant amount of extra space: + +Variables like missing_count, k, nums[i], and temporary variables are scalar values, which take O(1) space. +No additional data structures (e.g., arrays or lists) are used to store intermediate results. + */ +class LinearSearchLCMissingElement { + public int missingElement(int[] nums, int k) + { + int missingCount = 0; + + for(int i=0; i between 7 and 4 => 7-4-1 =2 missing elements + + if(missingCount+currentGap >= k) + { + return nums[i] + (k-missingCount); + } + + missingCount += currentGap; + } + + return nums[nums.length - 1] + (k - missingCount); + } +} +/* +Input: nums = [4, 7, 9, 10], k = 3 + +Iteration 1 (i = 0): +Current Numbers: nums[0] = 4, nums[1] = 7 +Gap Calculation: +There are 7 - 4 - 1 = 2 missing numbers between 4 and 7 (specifically, 5 and 6). +Update missing_count: +Add these 2 missing numbers to missing_count. +Now, missing_count = 0 + 2 = 2. + +Since missing_count (2) is still less than 𝑘,we continue to the next gap. +Iteration 2 (i = 1): +Current Numbers: nums[1] = 7, nums[2] = 9 +Gap Calculation: +There is 9 - 7 - 1 = 1 missing number between 7 and 9 (specifically, 8). +Update missing_count: +Add this 1 missing number to missing_count. +Now, missing_count = 2 + 1 = 3. +Check if We've Reached the k + +Now, missing_count equals 𝑘 +k (3), meaning the k-th missing number is within this gap. + +Calculate the k-th Missing Number: +To find the exact +𝑘 +k-th missing number, use the formula: +nums[i] + (k-missingCount) +Substituting values: +=7+(3−2)=8 +*/ \ No newline at end of file