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
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);
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.
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
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; }
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
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....
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
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...
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?
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; }
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
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; }
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!!
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; }