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) ?
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?
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()
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.
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
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.
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 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
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
@@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.
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.
#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