BS-7. Find out how many times array has been rotated

  Рет қаралды 141,726

take U forward

take U forward

Күн бұрын

Problem Link: bit.ly/3pMzTCh
Notes/C++/Java/Python codes: takeuforward.o...
We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/take...
0:00 Introduction of Course

Пікірлер: 189
@takeUforward
@takeUforward Жыл бұрын
Please comment understood and give us a like if you got everything :)
@sushamakumari4886
@sushamakumari4886 Жыл бұрын
Sir , please i request you make video in Hindi as well as ..sir there are so many students like me who waiting for you lecture in Hindi 🙏🙏
@bhargavonfire5978
@bhargavonfire5978 3 ай бұрын
​@@sushamakumari4886
@bhargavonfire5978
@bhargavonfire5978 3 ай бұрын
Understood
@pranavmisra5870
@pranavmisra5870 11 ай бұрын
Learning platforms appear, reach their peak and disappear, but takeuforward is going to be evergreen. The quality, the hardwork and the amazing way he teaches is unmatched.
@saswatrath4646
@saswatrath4646 10 ай бұрын
Yes
@dilipkumarbk7657
@dilipkumarbk7657 9 ай бұрын
Don't know how to thank you for your contribution to the community. I just did this problem by my self that I was able to use the previous question finding the minimum in a rotated sorted array. When I saw the question. I just remembered the line, "DON'T DIRECTLY JUMP TO THE SOLUTION, FIRST GIVE IT A TRY!" Thanks a lot striver bhaiya. I am feeling confident now, that I can understand the pattern in the problem.
@Beastboy-h6c
@Beastboy-h6c 6 күн бұрын
Hi bro, What are you doing right now?
@gurpritsingh2339
@gurpritsingh2339 Жыл бұрын
a tricky thing to spot here is whether the rotation is clockwise or anti-clockwise, in this problem we considered anti-clockwise rotation. the final rotated position can take a different no of steps depending upon the type of rotation.
@nownow1025
@nownow1025 28 күн бұрын
The current code works well for right rotations because the pivot index (`index`) represents the number of steps the array has been rotated to the right. For left rotations, you can calculate the equivalent number of rotations as: Left Rotations=(Size of Array)−Pivot Index (Smallest Element Index)
@inderjeet09
@inderjeet09 Жыл бұрын
Bro we need a playlist on Bit manipulation also . Please consider this request.
@gajsinghrathore8800
@gajsinghrathore8800 Жыл бұрын
TRY LOVE BABBAR
@iamnoob7593
@iamnoob7593 Жыл бұрын
Bit Manipulation is hardly asked in face to face interviews , But can be asked in OA.
@vaishnavejp9247
@vaishnavejp9247 7 ай бұрын
used a different approach. it goes to the side that is unsorted(as that will have the pivot element). it keeps doing that until nums[low] < nums[mid]. the moment we find that, we find our pivot value. int findKRotation(vector &nums) { // Code Here if((nums[0] < nums[nums.size()-1]) || (nums.size() == 1)) return 0; int low = 0, high = nums.size()-1; while(low nums[mid]) high = mid - 1; else low = mid + 1; if(nums[low] < nums[mid]) return low; } return 0; }
@Manasidas99
@Manasidas99 Жыл бұрын
Help me a lot thank you sir It is the biggest KZbin channel I follow every time on the channel. I learn lot .
@aadityamehta6342
@aadityamehta6342 Ай бұрын
solved this one on my own. Thanks Striver!!
@just_a_guy6985
@just_a_guy6985 5 ай бұрын
class Solution { public: int findKRotation(vector &arr) { int start = 0 ; int end = arr.size()-1; while(start< end) { int mid = start+(end-start)/2; if(arr[end]
@SuvradipDasPhotographyOfficial
@SuvradipDasPhotographyOfficial Жыл бұрын
Understood, awesome striver keep going, eagerly waiting for remaining videos
@cinime
@cinime Жыл бұрын
Understood! Wonderful explanation as always, thank you very much for your effort!!
@visase2036
@visase2036 Жыл бұрын
Thanks Striver. Adding my thoughts for (duplicates) . If we apply the previous logic of high-- or low++ as the mid and low/high values are equal , we will end up getting the minimum element but that does not gaurentee the no of times array has been rotated . Examples: array=[1,1,2,1,1] , orignal array = [1,1,1,1,2]. The correct answer is 3 (as the element at 0th index has been moved to 3rd index [1,1,2,1,1]). But if we apply the previous logic, the answer would come as 0 as 0th index is the minimum element. To upsolve this, we can do the following : Keep reducing high, untill [high-1] > [high] (2>1). Once you attain this point, high(3rd index) will be the answer.
@kaushiksen2190
@kaushiksen2190 Жыл бұрын
could you please elaborate and show the steps
@kaushiksen2190
@kaushiksen2190 Жыл бұрын
it is little confusing, please explain if possible
@dank7044
@dank7044 Жыл бұрын
well in case of duplicates, we just need to update the index, eveyrtime we encounter an elemwnt==min, whose index
@visase2036
@visase2036 Жыл бұрын
@@kaushiksen2190 in the example I mentioned , rotate the original array I highlighted 3 times. , You would get the rotated array as [1,1,2,1,1]. Now if you apply the same code , the minimum element would come as 1(0th index) which is correct but if you ask how many times the array is rotated ( the 1 which is originally at the 0th index have moved to 3rd Index) so 3 should the correct answer. To get 3 as your answer , you have to add one more condition which I mentioned !
@aisharawat9102
@aisharawat9102 10 ай бұрын
​@@dank7044can you share the code for the duplicate ones
@RitikLowanshi-d1s
@RitikLowanshi-d1s 8 күн бұрын
I understood this concept after watching this video.
@mehulthuletiya497
@mehulthuletiya497 Жыл бұрын
00:20 Problem statement 02:16 Code 03:54 Try out for duplicates to solve this problem
@Anshydv3
@Anshydv3 Жыл бұрын
bhaiya aap,, sbko knight and gaurdian badge dil ke rahoge , Leetcode pe ❤❤
@elizabethr5161
@elizabethr5161 Жыл бұрын
Best series ever..
@dakudevilyt751
@dakudevilyt751 6 ай бұрын
thank you so much great video
@manavsingh5919
@manavsingh5919 Жыл бұрын
Thank you Striver...Understood everything
@harshithareddygujjula2261
@harshithareddygujjula2261 5 ай бұрын
Understood very well!
@Gaurav_Tripathi_
@Gaurav_Tripathi_ Жыл бұрын
understood, wonderful explaination Bhaiya..!!
@kingbadshah452
@kingbadshah452 Жыл бұрын
understood everything thanks sstriver
@myproject6768
@myproject6768 Жыл бұрын
Absolutely understand ❤
@augustinradjou3909
@augustinradjou3909 Жыл бұрын
Understood boss🎉
@KCOYASH
@KCOYASH Жыл бұрын
understood :) , please continue such great videos
@infernogamer52
@infernogamer52 Жыл бұрын
Understood Bhaiya!
@yashsomani3042
@yashsomani3042 Жыл бұрын
Understood, Thank You!
@dgvlogs4183
@dgvlogs4183 Жыл бұрын
Understood😍😍😍😍😍😍
@PiyushKumar-xe1ng
@PiyushKumar-xe1ng 7 ай бұрын
int findKRotation(vector &arr) { int n=arr.size(); int low=0, high=n-1, ans=0; while(low
@riyabankawat4998
@riyabankawat4998 7 ай бұрын
awesome explanation.. bhaiya thank u
@NazeerBashaShaik
@NazeerBashaShaik 10 ай бұрын
Understood, thank you.
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
Understood. Thanks a lot.
@aakashsharma780
@aakashsharma780 Жыл бұрын
Understood ❤🙌🥳
@therandomguy21
@therandomguy21 8 ай бұрын
sir i have a confusion how do we know from where we have to count how many times the array has been rotated for ex - arr[] = [1,2,3,4,5] is if rotated from left hand gives the answer 2 and from right end it will give the answer 3 ?? for the rotated array = [3,4,5,1,2]
@time-barbaad
@time-barbaad 6 ай бұрын
right rotated
@HARSHAMANNE-bt1oo
@HARSHAMANNE-bt1oo 4 ай бұрын
It will be 54312 if it is right rotated by 3 places​@@time-barbaad
@HARSHAMANNE-bt1oo
@HARSHAMANNE-bt1oo 4 ай бұрын
It will be (n-index of min element)%n
@YourCodeVerse
@YourCodeVerse Жыл бұрын
Understood✅🔥🔥
@srija0608
@srija0608 3 ай бұрын
Understood 👍
@rahulsidhu5945
@rahulsidhu5945 Жыл бұрын
Please continue this playlist
@kiranmoura2974
@kiranmoura2974 Жыл бұрын
Understood ❤
@utsavseth6573
@utsavseth6573 Жыл бұрын
Understood.
@suraj_3240
@suraj_3240 Жыл бұрын
Understood 👍 👍
@bhavya8608
@bhavya8608 Жыл бұрын
understood!!
@DEVANSHVYAS-z1b
@DEVANSHVYAS-z1b 8 ай бұрын
Code for duplicates :- int findKRotation(int arr[], int n) { // code here int low = 0, high = n - 1; int minValue = INT_MAX; int k = -1; while(low low && arr[high-1] == arr[high]) high--; if(arr[high] < minValue) { k = high; minValue = arr[high]; } high = high - 1; continue; } if(arr[low] < arr[high]) { if(arr[low] < minValue) { k = low; minValue = arr[low]; } break; } if(arr[low]
@mainakdasgupta7130
@mainakdasgupta7130 5 күн бұрын
thank you man
@SYCOA12CHAITANYAASOLE
@SYCOA12CHAITANYAASOLE 8 ай бұрын
Understood !! 😎😎
@HarshSingh-fq3dv
@HarshSingh-fq3dv Жыл бұрын
Understood ❤
@anonymous-sz8jq
@anonymous-sz8jq 17 күн бұрын
Understoood!!
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
UNDERSTOOD
@ErenYeager-dp4er
@ErenYeager-dp4er 26 күн бұрын
Done on 14 Jan 2025 at 10:13 Place : Study Room 2 , Hostel 5 , IIT Bombay
@ZephyrrCummins
@ZephyrrCummins Жыл бұрын
can anyone explain why min() function is used directly??
@jitinroy2246
@jitinroy2246 Ай бұрын
to write line code in 1 code, by this you also save time in writing.
@ddevarapaga5134
@ddevarapaga5134 7 ай бұрын
Understood Brother
@UnknownforKnown-t1i
@UnknownforKnown-t1i Жыл бұрын
static int timesRotated(int[] a,int n) { int low=0; int high=n-1; int ans=Integer.MAX_VALUE; int index=-1; while (low
@aritrasaha3938
@aritrasaha3938 Жыл бұрын
it won't work for [2, 3, 1, 1] right?
@UnknownforKnown-t1i
@UnknownforKnown-t1i Жыл бұрын
It will work once dry run the code.​
@DeepakPatel-d5v
@DeepakPatel-d5v 9 ай бұрын
Thanks a lot Bhaiya
@anonymousofficial7824
@anonymousofficial7824 Жыл бұрын
Understood!
@hallupandet228
@hallupandet228 Жыл бұрын
Understood🕺
@aryanpatelyt7562
@aryanpatelyt7562 13 күн бұрын
Understood !!
@anshulrai6058
@anshulrai6058 3 ай бұрын
understood👍
@AnishRaj-gk2ll
@AnishRaj-gk2ll 6 ай бұрын
Just randomly found another solution to this with O(log(n)) int findKRotation(vector &arr) { int n = arr.size(); int left = 0; int right = n-1; int mid; while(leftarr[mid]) right = mid-1; else left = mid+1; } return left%n; }
@SriM-g6p
@SriM-g6p Жыл бұрын
understood!!!
@shubhamkumar170
@shubhamkumar170 Жыл бұрын
Understood!
@subhamghosh229
@subhamghosh229 Жыл бұрын
Understood :)
@sibiranganath
@sibiranganath 11 ай бұрын
UNDERSTOOD
@Learnprogramming-q7f
@Learnprogramming-q7f Жыл бұрын
Thank you bhaiya
@aumrudhlalkumar5487
@aumrudhlalkumar5487 6 ай бұрын
Thanks
@moonchild-il2xs
@moonchild-il2xs 5 ай бұрын
understood!
@Pw_Unfiltered
@Pw_Unfiltered 28 күн бұрын
was waiting for that iitb student comment here 😂ok let he come, we shall meet in some next vdo
@pihus3498
@pihus3498 Жыл бұрын
understood :)
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Understood
@MAHESHAITBECS
@MAHESHAITBECS 6 ай бұрын
excelleent
@TrinmoyDutta
@TrinmoyDutta 11 ай бұрын
will not the index of the minimum element be the number of rotations??? initially the minimum should be at 0, but the minimum is at index i due to i number of rotations.
@rupalikumari8829
@rupalikumari8829 7 ай бұрын
Understood:)
@Shivi32590
@Shivi32590 7 ай бұрын
thanq
@Manishgupta200
@Manishgupta200 Жыл бұрын
Same as minimum_in_sorted_array problem.
@oyeshxrme
@oyeshxrme 6 ай бұрын
understood bhaiya
@Anony.musk01
@Anony.musk01 Жыл бұрын
understood :)
@rahulab1165
@rahulab1165 Жыл бұрын
They said STRIVER i listen SAVIOUR
@codeman3828
@codeman3828 5 ай бұрын
Awesome
@sudhanshushekhar4222
@sudhanshushekhar4222 Жыл бұрын
Understood
@astronautgamer34
@astronautgamer34 8 ай бұрын
it is better to find min by finding the pivot and adding 1 to it
@divyadwivedi1527
@divyadwivedi1527 Жыл бұрын
understood
@Abhishekkumarnjw22-vf7cr
@Abhishekkumarnjw22-vf7cr 6 ай бұрын
❤❤
@singhji4149
@singhji4149 Жыл бұрын
If the array contains duplicates and then we have to find out the number of times the array is rotated ?????????????????????????????
@nownow1025
@nownow1025 28 күн бұрын
If arr[low] == arr[mid] == arr[high], it is unclear whether the pivot lies in the left or right half of the array. In such cases, we cannot rely on the usual binary search rules, so we must increment low or decrement high to bypass duplicates.
@nownow1025
@nownow1025 28 күн бұрын
int findKRotationLeft(vector &arr) { int low = 0, high = arr.size() - 1; int ans = 1e9 + 7; int index = -1; while (low
@kiranmoura2974
@kiranmoura2974 Жыл бұрын
I tried solving for duplicate elements also but was unable to do so ...can anyone help me out in this .
@nikhilthulaseedharan1525
@nikhilthulaseedharan1525 Жыл бұрын
Below is the solution for duplicates: public static int findKRotation(int nums[], int n) { int low=0; int high=n-1; int ans=Integer.MAX_VALUE; int index=-1; while(lownums[low]){ ans=nums[low]; index=low; } low++; high--; continue; } if(nums[low]nums[low]){ ans=nums[low]; index=low; } low=mid+1; }else{ if(ans>nums[mid]){ ans=nums[mid]; index=mid; } high=mid-1; } } return index; //TC:O(n/2) //SC:O(1) }
@divyakumardivyanshu4279
@divyakumardivyanshu4279 Ай бұрын
understand
@md_seraj786_
@md_seraj786_ 10 ай бұрын
understood all
@godson200
@godson200 Жыл бұрын
Whenever someone pronounces the word understood, I feel someone is calling my name (homophones)
@himanshukaushik9223
@himanshukaushik9223 Жыл бұрын
Find minimum ki 2 approach use nahi hori ish question ma meri can anyone explain??
@tamanna354
@tamanna354 4 ай бұрын
💗
@AnandKumar-lz2nf
@AnandKumar-lz2nf 4 ай бұрын
@raghavkansal3765
@raghavkansal3765 Жыл бұрын
"understood"
@girikgarg8
@girikgarg8 Жыл бұрын
Done
@jagadeeshp1163
@jagadeeshp1163 Жыл бұрын
US💯
@CodeMode9313
@CodeMode9313 Жыл бұрын
Habibi mst hai bideo
@shalinijha2615
@shalinijha2615 7 ай бұрын
class Solution { public int findKRotation(List arr) { int n=arr.size(); int lo=0; int hi=n-1; int min=Integer.MAX_VALUE; int minIndex=Integer.MAX_VALUE; while(lo
@anshsaxena7297
@anshsaxena7297 5 ай бұрын
UnderStood
@apoorva5961
@apoorva5961 3 ай бұрын
😇
@codingp110
@codingp110 8 ай бұрын
US!
@deeksha6514
@deeksha6514 2 ай бұрын
int findKRotation(vector &arr){ // Write your code here. int low = 0; int high = arr.size()-1; if(arr[low]
@tanujaSangwan
@tanujaSangwan 5 ай бұрын
Solved it without watching the video
@Itsme1n1ly
@Itsme1n1ly 3 ай бұрын
US
@harshitjaiswal9439
@harshitjaiswal9439 Жыл бұрын
Undetstood!
@AmiteshRanjan24
@AmiteshRanjan24 11 күн бұрын
"us"
BS-8. Single Element in Sorted Array
22:16
take U forward
Рет қаралды 199 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 223 М.
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 329 М.
How many times is a sorted array rotated?
13:03
mycodeschool
Рет қаралды 173 М.
How to Start LeetCode from ZERO in 2025
11:31
Ashish Pratap Singh
Рет қаралды 153 М.
How to Stop Procrastinating and Finally Take Action
16:31
Ali Abdaal
Рет қаралды 132 М.
I Spent 100 Hours Inside The Pyramids!
21:43
MrBeast
Рет қаралды 45 МЛН
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 338 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 694 М.
BS-9. Find Peak Element
32:53
take U forward
Рет қаралды 230 М.
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН