Rotate Array by K times:
Lets says array [1,2,3,4,5,6,7,8], K = 2
result = [7,8,1,2,3,4,5,6]
if K>N where N = length of array and K = numer of rotations
let's say k=20, then k%n gives us the number of rotations
Can do it in 3 ways:
Approach 1: (Naive or Brute Force Approach)
[1,2,3,4,5,6,7,8] -> [8,1,2,3,4,5,6,7] -> [7,8,1,2,3,4,5,6]
If k=5, then it will be more run time
Time Complexity: O(N*K) where N = length of array and K = numer of rotations
Space Complexity: O(1) as no space is used
Approach 2:(Using Extra Space)
Create a new array of same size, and then arrange elements accordingly
Time Complexity: O(N) where N = length of array and K = numer of rotations
Space Complexity: O(N) as elements of N space is used
References:
https://www.youtube.com/watch?v=utE_1ppU5DY
Lets says array [1,2,3,4,5,6,7,8], K = 2
result = [7,8,1,2,3,4,5,6]
if K>N where N = length of array and K = numer of rotations
let's say k=20, then k%n gives us the number of rotations
Can do it in 3 ways:
Approach 1: (Naive or Brute Force Approach)
[1,2,3,4,5,6,7,8] -> [8,1,2,3,4,5,6,7] -> [7,8,1,2,3,4,5,6]
If k=5, then it will be more run time
Time Complexity: O(N*K) where N = length of array and K = numer of rotations
Space Complexity: O(1) as no space is used
Approach 2:(Using Extra Space)
Create a new array of same size, and then arrange elements accordingly
Time Complexity: O(N) where N = length of array and K = numer of rotations
Space Complexity: O(N) as elements of N space is used
Approach 3:(Optimized)
Three main points:
Create your own Reverse Function which takes the array, start and end indexes
1) Reverse all Elements
2) Reverse first K elements
3) Reverse N-K Elements
or
similar to above algorithm
1) Make two parts of array as (N-K) and K {A,B}
2) Reverse A and B
3) Reverse whole A and B that we got from 2
or
similar to above algorithm
1) Make two parts of array as (N-K) and K {A,B}
2) Reverse A and B
3) Reverse whole A and B that we got from 2
Time Complexity: O(N) where N = length of array and K = numer of rotations
Space Complexity: O(1) as No extra space is used
Space Complexity: O(1) as No extra space is used
Quickly find multiple left rotations of an array:
Coming soon...
References:
https://www.geeksforgeeks.org/quickly-find-multiple-left-rotations-of-an-array/
Land Right Rotation of a String:
coming soon...
References:
https://www.geeksforgeeks.org/left-rotation-right-rotation-string-2/
Array Rotation in Place:
Accomplish above by using the Array Rotation in place:
Example: [1,2,3,4,5,6,7,8,9]
output: [4,5,6,7,8,9,1,2,3]
Steps:
1) Divide array into sets & rotate each element in set k postitions to left
2) outer loop: number of sets, inner loop: rotate elements of a set k postions to the left
number of sets?? (Use GCD to calculate the number of sets)
Coming soon...
References:
https://www.geeksforgeeks.org/quickly-find-multiple-left-rotations-of-an-array/
Land Right Rotation of a String:
coming soon...
References:
https://www.geeksforgeeks.org/left-rotation-right-rotation-string-2/
Array Rotation in Place:
Accomplish above by using the Array Rotation in place:
Example: [1,2,3,4,5,6,7,8,9]
output: [4,5,6,7,8,9,1,2,3]
Steps:
1) Divide array into sets & rotate each element in set k postitions to left
2) outer loop: number of sets, inner loop: rotate elements of a set k postions to the left
number of sets?? (Use GCD to calculate the number of sets)
References:
https://www.youtube.com/watch?v=utE_1ppU5DY
GCD of 1 or more numbers:
-> GCD between two: GCD(a,b)
-> GCD between 3: GCD(a,GCD(b,c))
gcd(int a,int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
References:
https://www.geeksforgeeks.org/gcd-two-array-numbers/
LCD between a and b = (a/GCD) * b;
-> GCD between two: GCD(a,b)
-> GCD between 3: GCD(a,GCD(b,c))
gcd(int a,int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
References:
https://www.geeksforgeeks.org/gcd-two-array-numbers/
LCD between a and b = (a/GCD) * b;