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

preview_player
Показать описание


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.

You can follow me across social media, all my handles are below:

0:00 Introduction of Course
Рекомендации по теме
Комментарии
Автор

Please comment understood and give us a like if you got everything :)

takeUforward
Автор

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.

pranavmisra
Автор

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<int> &nums) {
// Code Here

if((nums[0] < nums[nums.size()-1]) || (nums.size() == 1)) return 0;

int low = 0, high = nums.size()-1;

while(low <= high) {

int mid = (low+high)/2;

if(nums[low] > nums[mid]) high = mid - 1;
else low = mid + 1;

if(nums[low] < nums[mid]) return low;
}

return 0;
}

vaishnavejp
Автор

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.

dilipkumarbk
Автор

Understood! Wonderful explanation as always, thank you very much for your effort!!

cinime
Автор

Help me a lot thank you sir It is the biggest YouTube channel I follow every time on the channel. I learn lot .

Manasidas
Автор

Understood, awesome striver keep going, eagerly waiting for remaining videos

SuvradipDasPhotographyOfficial
Автор

Thank you Striver...Understood everything

manavsingh
Автор

Bro we need a playlist on Bit manipulation also . Please consider this request.

inderjeet
Автор

understood :) , please continue such great videos

KCOYASH
Автор

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.

gurpritsingh
Автор

understood, wonderful explaination Bhaiya..!!

Gaurav_Tripathi_
Автор

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.

visase
Автор

bhaiya aap, , sbko knight and gaurdian badge dil ke rahoge, Leetcode pe ❤❤

Anshydv