From 9aa1805d53a1b1c251f44ab25debd8eec51539ff Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 09:29:42 +0530 Subject: [PATCH 1/7] Search in rotated sorted array --- .../Searching/SearchInRotatedSortedArray.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 JAVA/Algorithms/Searching/SearchInRotatedSortedArray.java diff --git a/JAVA/Algorithms/Searching/SearchInRotatedSortedArray.java b/JAVA/Algorithms/Searching/SearchInRotatedSortedArray.java new file mode 100644 index 0000000..518ae96 --- /dev/null +++ b/JAVA/Algorithms/Searching/SearchInRotatedSortedArray.java @@ -0,0 +1,43 @@ +package JAVA.Algorithms.Searching; + +// PROBLEM LINK : https://leetcode.com/problems/search-in-rotated-sorted-array/ + +/* +Explanation: +The Rotated sorted array has two sorted sections, let the sections be called as left and right section respectively. +The elements in the left section will be always greater than the element in the right section. +The mid is found in the middle of start and end index which is initialized as the start and end of the array. +There are three paths for the flow of the program: + 1) If the target is found in the middle index, just return the index. + 2) If the target lies in the part of left section, ie if start element is less than mid element and target is in between these, + move end to middle - 1. Otherwise move start to middle + 1. + 3) If the target lies in the part of right section, ie if end element is greater than mid element and target is in between these, + move start to middle + 1. Otherwise move end to middle - 1. + If the target isn't found just return -1. +*/ + +public class SearchInRotatedSortedArray +{ + public static void main (String[]args) + { + int [] nums = {0,1,2,4,5,6,7}; + System.out.println(search(nums, 5)); + } + public static int search(int[] nums, int target) { + int s = 0; // start index initialized to the start of the array + int e = nums.length - 1; // end index initialized to the end of the array + int m = -1; + while(s <= e){ + m = s + (e - s)/2; // middle index found by taking the middle between start and end + if(nums[m] == target) return m; // answer found at mid + else if(nums[s] <= nums[m]){ + if(target < nums[m] && target >= 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 + } +} From d32887a0a210a3e386a3647a025e70280643c16b Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 10:28:32 +0530 Subject: [PATCH 2/7] Find first and last position in a sorted array --- .../Searching/FirstAndLastInSortedArray.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 JAVA/Algorithms/Searching/FirstAndLastInSortedArray.java 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; + } +} From c64153da0e2a5dba4692523c1726510acaf9002a Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 10:46:16 +0530 Subject: [PATCH 3/7] Search Insert Position --- .../Searching/SearchInsertPosition.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 JAVA/Algorithms/Searching/SearchInsertPosition.java 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); + } +} From 3c91cb995075099203245984d50c01fbbb491051 Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 11:02:37 +0530 Subject: [PATCH 4/7] Find the minimum in rotated sorted array --- .../FindMinimumInRotatedSortedArray.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 JAVA/Algorithms/Searching/FindMinimumInRotatedSortedArray.java 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]); + } +} From ce31215c0ddc8d777e70bb66f944a7d3c1e43bd3 Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 11:13:24 +0530 Subject: [PATCH 5/7] Find Peak Element --- .../Algorithms/Searching/FindPeakElement.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 JAVA/Algorithms/Searching/FindPeakElement.java 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); + } +} From f278af8e91e796effbaa6f92d4c136cf2e998b97 Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 11:18:13 +0530 Subject: [PATCH 6/7] First Bad Version --- .../Algorithms/Searching/FirstBadVersion.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 JAVA/Algorithms/Searching/FirstBadVersion.java 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. + */ From 0a8ed89bd8945dd1f41ad8a8e88b27a5c69fc957 Mon Sep 17 00:00:00 2001 From: Ritvik Saran S <88278326+RitvikSaranS@users.noreply.github.com> Date: Wed, 12 Oct 2022 11:31:39 +0530 Subject: [PATCH 7/7] Find the Missing Number --- JAVA/Algorithms/Searching/MissingNumber.java | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 JAVA/Algorithms/Searching/MissingNumber.java 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