Search element in a circular sorted array

  Рет қаралды 124,869

mycodeschool

mycodeschool

Күн бұрын

Пікірлер: 60
@wonderInNoWhere
@wonderInNoWhere 3 жыл бұрын
You are a legend. Even almost ten years later, your explanation is the best.
@AruneshSrivastava
@AruneshSrivastava 5 жыл бұрын
Before watching this , i watched 2 other videos on similar issue , dint understand a damm thing , then i just see this video's link on suggestions and i smiled . The complexity of the problem doesn't bother you if you have a good teacher. Thanks man
@findoc9282
@findoc9282 3 жыл бұрын
as I keep learing and comparing I realize how energy saving it can be with a good teacher/tutotial
@excitingmonkey3970
@excitingmonkey3970 3 жыл бұрын
Man you are awesome! I was Leetcoding, got stuck in understanding the solution and searched for explanation. And here i am , mindblown by how simple you made me understand.
@RaoVenu
@RaoVenu 9 жыл бұрын
Thanks for the great tutorial. My implementation (assuming we already have implementations for findRotations and binary search in place) CircularArraySearch(A, n, x) { lowIndex = findRotations(A, n) // Index of lowest value in circular sorted array O(lgN) //Now we have two cases. The array to left of lowIndex is sorted. And the array to the right(including lowIndex is sorted) if (x == A[lowIndex]) return lowIndex // O(c) else if (x > A[lowIndex]) return BST(A, lowIndex + 1, n, x) // O(lgN) else return BST(A, 0, lowIndex - 1, x) //O(lgN) }
@S__Arslaan
@S__Arslaan 2 жыл бұрын
Amazing!!!No other youtube channel matches your level of explaination!!!!Thanks mycodeschool
@Aggarwal_Anshul
@Aggarwal_Anshul 5 жыл бұрын
Such an Efficient & Simple Solution to a tricky problem! Fantastic explanation BIG THANKS!
@mycodeschool
@mycodeschool 12 жыл бұрын
Thanks !
@Guillermorosales
@Guillermorosales 9 жыл бұрын
Thank you very much!!! for this series of videos, you explained it very well.
@Joyddep
@Joyddep 5 жыл бұрын
Exactly what I was looking for. Thank you for your excellent content!
@StartDeutsch
@StartDeutsch 9 жыл бұрын
Hi! What do you use for writing? A tablet or what? Looks very good! Thank you
@yunususmani3294
@yunususmani3294 7 жыл бұрын
Can't thanks enough...... You just saved my night :) Thanks a lot :)
@SaumyaSharma007
@SaumyaSharma007 3 жыл бұрын
Thank you so much....The evergreen lectures 🌟👌
@codingandmathvideos
@codingandmathvideos 10 жыл бұрын
great tutorial! you simply rock!!! God bless you. You probably should adjust the way you calculate the middle index. If you add 2 big positive numbers or 2 big negative numbers for that matter, you can get overflow. Use mid = low + (high - low)/2 instead to get the index of the middle element.
@AbhijithVMohan
@AbhijithVMohan 10 жыл бұрын
In typical system, overflow will only happen if the array has about 2**30 integers. In that case, the array itself will take ~4 GB space.
@saiadityagedda
@saiadityagedda 6 жыл бұрын
He already mentioned about that in his initial lecture
@S__Arslaan
@S__Arslaan 2 жыл бұрын
not if u use python
@saileshverma1942
@saileshverma1942 3 жыл бұрын
Thank you for your excellent teaching
@siddharthasharma9316
@siddharthasharma9316 3 жыл бұрын
as you mention that a circular sorted array with Distinct elements in description part of this problem , we can also skip equal sign- Array[mid] < Array[high] and same for Array[mid] > Array[low].
@siddharthasharma9316
@siddharthasharma9316 3 жыл бұрын
and thanks for such nice explanation.
@adityagupta6338
@adityagupta6338 9 жыл бұрын
Hey, thanks for this. It really helps. I want to just verify my approach is correct. How about we find the pivot using the previous tutorial, figure out which sorted array it belongs to (left of pivot or right of pivot) and apply binary search to whichever direction of pivot x belongs to? The time complexity is still 0 (log n), right?
@allanbee5752
@allanbee5752 10 жыл бұрын
Hi, Firstly, you are doing amazing job. Your teaching skills are superb!! Can you please share your algorithm approach to print all the permutations of a string? String length could be any length. e.g. abc, acb, bac,bca, cab,cba.
@swapnanilsharma
@swapnanilsharma 10 жыл бұрын
#include void swap (char *x, char *y) { char temp; temp = *x; *x = *y; *y = temp; } void res(char *A,int m,int n) { int i; static int num=1; if(m==n) { printf("[%d] ",num); printf("%s",A); printf(" "); num++; } else { for(i=m;i
@nouralaaeldinmounirhashem8570
@nouralaaeldinmounirhashem8570 5 жыл бұрын
Thanks for your great effort You are an excellent teacher
@bharathrajakumar1823
@bharathrajakumar1823 9 жыл бұрын
Simple and easy to understand. Thank you !!!
@jalajkhajotiaiitr
@jalajkhajotiaiitr 11 жыл бұрын
Thanks for your work sir, it is really helpful
@bhoopendrasharma9474
@bhoopendrasharma9474 4 жыл бұрын
Excellent tutorial, Thanks a ton. I believe this will work for duplicates as well, especially for the example shown in the tutorial { 2, 2, 2, 2, 2, 0, 2, 2 }. I tried it and found working.
@stym06
@stym06 6 жыл бұрын
best explanation ever.
@yuvarajravi7849
@yuvarajravi7849 6 жыл бұрын
Great tutorial. Other approach is just keep comparing first and last elements. Say if i=1, j=n-1, perform if a[i]
@harshbaliyan5867
@harshbaliyan5867 4 жыл бұрын
O(n) complexity still
@piyushjindal4955
@piyushjindal4955 9 жыл бұрын
simple and nice explanation :)
@vramali1
@vramali1 8 жыл бұрын
I am finding it hard to understand the code. I think i need a lot of example use cases for each of the conditions applied in the code.
@salemouail627
@salemouail627 4 жыл бұрын
Write it write it write it
@nobody2937
@nobody2937 2 жыл бұрын
Thank you very much... Thank you ...
@sawyerford2191
@sawyerford2191 3 жыл бұрын
How you were able to return the index value without returning it
@saisudheermittapalli574
@saisudheermittapalli574 8 жыл бұрын
Looks like you are not returning any value from CircularArraySearch() function in success case (when x is part of array)
@unknownman1
@unknownman1 4 жыл бұрын
mid is returned
@401varun
@401varun 10 жыл бұрын
Very nice.. this code is for ascending array rotations.. but doesn't work for descending array rotations... kindly generalize for both.. working for : {4,5,1,2,3} ---> initial array {1,2,3,4,5} rotated twice right . not working for : {5,4,8,7,6} --> initial array {8,7,6,5,4} rotated twice right. Thanks :)
@kiranmahesh93
@kiranmahesh93 6 жыл бұрын
same here
@dangidelta
@dangidelta 5 жыл бұрын
Just change every '' and vice-versa. It should work just fine.
@prs314
@prs314 8 жыл бұрын
have you considered the possibility of (low + high) overflowing in "int mid = (low + high) / 2"? Since int is signed the (low + high) will give an undefined behavior in C++ or a negative number in Java. I think the better way would be to cast them to unsigned int and shift. int mid = ((unsigned int) low + (unsigned int) high) >> 1;
@adarshverma3372
@adarshverma3372 2 жыл бұрын
One of the variation can be achieved using below code: private static int searchNumberInCircularSortedArray(int []inputs, int numberToSearch, int startIndex, int endIndex) { if (startIndex > endIndex) return -1; int mid = (startIndex + endIndex) / 2; if (inputs[mid] == numberToSearch) return mid; if (inputs[mid] = numberToSearch) return searchNumberInCircularSortedArray(inputs, numberToSearch, mid + 1, endIndex); return searchNumberInCircularSortedArray(inputs, numberToSearch, startIndex, mid - 1); } else { if ((inputs[mid] > numberToSearch && numberToSearch
@krushnnabaviskar2170
@krushnnabaviskar2170 2 жыл бұрын
Completed playlist
@sdhupar
@sdhupar 9 жыл бұрын
If array has duplicate elements then we can still apply Binary Search
@WhisperingWalnuts
@WhisperingWalnuts 9 жыл бұрын
saurabh Dhupar yes, i tried the code by giving array with duplicate elements as input, and it works!!! Simple and great algo!!
@artyzach
@artyzach 8 жыл бұрын
but wouldn't it only show the index of the first occurrence of duplication?
@dangidelta
@dangidelta 5 жыл бұрын
A recursive approach : int CircularlySorted(int Array[ ], int first, int last, int value){ if(first > last) return -1; int m = first + (last - first)/2; if(Array[m] == value) return m; else if(Array[m] = Array[first]){ if(Array[m] < value) return CircularlySorted(Array, m+1, last, value); return CircularlySorted(Array, first, m-1, value); } else if(Array[m] = Array[first]) return CircularlySorted(Array, first, m-1, value); return CircularlySorted(Array, m+1, last, value); } }
@Sohanbadaya
@Sohanbadaya 9 жыл бұрын
public int searchItem(int arr[], int item){ int low = 0; int high = arr.length-1; while(low arr[mid] && item
@srkrohit
@srkrohit 9 жыл бұрын
+Sohan badaya The authenticity of this code is only valid if the segment is sorted otherwise not. "if(item > arr[mid] && item
@Sohanbadaya
@Sohanbadaya 9 жыл бұрын
+Rohit Kumar You are right Rohit. Thanks :)
@omkarkulkarni3207
@omkarkulkarni3207 5 жыл бұрын
// Search an element in rotated/circular array. public static int findElementInCircularArray(int[]a, int n, int x) { int pivot=findRotationCount(a,n); if(x==a[pivot]) { return pivot; } if(x> a[0]) { //Search in sub-array left of the pivot return binarySearchRecursive(a,0,pivot-1,x); } else { //Search in sub-array right of the pivot return binarySearchRecursive(a,pivot+1,n,x); } }
@yashtailor1543
@yashtailor1543 4 жыл бұрын
love you @mycodeschool
@mayuraggarwal5840
@mayuraggarwal5840 5 жыл бұрын
This solution will not work if the element to be searched is present in unsorted part of the array.
@Hack_Neuron_To_DSA
@Hack_Neuron_To_DSA 2 жыл бұрын
Wrong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.
@Hack_Neuron_To_DSA
@Hack_Neuron_To_DSA 2 жыл бұрын
subscribe, share my channel and comment on them, then I would explain in depth how mine is correct
@tejaskadam1889
@tejaskadam1889 6 жыл бұрын
Hi please remove this videos this solution is not correct at all
@psn999100
@psn999100 5 жыл бұрын
Why not ? This exact implementation passed all test cases in Leetcode leetcode.com/problems/search-in-rotated-sorted-array/
How many times is a sorted array rotated?
13:03
mycodeschool
Рет қаралды 172 М.
Reverse a string or linked list using stack.
16:25
mycodeschool
Рет қаралды 387 М.
Wednesday VS Enid: Who is The Best Mommy? #shorts
0:14
Troom Oki Toki
Рет қаралды 50 МЛН
SLIDE #shortssprintbrasil
0:31
Natan por Aí
Рет қаралды 49 МЛН
What is binary search
12:45
mycodeschool
Рет қаралды 706 М.
Find merge point of two linked list
12:48
mycodeschool
Рет қаралды 108 М.
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 326 М.
Print 2-D array in spiral order
10:13
mycodeschool
Рет қаралды 229 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 216 М.
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,5 МЛН
Bug in Binary Search - Computerphile
11:31
Computerphile
Рет қаралды 289 М.
Check for balanced parentheses using stack
14:13
mycodeschool
Рет қаралды 481 М.