Arrays Flashcards

1
Q

For circular sorted array , that has been rotated , what is the formula for finding the number of rotations?

A

Number of rotations=Number of elements before minimum element or
index of the minimum element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Reversal algorithm for array(size n) rotation on left side(anti clock wise) by d

A
rotate(arr[], d, n)
  reverse(arr[], 0, d-1) ;
  reverse(arr[], d, n-1);
  reverse(arr[], 0, n);
why d-1 because array is zero based index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Reversal algorithm for array(size n) rotation right side(clock wise) by d

A
rotate(arr[], d, n)
  reverse(arr[], 0, n-1) ;
  reverse(arr[], 0, d-1);
  reverse(arr[], d, n-1);
why d-1 because array is zero based index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If we know that array is sorted but don’t know how it is sorted (ascending or descending) than how will we know?

A

Compare first element with last element.
If first element < last element sorted in ascending
else sorted in descending order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is transposing a matrix means ?

A
The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.
for (int r = 0; r < rows; r++)
            {
                // ! initializing r with one more as swapping across the diagonal will transpose the matrix
                // ! Not adding by one will give wrong result
                for (int c = r + 1; c < columns; c++)
                {
                    //!  row becomes column and column become row acorss the  diagonal 
                    int temp = matrix[r][c];
                    matrix[r][c] = matrix[c][r];
                    matrix[c][r] = temp;
                }
            }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to calculate the pivot index and pivot element on 2D array that is sorted row by row?

A

pivot index will be calculated same way as in one dimensional array
pivotIndex=lo+((hi-lo)/2);
For pivot element, we will use below formula
pivotElement = matrix[pivotIndex/ columns][pivotIndex% columns];
where columns are number of columns
Why pivotIndex/columns or pivotIndex%columns because we are visualizing 2D array as 1D array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to calculate pivot index for sorted array that has been rotated x number of times?

A
Pivot element will always be in the non-uniformly increasing part. We can do binary search to find out where in array we have non-uniformly increasing part.
private int FindPivotIndex(int[] nums)
        {
            int low = 0;
            int pivotIndex = 0;
            int high = nums.Length - 1;
            //array is sorted already 
            if (nums[low] < nums[high])
                return 0;
            while (low < high)
            {
                pivotIndex = low + ((high - low) / 2);
                //! pivot element is one having left element  and right element  less than it.
                //! Comparing only on the right side is enough
                if (nums[pivotIndex] > nums[pivotIndex + 1])
                {
                    return pivotIndex;
                }
                // !Pivot element always present in the non uniformly increasing part
                //! if first element is less than pivot index element than it means its strictly increasing 
                //! we need to search on  right side as its non uniformly increasing part
                else if (nums[low] < nums[pivotIndex])
                {
                    low = pivotIndex + 1;
                }
                else
                {
                    //! why pivotIndex and why not pivotIndex-1? 
                    //!because element at pivot index can also be the part of the non uniformly increasing 
                    high = pivotIndex;
                }
            }
            return pivotIndex;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to convert a 2-d array of size (m x n) into 1-d array and how to store the element at position [i, j] of 2-d array in 1-d array?

A
https://www.geeksforgeeks.org/emulating-a-2-d-array-using-1-d-array/
k =(i * total_no_of_columns_in_matrix)+j
where k is 1d array index
where i is the row index in 2D array
where j is the column index in 2D array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given 1-d array that maps to 2-d array. What is the formula for recalculation of 1D array indices into 2D array index ?

A

https://leetcode.com/problems/random-flip-matrix/

index/columns, index%columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
How to do round robin on dictionary with frequency as value and char as key sorted in descending by value and store  them to buckets ? 
For example
a:5
b:4
c:2
d:1
A

https://leetcode.com/problems/reorganize-string/
int i = 0;
foreach (KeyValuePair keyValue in frequencyCount)
{
for (int j = 0; j < keyValue.Value; ++j)
{
buckets[i % maxCount].Append(keyValue.Key);
++i;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given an array of integers of size ‘n’. How to apply sliding window technique to calculate the maximum sum possible for ‘k’ consecutive elements in the array?

A
https://medium.com/leetcode-patterns/leetcode-pattern-2-sliding-windows-for-strings-e19af105316b
int max_sum = 0, window_sum = 0; 
/* calculate sum of 1st window */
for (int i = 0; i < k; i++)  window_sum += arr[i]; 
/* slide window from start to end in array. */

for (int i = k; i < n; i++){
window_sum += arr[i] - arr[i-k]; // saving re-computation
max_sum = max(max_sum, window_sum);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to apply hare tortoise algorithm on array

A
https://leetcode.com/problems/find-the-duplicate-number/
 int tortoise=nums[0];
 int hare=nums[0];
do          
      {
         tortoise=nums[tortoise];
         hare=nums[nums[hare]];
  } while(tortoise!=hare);

  hare=nums[0];
      while(hare!=tortoise)
      {
          tortoise=nums[tortoise];
          hare=nums[hare];
      }
  return hare;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to know how many non-empty subarrays are there in an array?

A

https://www.youtube.com/watch?v=qoI26oy8MeI

N(N+1)/2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between subarray , subsequence and subset?

A

https://www.youtube.com/watch?v=qoI26oy8MeI
Subarray: contiguous part of an array
Subsequence: May not be contiguous but maintain the relative order
Subset: May not be contiguous and may not be in order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many non-empty subsequences in an array of size n?

A

https://www.youtube.com/watch?v=qoI26oy8MeI

2^n - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Get Neighbors in the grid.

A

private IEnumerable GetNeighbors(int[][] matrix, int row, int column)
{
foreach ((int x, int y) in new List() { (row + 1, column), (row-1, column)
(row , column+1), (row, column -1)
}
)
{
if ( x>=0 && x < matrix. Length &&
y>=0 && y < matrix[0].Length &&
)
yield return (x, y);
}
}

17
Q

Get 2d array dimensions

A

int rows=matrix.GetLength(0);

int cols=matrix.GetLength(1);

18
Q

Sliding the window from front and back of the array?

A

https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
k-i-1 –> to remove the element from window
n-i-1—> to add an element to the window