filmov
tv
Find an element in a sorted rotated array without finding pivot (minimum element)
Показать описание
Problem:
Given a sorted integer array with distinct elements which is rotated any number of times and an integer num, find the index of num in the array.
If not found, return -1.
Solution:
1: Initialize start = 0, end = array length - 1
2: Repeat following steps till start less than or equal to end:
(a) Set mid = (start + end)/2
(b) If num == array[mid], return mid.
(c) If array[start…mid] is sorted, i.e. array[start] is less than array[mid]
(i) If array[start] is less than or equal to num is less than or equal to array[mid], set end = mid-1
(ii) Else set start = mid+1
(d) Else If array[mid…end] is sorted, i.e. array[mid] less than or equal to array[end]
(i) If array[mid] is less than or equal to num is less than or equal to array[end], set start = mid+1
(ii) Else set end = mid-1
3: If not found, return -1.
Related Videos:
Given a sorted integer array with distinct elements which is rotated any number of times and an integer num, find the index of num in the array.
If not found, return -1.
Solution:
1: Initialize start = 0, end = array length - 1
2: Repeat following steps till start less than or equal to end:
(a) Set mid = (start + end)/2
(b) If num == array[mid], return mid.
(c) If array[start…mid] is sorted, i.e. array[start] is less than array[mid]
(i) If array[start] is less than or equal to num is less than or equal to array[mid], set end = mid-1
(ii) Else set start = mid+1
(d) Else If array[mid…end] is sorted, i.e. array[mid] less than or equal to array[end]
(i) If array[mid] is less than or equal to num is less than or equal to array[end], set start = mid+1
(ii) Else set end = mid-1
3: If not found, return -1.
Related Videos:
Search an element in a Linked List
Find Peak Element - Leetcode 162 - Python
How to find the Protons Neutrons and Electrons of an element on the Periodic table
Find Peak Element
8 Find an Element in a Rotated Sorted Array
Frequently Asked Python Program 11: How To Search an Element in a List
BS-26. Find Peak Element-II | Binary Search
Find a Peak Element | GeeksforGeeks
Daily Question - 118 | Find the output?#codelife #dailyquestion #shorts #js #output #evaluate #array
Single element in a sorted array | Leetcode #540
Search an element in a Sorted & Rotated Array | Binary Search, Part 3 | DSA-One Course #24
BS-4. Search Element in Rotated Sorted Array - I
The man who tried to fake an element
Find min and max element in a binary search tree
BS-9. Find Peak Element
Difference Between FindElement and FindElements in Selenium | Find Element in Selenium| Day 24
Find maximum element in an array (Largest element)
Difference between Find Element and Find Elements in Selenium | Selenium class 15th | Learn Selenium
Find a peak element in an array
Search an element in an Infinite Sorted array | Binary Search, Part 2 | DSA-One Course #23
How To Find Mid Element In The Given String
How To Find The Most Frequent Element In a List PYTHON ?!
How to search an element in an array in C++
Search an element in sorted Matrix( row and column sorted)
Комментарии