Wednesday, February 20, 2019

Rotate Array by K times && Quick Left Rotation, GCD of two or more numbers, LCD

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

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


   Time Complexity: O(N) where N = length of array and K = numer of rotations

   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)

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;


Tuesday, February 19, 2019

Different Permutations of a String

Different Permutations of a String:

Coming soon...

Lexicographycally ordered??
coming soon..
Permutations in String:
Given two strings s1 and s2, , function to return true, if S2, containts the permutations of S1.
Example 1:
Input:s1 = "abc" s2 = "eidcbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("cba").
Leet Code: https://leetcode.com/problems/permutation-in-string/
GeeksForGeeks Similar problem:
https://www.geeksforgeeks.org/check-if-two-strings-are-permutation-of-each-other/
Hints:
1) Obviously, brute force will result in TLE. Think of something else.
2) how will you check one string is permutation of another??(sort both strings and check if all of the both strings match??)
3) If one string is a permutation of another string then they must one common metric. What is that? (Character frequencies)
4) Both strings must have same character frequencies, if one is permutation of another. Which data structure should be used to store frequencies?(int[26])
5) What about hash table? An array of size 26?