Комментарии:
while d > 0:
element = a[0]
a.pop(0)
a.append(element)
d -= 1
return a
Make it simple, BTW nice explaination
reverse was super
ОтветитьAwesome explanation!
ОтветитьCan you please explain why you have written k%= arr.length in code of method 2? It will be very helpful.
Ответить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?
Ответить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?
ОтветитьYou can reduce the number of rotation if rot value is greater than size of array by using modulo operator.
As the rotation of array gives again the same array when done 3 times in case of 3 sized array. so we can say. Total number of required rotation is (requiredrotation%sizeofarray).
like, you have 5 size array and u rotate it 8 times in either direction, instead of rotating it 8 times we can rotate it 8mod5 = 3 times as after 5 rotation the array will be become the original array.
Hence the required rotation will always be less than the size of array.
Love the clarity and the effects.
Beautifully done.
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()
can you please explain the flipping the matrix problem in java, hackerrank problem,
thanks in advance
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
method 1 is very intersting
thanks bro
really great bro thanks a lot!!!!
ОтветитьWhat if k is grater than array length?
& Explaination is mast :)
The code works only for k= 3 . For other values of k, it shows arrayOutofBound error
ОтветитьPython
Return (a[d:] + a[:d])
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<size;i++){
rotated_array[i]=arr[(i+d)%size];
}
return rotated_array;
}
}
Great explaination 😊
Ответитьthanku very much
ОтветитьMethod 2 is awesome logical thinking 🔥🔥🔥
Ответить