diff --git a/JAVA/Algorithms/Searching/FindMinimumInRotatedSortedArray.java b/JAVA/Algorithms/Searching/FindMinimumInRotatedSortedArray.java new file mode 100644 index 0000000..43c9652 --- /dev/null +++ b/JAVA/Algorithms/Searching/FindMinimumInRotatedSortedArray.java @@ -0,0 +1,34 @@ +package JAVA.Algorithms.Searching; + +// PROBLEM LINK : https://leetcode.com/problems/find-minimum-in-rotated-sorted-array +/* +Explanation: +In rotated sorted array there are two sorted sections. +As the right section is lesser than the left section, the first element of the right section will be the minimum. +There are two conditions to find the merging point of the sections. +If the loop condition is violated, the array will be sorted. So the minimum will just be the forst element. +*/ + +class FindMinimumInRotatedSortedArray { + public static void main(String [] args) { + int [] nums = {3,4,5,1,2}; + int s = 0; // Start index + int e = nums.length - 1; // End index + int m = -1; // Mid index + while(s < e){ + m = s + (e - s)/2; + // Two conditions where the merging point of two sorted sections can be found + if(e > m && nums[m] > nums[m+1]) { + System.out.println(nums[m + 1]); + return; + } + else if(s < m && nums[m] < nums[m - 1]) { + System.out.println(nums[m]); + return; + } + else if(nums[m] < nums[e]) e = m - 1; + else if(nums[s] < nums[m]) s = m; + } + System.out.println(nums[0]); + } +} diff --git a/JAVA/Algorithms/Searching/FindPeakElement.java b/JAVA/Algorithms/Searching/FindPeakElement.java new file mode 100644 index 0000000..cd572b5 --- /dev/null +++ b/JAVA/Algorithms/Searching/FindPeakElement.java @@ -0,0 +1,21 @@ +package JAVA.Algorithms.Searching; + +// PROBLEM LINK : https://leetcode.com/problems/find-peak-element/submissions +/* +The condition for sorted array can be added. +*/ + +class FindPeakElement { + public static void main(String [] args) { + int [] nums = {1,2,3,1}; + int s = 0; + int e = nums.length - 1; + int m = -1; + while(s < e){ + m = s + (e - s)/2; + if(nums[m] < nums[m+1]) s = m+1; + else e = m; + } + System.out.println(s); + } +} diff --git a/JAVA/Algorithms/Searching/FirstAndLastInSortedArray.java b/JAVA/Algorithms/Searching/FirstAndLastInSortedArray.java new file mode 100644 index 0000000..5ffa33a --- /dev/null +++ b/JAVA/Algorithms/Searching/FirstAndLastInSortedArray.java @@ -0,0 +1,55 @@ +package JAVA.Algorithms.Searching; +import java.util.Arrays; + +// PROBLEM LINK : https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array + +/* +Explanation: +First check whether the array has the target by using simple binary search algorithm. +If yes then continue or print {-1, -1}. +An extra boolean variable check is added, if check is true means the algoithm is searching for the right index otherwise left index. +When the mid hits the target, mid is skipped to the right of left till there is no target left and it leads to the violation of + the loop condition. +Thus for the call of right index, right index will be end. In the call for left index the left index will be start. +*/ + +public class FirstAndLastInSortedArray{ + public static void main(String [] args){ + int [] nums = {5,7,7,8,8,10}; + int target = 8; + if(binarysearch(nums, target)){ + int rightindex = helpersearch(nums, target, true); + int leftindex = helpersearch(nums, target, false); + System.out.println(Arrays.toString(new int[]{leftindex, rightindex})); + } + System.out.println(Arrays.toString(new int[]{-1, -1})); + } + public static int helpersearch(int [] nums, int target, boolean check){ + if(nums.length == 1) return 0; + int s = 0; // Start index + int e = nums.length - 1; // End index + int m = -1; // Mid index + while(s <= e){ + m = s + (e - s)/2; + if(nums[m] > target) e = m - 1; + else if(nums[m] < target) s = m + 1; + else if(check) s = m + 1; // Skipping mid to right for the right index + else e = m - 1; // Skipping mid to the left for left index + } + if(check) return e; + else return s; + } + //Normal Binary Search Algorithm + public static boolean binarysearch(int [] nums, int target){ + int s = 0; + int e = nums.length - 1; + int m = -1; + while(s <= e){ + m = s + (e - s)/2; + if(nums[m] > target) e = m - 1; + else if(nums[m] < target) s = m + 1; + else return true; + } + return false; + } +} diff --git a/JAVA/Algorithms/Searching/FirstBadVersion.java b/JAVA/Algorithms/Searching/FirstBadVersion.java new file mode 100644 index 0000000..1be9d2a --- /dev/null +++ b/JAVA/Algorithms/Searching/FirstBadVersion.java @@ -0,0 +1,28 @@ +// PROBLEM LINK : https://leetcode.com/problems/first-bad-version +// This problem extents a VersionControl class defined in the leetcode question. + +/* The isBadVersion API is defined in the parent class VersionControl. + boolean isBadVersion(int version); */ + +public class Solution extends VersionControl { + public int firstBadVersion(int n) { + int s = 1; + int e = n; + int m = -1; + while(s <= e){ + m = s + (e - s)/2; + if(isBadVersion(m)) e = m - 1; + else s = m + 1; + } + return s; + } +} +/*Why this is a binarysearch question ? + * 1 ) This problem has a sorted array. + * 2 ) We should search the first bad product so its a searching question. + *How to solve ? + * 1 ) Implement normal binarysearch algorithm. + * 2 ) Whenever a product is bad, shift to the left ... until the array section wont have + * the bad product, in that case the condition for while loop terminates and start index will + * point at first bad product index in the array. + */ diff --git a/JAVA/Algorithms/Searching/MissingNumber.java b/JAVA/Algorithms/Searching/MissingNumber.java new file mode 100644 index 0000000..ade96a9 --- /dev/null +++ b/JAVA/Algorithms/Searching/MissingNumber.java @@ -0,0 +1,21 @@ +package JAVA.Algorithms.Searching; + +// PROBLEM LINK : https://leetcode.com/problems/missing-number +/* +Explanation: +The property of xor function is that, if we take xor of same numbers the result will be zero. +By taking the xor of all the elements and indices will give the missing number xor size of the array. +To avoid the size of the array, result is initialized by the size, so that both gets cancelled out. +*/ + +public class MissingNumber{ + public static void main(String [] args) { + int [] nums = {9,6,4,2,3,5,7,0,1}; + int result = nums.length; + for(int i=0; i= nums[s]) e = m - 1; // answer to the part of left sorted section + else s = m + 1; + }else if(nums[e] >= nums[m]){ + if(target > nums[m] && target <= nums[e]) s = m + 1; // answer to the part of right sorted section + else e = m - 1; + } + } + return -1; //if the answer is not found return -1 + } +} diff --git a/JAVA/Algorithms/Searching/SearchInsertPosition.java b/JAVA/Algorithms/Searching/SearchInsertPosition.java new file mode 100644 index 0000000..ea8b3a1 --- /dev/null +++ b/JAVA/Algorithms/Searching/SearchInsertPosition.java @@ -0,0 +1,29 @@ +package JAVA.Algorithms.Searching; + +// PROBLEM LINK : https://leetcode.com/problems/search-insert-position +/* +Explanation: +If the target is present, the algorithm is simple Binary Search. +If the target is absent, when the loop condition violates the start always will be the position where target + should be if it was present. +*/ + +class SearchInsertPosition { + public static void main(String [] args) { + int [] nums = {1,3,5,6}; + int target = 5; + int s = 0; + int e = nums.length - 1; + int m = -1; + while(s <= e){ + m = s + (e - s)/2; + if(nums[m] > target) e = m-1; + else if(nums[m] < target) s = m+1; + else { + System.out.println(m); + return; + } + } + System.out.println(s); + } +}