Find Pivot in rotating Array in JAVA - Learn Java.
// Rotating array is the array with is shorted be can be rotated a number of times.
// {1,2,3,4,5,6,7,8,9} => simple array.
// {9,1,2,3,4,5,6,7,8} => One Rotation
// {8,9,1,2,3,4,5,6,7} => 2nd Rotation
// {7,8,9,1,2,3,4,5,6} => 3rd Rotation
// => with each rotation just last element come at first.
// Find the pivot of array (larget number index)
// Apply binary search at right if not found apply at left.
// Our task is to find the pivot
public class RotatingAarrayTarget {
public static void main(String[] args) {
int[] arr = {7,8,9,10,2,3,4,5,6};
int target = 3;
int ourPivod = findPivod(arr);
System.out.println(ourPivod);
}
static int findPivod(int[] arr){
int start = 0;
int end = arr.length - 1;
while (start <= end ){
int mid = start + (end - start)/2;
// case 1
if (arr[mid] > arr[mid+1]){
return mid;
}
// case 2
if (arr[mid-1] > arr[mid]){
return mid - 1;
}
// case 3 and 4
if (arr[start] >= arr[mid]){
end = mid - 1;
}else{
start = mid +1;
}
}
return -1;
}
}
// Case 1: When mid < mid + 1 => mid is pivot
// Case 2: When mid-1 > mid; then mid-1 is pivot
// Case 3: When start >= mid then all number right of mid is smaller so we ignore them.
// Case 4: When start < mid then first half is soted array, then we ignore the left side and work with right parts
// for case 4 use start = mid +1;
// 3:10: of video
Leave Find Pivot in rotating Array in JAVA - Learn Java. to:
Read more #java-tutorial posts
Best Posts From SPYDO
We have not curated any of spydo's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From SPYDO
- Celebrating Ganesh Chaturthi in Indore | India
- Lohri Celebration in Punjab | Chandigarh | India
- This is how we celebrate diwali in India | Indian Festival.
- Java vs C++ for Data Structures & Algorithms
- Complete Dsa course
- Sweet Corn Chaat Trio: Butter, Cheese & Chili, and Masala Corn.
- Crispy Cheese Balls Recipe – Café Style Magic at Home!
- क्रिस्पी मिक्स वेज पकौड़े – बाहर से क्रंची, अंदर से मज़ेदार!
- Lasooni Palak Paneer - Dhaba Style Recipe with a Twist!
- Authentic Dhaba-Style Butter Dal with Smoky Tandoori Roti – A Flavorful Delight!
- Our Hive - A Hero of Digital Revolution
- Our Hive - A Hero of Digital Revolution
- What is Redux: Beginner's Guide to React State Management | Part 1
- Find Pivot in rotating Array in JAVA - Learn Java.
- Create Eye-Catching YouTube Thumbnails with Canva: A Step-by-Step Guide
- Making Optional Properties Nullable in TypeScript | Revive Coding
- Best Practices and Strategies for Promoting My Video and Increasing its Visibility ?
- Why Not use AND (&&) Operator for Conditional Rendering in React
- Why Not use AND (&&) Operator for Conditional Rendering in React
- Top 10 Pro-Level React Do’s & Don’ts for Developers | Revive Coding