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

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

Nikhil Lohia

3 года назад

17,365 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@anveshpandey2917
@anveshpandey2917 - 10.10.2023 06:18

while d > 0:
element = a[0]
a.pop(0)
a.append(element)
d -= 1
return a


Make it simple, BTW nice explaination

Ответить
@MAHESHKONAR
@MAHESHKONAR - 16.11.2022 16:13

reverse was super

Ответить
@MsProtestante
@MsProtestante - 28.10.2022 21:53

Awesome explanation!

Ответить
@anirudhbhardwaj2693
@anirudhbhardwaj2693 - 20.09.2022 15:07

Can you please explain why you have written k%= arr.length in code of method 2? It will be very helpful.

Ответить
@evayang7251
@evayang7251 - 02.09.2022 05:15

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?

Ответить
@abhasupadhayay6420
@abhasupadhayay6420 - 26.06.2022 16:27

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?

Ответить
@AlberTesla1024
@AlberTesla1024 - 06.06.2022 11:25

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.

Ответить
@CostaKazistov
@CostaKazistov - 09.02.2022 09:29

Love the clarity and the effects.
Beautifully done.

Ответить
@fredosuala8605
@fredosuala8605 - 31.12.2021 14:00

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()

Ответить
@pawankrishna8918
@pawankrishna8918 - 04.11.2021 09:52

can you please explain the flipping the matrix problem in java, hackerrank problem,
thanks in advance

Ответить
@likhithakande2902
@likhithakande2902 - 23.09.2021 17:46

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

Ответить
@prabhatkumarjha4578
@prabhatkumarjha4578 - 14.09.2021 17:02

method 1 is very intersting
thanks bro

Ответить
@techmoon_
@techmoon_ - 21.06.2021 17:34

really great bro thanks a lot!!!!

Ответить
@srirampujar668
@srirampujar668 - 21.04.2021 15:25

What if k is grater than array length?
& Explaination is mast :)

Ответить
@aera218
@aera218 - 10.04.2021 10:33

The code works only for k= 3 . For other values of k, it shows arrayOutofBound error

Ответить
@shubhamsharma-ec3re
@shubhamsharma-ec3re - 02.04.2021 18:18

Python

Return (a[d:] + a[:d])

Ответить
@thanos9704
@thanos9704 - 01.03.2021 05:44

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;
}
}

Ответить
@tonmoychiran3000
@tonmoychiran3000 - 13.02.2021 21:18

Great explaination 😊

Ответить
@kishorjha5028
@kishorjha5028 - 11.01.2021 19:48

thanku very much

Ответить
@arundoss6785
@arundoss6785 - 22.11.2020 19:45

Method 2 is awesome logical thinking 🔥🔥🔥

Ответить