array
array
if (arr[i] != arr[j]) {
i++;
arr[i] = arr[j];
always (d=d%n) [no of rotation must be less than size of array if rotate
the array equal to its size then we get same array]
Q.3.1) similar concept for right rotate also?
reverse(a,a+n-1-d);
reverse(a+n-d,a+n);
reverse(a,a+n);
Brute-force: Count non-zero and zero and put non-zero up-front and
then use zero
Optimal: first find the first 0 where it occur then Use two - pointer
Q.5) Union of two sorted array?
Brute-force: Use set two store both array and store in array to return
Optimal: Use two-pointer one at first array and second at next array
and take in union array which is smaller and move pointer and at last
the array which is remaining store in union , use unionArr.back()
(consider the case if unionArr.size()==0) for previous array element
comparison and run while loop till (i<n1 and j<n2) .
Optimal: Use 2 pointer approach if sum ==k find (j-i) and update length
if greater and sum exceeds k then trim from left i.e, subtract ith
element move i to i+1
Q.12) Majority element we have integer find the array which has more
than n/2 appearance ?
Brute force: Use two loop to find the count of each element and one
which has count greater than n/2 is our answer
Better: We use hash map to store array element along with its count
the one which has count greater than n/2 is our answer
Optimal: Moore voting Algorithm
1.Apply moore voting algo
2. verify whether this element appears more than n/2 times or not
Take two variable cnt and majority_element when cnt is 0 change
majority element and when equal increase cnt otherwise decrease cnt
Q.14) Rearrange element by sign one positive and one negative array
contains equal no of + and - element ?
Brute force: Use 2 array to store positive and negative element and
then put in + element at even index and – element at odd index.
Optimal: We create answer array and check if element is + put in even
available index and if – put it odd available index
top++;
// For moving top to bottom.
for (int i = top; i <= bottom; i++)
ans.push_back(mat[i][right]);
right--;
// For moving right to left.
if (top <= bottom) {
for (int i = right; i >= left; i--)
ans.push_back(mat[bottom][i]);
bottom--;
}
// For moving bottom to top.
if (left <= right) {
for (int i = bottom; i >= top; i--)
ans.push_back(mat[i][left]);
left++;}} return ans;}
Type 3: Find pascal triangle given n we run for loop for every row and
compute value for every row like ans*(row-col)/col)
Brute force: Run 3 loop to generate all product and find maximum
one.
{1st loop: 0->n, 2nd loop: i->n, 3rd loop: i->j }
Better: We can remove 3 loop and do this using two loop only
for(int i=0;i<n;i++){
o pro=1;
for(int j=i;j<n;j++){
pro=pro*arr[j];
ans=max(ans,pro);}
Optimal: Do product from front and back find maximum product and
before doing product check if front product is 0 make it 1 and also if
back product is 0 make it 1.
First update front product and back product in answer then in next
iteration check if product is 0 make it 1.