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:
1: Find an element in a sorted rotated array: https://www.youtube.com/watch?v=6pSzsJH56BA&index=3&list=PLamzFoFxwoNhR2uFoqm6nr8VgsERgTmYy
2: Binary Search Playlist: https://www.youtube.com/playlist?list=PLamzFoFxwoNhR2uFoqm6nr8VgsERgTmYy
Code and Algorithm Visualization: http://www.ideserve.co.in/learn/find-an-element-in-a-sorted-rotated-array-without-finding-pivot
Website: http://www.ideserve.co.in
Facebook: https://www.facebook.com/IDeserve.co.in
Видео Find an element in a sorted rotated array without finding pivot (minimum element) канала IDeserve
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:
1: Find an element in a sorted rotated array: https://www.youtube.com/watch?v=6pSzsJH56BA&index=3&list=PLamzFoFxwoNhR2uFoqm6nr8VgsERgTmYy
2: Binary Search Playlist: https://www.youtube.com/playlist?list=PLamzFoFxwoNhR2uFoqm6nr8VgsERgTmYy
Code and Algorithm Visualization: http://www.ideserve.co.in/learn/find-an-element-in-a-sorted-rotated-array-without-finding-pivot
Website: http://www.ideserve.co.in
Facebook: https://www.facebook.com/IDeserve.co.in
Видео Find an element in a sorted rotated array without finding pivot (minimum element) канала IDeserve
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
Programming Interview Question: How to print all diagonal's sums for a given binary tree?Level Order TraversalReverse a Linked List - IterativeProgramming Interview Question: Searching a 2D Sorted MatrixImplement a fair coin given an unfair coinBuying and selling stocksBinary SearchMinimum length subarray of an unsorted array sorting which results in complete sorted arrayMaximum size square sub-matrix with all 1sBuilding Bridges Dynamic ProgrammingLeaders in an arrayFind intersection of two Linked Lists - O(A + B) Time Complexity and O(1) Space ComplexityCreate a balanced Binary Search Tree (BST) from a sorted arrayNext greater element in an arraySpiral level order traversal of a binary treeProgramming Interview Question: Recover Binary Search TreeDetect a loop in a linked listProgramming Interview Question: Find intersection of two Linked ListsFind an element in a sorted rotated arrayDemo of IDeserve web platform www.ideserve.co.in