Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions JAVA/Algorithms/Searching/FindMinimumInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -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]);
}
}
21 changes: 21 additions & 0 deletions JAVA/Algorithms/Searching/FindPeakElement.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
55 changes: 55 additions & 0 deletions JAVA/Algorithms/Searching/FirstAndLastInSortedArray.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions JAVA/Algorithms/Searching/FirstBadVersion.java
Original file line number Diff line number Diff line change
@@ -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.
*/
21 changes: 21 additions & 0 deletions JAVA/Algorithms/Searching/MissingNumber.java
Original file line number Diff line number Diff line change
@@ -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.length; i++){
result ^= i;
result ^= nums[i];
}
System.out.println(result);
}
}
43 changes: 43 additions & 0 deletions JAVA/Algorithms/Searching/SearchInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -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
}
}
29 changes: 29 additions & 0 deletions JAVA/Algorithms/Searching/SearchInsertPosition.java
Original file line number Diff line number Diff line change
@@ -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);
}
}