Тёмный

HackerRank - Left Rotation | Full Solution and Examples | Study Algorithms 

Nikhil Lohia
Подписаться 49 тыс.
Просмотров 19 тыс.
50% 1

Опубликовано:

 

6 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 43   
@CostaKazistov
@CostaKazistov 2 года назад
Love the clarity and the effects. Beautifully done.
@tuntunewala
@tuntunewala 3 года назад
brother i saw videos of many big youtubers but u got the point themx vro😊😊😊
@kannankannank
@kannankannank 2 года назад
Amazing explanation. I just have a question regarding space complexity you mentioned in 12:30. Isn't the space complexity O(1) ? My assumption was that we are using only the given array for processing and the final result is also present inside the same array, hence extra array wasn't required. Could you explain how it became O(n) ?
@nikoo28
@nikoo28 2 года назад
You are absolutely correct. It will be O(1). Thanks for the correction.
@anveshpandey2917
@anveshpandey2917 Год назад
while d > 0: element = a[0] a.pop(0) a.append(element) d -= 1 return a Make it simple, BTW nice explaination
@spouter
@spouter 3 месяца назад
In method-1 , you should change the code to temp[i-k+1], otherwise it will give arrayindexoutofbound exception
@prabhatkumarjha4578
@prabhatkumarjha4578 3 года назад
method 1 is very intersting thanks bro
@evayang7251
@evayang7251 2 года назад
Hi, Nikhil, thanks for sharing your talented solutions. I didn't notice that there are so many things could be considered and discussed in such a relative easy problem. By the way, could you tell me what app or tools are you using when you explaining the problem. Is it a notebook app?
@nikoo28
@nikoo28 2 года назад
thanks for your feedback and support :) I am using the GoodNotes app. All the links are always available in video descriptions. Cheers!!
@fredosuala8605
@fredosuala8605 2 года назад
A more strightforward kotlin solution. This works even if k is greater than the array lenght val length = arr.size val splitIndex = d % length if (splitIndex == 0) return arr val leadArr = arr.slice(0 until splitIndex) val trailArr = arr.slice(splitIndex until length) return (trailArr + leadArr).toTypedArray()
@nikoo28
@nikoo28 2 года назад
Interesting approach…
@arundoss6785
@arundoss6785 3 года назад
Method 2 is awesome logical thinking 🔥🔥🔥
@nikoo28
@nikoo28 3 года назад
Glad you liked it!!
@MsProtestante
@MsProtestante Год назад
Awesome explanation!
@srirampujar668
@srirampujar668 3 года назад
What if k is grater than array length? & Explaination is mast :)
@nikoo28
@nikoo28 3 года назад
Let us suppose array size is 5, and the value of k=6. Then, after 5 rotations array will become the same as original. So, 6 rotations is same as just rotating 1 time. Hence, if you look at the code the first thing we do is "k = k % size" This will change the value of k to resultant rotations which would be less than array size.
@anirudhbhardwaj2693
@anirudhbhardwaj2693 2 года назад
Can you please explain why you have written k%= arr.length in code of method 2? It will be very helpful.
@nikoo28
@nikoo28 2 года назад
Think like this…if your array size is 5 and you rotate it 12 times…what will happen? After 5 rotations the array will be same as original…again after 5 rotations the array will be same…you completed 10 rotations…and then you have just 2 more rotation left So basically you only have to do 12%5 = 2 rotations
@nikoo28
@nikoo28 2 года назад
Hope this helps
@Srishreya
@Srishreya 7 месяцев назад
Thank you sir
@abhasupadhayay6420
@abhasupadhayay6420 2 года назад
Can you please tell the intuition behind Method -2 (reverse logic). How to start thinking of such an approach? Is it something we should just know?
@nikoo28
@nikoo28 2 года назад
I would say that as you solve more and more problems you will start seeing these kind of patterns in problems. Reversing, rotating…these are all array manipulation techniques…so the more you solve, the more you learn.
@tonmoychiran3000
@tonmoychiran3000 3 года назад
Great explaination 😊
@techmoon_
@techmoon_ 3 года назад
really great bro thanks a lot!!!!
@likhithakande2902
@likhithakande2902 3 года назад
Bro, can we take the first k numbers and then store in in a temp(1) And then we will take the remaining part of array(2) and store at one position Now, I will append the temp to the second array...... I will call this as method three
@nikoo28
@nikoo28 3 года назад
This is what I exactly do in Method 1 :)
@thanos9704
@thanos9704 3 года назад
METHOD 3 include this also import java.util.Arrays; public class LeftRotate { public static void main(String[] args) { int[] arr = {1, 2, 3, 4,5}; System.out.println(Arrays.toString(leftRotate(arr,3))); //4,5,1,2,3 } static int[] leftRotate(int[] arr, int d) { int size=arr.length; int[] rotated_array = new int[size]; for(int i=0;i
@pawankrishna8918
@pawankrishna8918 2 года назад
can you please explain the flipping the matrix problem in java, hackerrank problem, thanks in advance
@nikoo28
@nikoo28 2 года назад
Check out my video on “Rotate Image”, the concept should be similar. I will try to add a video for your problem too sometime in the future
@pawankrishna8918
@pawankrishna8918 2 года назад
@@nikoo28 thank you
@11csepratikshaargulewar71
@11csepratikshaargulewar71 5 месяцев назад
Why modulo operator is used for k?
@nikoo28
@nikoo28 5 месяцев назад
Because let us say you have a size of 4 and you are rotating 10 times. First 4 rotations the array becomes the same. Second 4 rotations, again the array is same. That means only the last 2 rotations are meaningful. Hence we do 10 % 4 which = 2
@manasjena5124
@manasjena5124 7 месяцев назад
Sir please run the first approach in compiler because i am tried it but it shows error please sir
@nikoo28
@nikoo28 6 месяцев назад
check out my code on github...link available in description
@kishorjha5028
@kishorjha5028 3 года назад
thanku very much
@nikoo28
@nikoo28 3 года назад
Most welcome
@ai_universeman
@ai_universeman Год назад
reverse was super
@aera218
@aera218 3 года назад
The code works only for k= 3 . For other values of k, it shows arrayOutofBound error
@nikoo28
@nikoo28 3 года назад
which method are you facing problems with?
@aera218
@aera218 3 года назад
@@nikoo28 the brute force method. I couldn't find your account on any other platform, or else I wanna gonna dm/mail it. The code runs only for k= 3. If we change it's value, the error is Array out of bound index. I tried resizing the array , and doing minute Changes while assigning elements of temp[ ] array to arr[ ] . None worked.
@nikoo28
@nikoo28 3 года назад
If you look at my channel info, there is a email address available. DM me your doubt over there. I have tried the code and all the cases pass on HackerRank. Would be happy to assist you.
@_COMP_B_SURAJMISHRA
@_COMP_B_SURAJMISHRA 2 года назад
#include using namespace std; int main(){ int n,d=0; cin>>n>>d; int arr[n]; int size=sizeof(arr)/sizeof(arr[0]); int temp[d]; int dsize=sizeof(temp)/sizeof(temp[0]); cout
@shubhamsharma-ec3re
@shubhamsharma-ec3re 3 года назад
Python Return (a[d:] + a[:d])
Далее
НЮША УСПОКОИЛА КОТЯТ#cat
00:43
Просмотров 1 млн
This mother's baby is too unreliable.
00:13
Просмотров 22 млн
Java Program to left rotate the elements of an array
9:04
НЮША УСПОКОИЛА КОТЯТ#cat
00:43
Просмотров 1 млн