Тёмный
No video :(

HackerRank Arrays : Left Rotation Explained - Java 

Nick White
Подписаться 380 тыс.
Просмотров 34 тыс.
50% 1

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

 

22 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 46   
@sangeethap.h.4926
@sangeethap.h.4926 4 года назад
Thanks Nick for the solution. We can also go with the hashing technique static int[] rotLeft(int[] a, int d) { int count =0; int n = a.length; int [] new_arr = new int[n]; while(count
@nedraHarden
@nedraHarden 4 года назад
This is a great solution!
@neerajtadhiyal3152
@neerajtadhiyal3152 3 года назад
whts pivot mam?
@sangeethap.h.4926
@sangeethap.h.4926 3 года назад
@@neerajtadhiyal3152 pivot is the number of left rotation. I have changed the solution which matches the function definition.
@dhanushraju4062
@dhanushraju4062 3 года назад
easy code in java int n = a.length; // reading size for( int j = 0 ; j
@vern0312
@vern0312 2 года назад
My solution: ***they changed the input to an ArrayList*** int current = 0; Integer [] mylist = new Integer [a.size()]; while(current < a.size()){ int newIndex = current - d < 0? a.size() - (Math.abs(current - d)): current - d; mylist[newIndex] = a.get(current); current ++; } return Arrays.asList(mylist);
@MohammedElAssal123
@MohammedElAssal123 4 года назад
for(int i =0; i< a.Length;i++){ if(i>= d){ res[i - d] = a[i]; } else { res [ i + (a.Length - d)] = a[i]; } }
@thierrya588
@thierrya588 4 года назад
Good solution. An alternative solution is use % which uses one loop instead. So for example, if I'm rotating it 10 times, then it didn't really rotate. 10 % 5 (length) = 0.
@kunalaggarwal1928
@kunalaggarwal1928 4 года назад
can you provide the solution plz
@swarna_jee
@swarna_jee 3 года назад
yeah this is much better in terms of space and time complexity as well
@usernamesrbacknowthx
@usernamesrbacknowthx 5 месяцев назад
@@swarna_jee using two loops does not affect time complexity, O(n) + O(n) = O(n)
@vaniahmadramadhan1844
@vaniahmadramadhan1844 4 года назад
My own logic got message "Your code did not execute within the time limits" and when I see this video. I'm feeling like an idiot.
@shashwatsingh5129
@shashwatsingh5129 4 года назад
Same happened with me. Complexity was n squared lol. Looking at his version of the code makes me feel so dumb man😂
@TarekBuhdeima
@TarekBuhdeima Год назад
Solution in Kotlin: ```kotlin fun rotLeft(a: Array, d: Int): Array { val size = a.size val rotatedArray = MutableList(size){0} var i = 0 var rotateIndex = d while(rotateIndex < size){ rotatedArray[i] = a[rotateIndex] i++ rotateIndex++ } rotateIndex = 0 while(rotateIndex < d){ rotatedArray[i] = a[rotateIndex] i++ rotateIndex++ } return rotatedArray.toTypedArray() } ```
@atiqulislam4783
@atiqulislam4783 4 года назад
The initial rotated_index at line 17, I initialised it to 'd % size;'. My reasoning for this is because what if the rotation number was larger than the size of the array
@bnath3375
@bnath3375 3 года назад
Check out the constraints.. the value of d lies between 1 and n both inclusive
@amlsakr9957
@amlsakr9957 3 года назад
you are very good. I upgrade your solution to that static int[] rotLeft(int[] a, int d) { int [] rotated_array = new int [a.length]; for(int i =0 ; i= 0) { rotated_array[j] = a[i]; }else{ rotated_array[j+a.length] = a[i]; } } return rotated_array; }
@PhucNguyen-cd2rj
@PhucNguyen-cd2rj 3 года назад
i also come up with the solution with index i=0 and j=length-d-1, then we swap the elements. it works well. i think it is quite similar to yours so i wannna share with you guys
@rahulsaine9641
@rahulsaine9641 5 лет назад
nick you are amazing, the approach you used was simple and different, keep making such videos
@OneDayCreation
@OneDayCreation 3 года назад
simply add this line for taking input in an array... arr[(i+num-d)%num] = sc.nextInt(); here num is the number of elements in an array and d is the distance you want to rotate....
@stevothebeavo
@stevothebeavo 4 года назад
Just a silly note here, instead of two whiles, you should be able to read the original array. This would create the need for a check on rotated index greater than length but cuts out the second while completely
@loam
@loam 2 года назад
I wrote my solution, and then went into discussions, and saw there another solution without "if" statement, and guy in there mentioned that he used modular arithmetic. Then I checked out about modular arithmetic, still got no answers how to apply that, so finally decided to watch some videos with the explanation of how to solve that exact problem. Fuck, I don't get how your solution works, in case if "d" is much bigger than size of the array...
@rameshkaviath574
@rameshkaviath574 5 лет назад
nick, for the Array: Left Rotation code, am I right in saying we could also try by using a linkedList where we push all these array values to that linkedList first and then loop around a logic which makes head as tail for number of time to rotate. What do you think?
@rathan235
@rathan235 4 года назад
yeah.. that's a good solution too
@ahmetyusuf9964
@ahmetyusuf9964 3 месяца назад
My Solution public static List rotLeft(List a, int d) { int len = a.size(); int rotation_count = d % len; List rotated_arr = new ArrayList(); rotated_arr.addAll(a); for (int i = 0; i < len; i++) { rotated_arr.set( i - rotation_count < 0 ? i - rotation_count + len : i - rotation_count, a.get(i) ); } return rotated_arr; }
@rishiyadav3033
@rishiyadav3033 4 года назад
Superb bro u just made it look so easy 👌
@usernamesrbacknowthx
@usernamesrbacknowthx 5 месяцев назад
go b := make([]int32, len(a)) copy(b, a) newPos := 0 r := int(d) for r < len(a) { b[newPos] = a[r] newPos++ r++ } r = 0 for r < int(d) { b[newPos] = a[r] newPos++ r++ } return b
@yolo-ed9dk
@yolo-ed9dk 2 года назад
they changed loads of the datatypes to ArrayLists now in all of their questions
@monishnjs
@monishnjs 4 года назад
In ruby arr.roatate(d) will give us the rotated array. Will such solutions be accepted in interviews or in contests
@rajatdubey6951
@rajatdubey6951 4 года назад
No ...it will be accepted in NASA
@606shann
@606shann 4 года назад
This is amazing... Helped me a lot... different way of thinking great bro...
@kamauwairegi
@kamauwairegi 3 года назад
My solution static int[] rotLeft(int[] a, int d) { int[] arr = new int[a.length]; for (int i = 0; i < a.length; i++) { int position = i - d; if (position < 0) { position = a.length + position; } arr[position] = a[i]; } return arr; }
@looserxxx4017
@looserxxx4017 4 года назад
thanks man it didn't really helped me for what i was looking but still amazing work
@alcaponelol3980
@alcaponelol3980 5 лет назад
u so good. pls go on to other problems ^^
@TheAdun
@TheAdun 2 года назад
This doesn't work for large number of rotations
@althemad
@althemad 4 года назад
Hey Nick, Your videos are awesome and inspiring! (so I started to think what other ways could be to solve it :)) I tried this one myself and took me too much time, but here it is: static int[] rotLeft(int[] a, int d) { int tempValue =0; int[] tempArr = new int[a.length]; tempValue = a[0]; for(int i = 0; i < d; i++){ for(int j = 0; j < tempArr.length-1; j++){ tempArr[j] = a[j+1]; } tempArr[tempArr.length-1] = tempValue; tempValue = tempArr[0]; System.arraycopy(tempArr, 0, a, 0, a.length); } return tempArr; } Thanks!!
@rohitkumar-bi5xc
@rohitkumar-bi5xc 4 года назад
hey can u solve this by using for loop
@karandeepsingh2311
@karandeepsingh2311 3 года назад
you took another array in the method. explain solution with single array that given in argument.... int[] a.
@tracy_gao
@tracy_gao 4 года назад
Neet code O(N) complicity
@mohammedjahangirhossain549
@mohammedjahangirhossain549 4 года назад
Can anyone please confirm if below code is good ? public static int [] result(int [] arr, int target) { int [] rotatedArray = new int [arr.length]; int i = 0; int j = arr.length-1; int c = 0; while(c < arr.length) { if(target < arr[j]) { rotatedArray[c++] = arr[j--]; }else { rotatedArray[c++] = arr[i++]; } } return rotatedArray; }
@ashishupadhyay6881
@ashishupadhyay6881 2 года назад
This is not an in-place solution
Далее
Google Coding Interview Question - firstDuplicate
20:17
HackerRank 2D Array - DS Explained - Java
9:21
Просмотров 49 тыс.
Never Troll Shelly🫡 | Brawl Stars
00:10
Просмотров 1,4 млн
HackerRank Two Strings Explained - Java
8:27
Просмотров 17 тыс.
Self Taught Programmers... Listen Up.
11:21
Просмотров 1 млн
HackerRank Luck Balance Solution Explained - Java
10:59
Coding Was Hard Until I Learned THESE 5 Things!
8:02
I've been using Redis wrong this whole time...
20:53
Просмотров 353 тыс.
Arrays Left Rotation HackerRank Solution
11:52
Просмотров 46 тыс.
HackerRank Sock Merchant Solution Explained - Java
4:36
Never Troll Shelly🫡 | Brawl Stars
00:10
Просмотров 1,4 млн