3. Warm up problem: Largest Number At Least Twice of Others
You are given an integer array nums where the largest integer is unique.
Determine whether the largest element in the array is at least twice as
much as every other number in the array. If it is, return the index of the
largest element, or return -1 otherwise.
4. Largest Number At Least Twice of Others
Example 1:
Input: nums = [3,6,1,0]
Output: 1
Explanation: 6 is the largest integer. For every other number in the
array x, 6 is at least twice as big as x. The index of value 6 is 1, so
we return 1.
5. Example 2:
Input: nums = [1,2,3,4]
Output: -1
Explanation: 4 is less than twice the value of 3, so we return -1.
Largest Number At Least Twice of Others
6. Understand
What is the input?
An integer array nums
What is the output?
If the largest element is at least twice as large as every other number,
return its index.
Otherwise, return -1.
7. Understand
Example Walkthrough 1:
[3, 6, 1, 0]
The largest number is 6.
6 is at least twice as large (2 times) as 3, 1, and 0. The index of 6 is
1, so we return 1 as the output.
8. Understand
Example walkthrough 2:
[1, 2, 3, 4]
The largest number is 4. 4 is not at least twice as large as 3. Twice
of 3 is 6 and 4 is not greater than that. The condition fails so the
output is -1.
10. Plan
Approach 1: Two Pass Solution
1. Find the maximum element and its index on the first iteration
of the array.
2. Check if this max value is at least twice as large as every other
number on the second iteration of the array.
3. If it satisfies the condition, return its index; otherwise, return -1
11. Plan
Approach 2: One Pass Solution
1. Using a single loop, track the largest number (max1) and its
index and the second largest number (max2).
2. After scanning / searching through the array, check if max >= 2 *
max 2.
3. If this is true return max1’s index else return -1.
16. 2D Array
Array of Arrays.
2-D array usually represented with rows and columns
17. 2-D Array Declaration
We can declare 2-D array in following ways
type[][] variable;
or
type variable[][];
For example:
int marks[][];
Each element in the 2-D Array must be the same type.
18. 2-D Array Creation
marks = new int[][] // incorrect;
marks= new int[][4] // incorrect;
marks = new int[3][] // correct;
19. 2-D array in form of rows and columns
marks=new int[3][4];
20. We can also initialize the array at the
time of array creation
int[][] marks = new int{
{1,2,0,5},
{4,5,1,4},
{7,6,5,9}
}
21. How we can get array length
marks.length // tell us rows.
marks[0].length // tell us columns.
22. Problem: Transpose Matrix
Given a 2Dinteger array matrix, return the transpose of matrix.
The transpose of a matrix is the matrix flipped over its main diagonal,
switching the matrix's row and column indices.
23. Understand
What does “transpose” mean?
•Flipping the matrix over its main diagonal (top-left to bottom-
right).
•In other words: switch row and column indices.
•The rows of the original matrix become the columns of the new
one.
•transposeMatrix[i][j] = matrix[j][i]
24. Match
This is a row-column swapping problem
Pattern: Iterate through each row and column and swap the
indices.
25. Plan
The approach for this problem is simple and straight-forward
1. Get the dimensions of the original matrix ( rows (m) and cols (n)).
2. Create a new matrix of size n * m (dimensions are flipped).
3. Iterate / loop over each cell in the original matrix.
4. For each cell (matrix[i][j]), assign its value to result[j][i]
28. Problem 2: Shift 2D Grid
Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
In one shift operation:
Element at grid[i][j] moves to grid[i][j + 1].
Element at grid[i][n - 1] moves to grid[i + 1][0].
Element at grid[m - 1][n - 1] moves to grid[0][0].
Return the 2D grid after applying shift operation k times.
29. Understand
1. We need to apply a right-shift operation to a 2D grid k times.
2. Each shift moves all elements one position to the right.
3. Elements at the last column wrap around to the next row or top
of the grid.
30. Understand
What happens in a single shift? (Simple Case)
1.grid[i][j] grid[i][j+1]
→
2.Last column in a row moves to the first column of the
→ next row.
3.Bottom-right element wraps to top-left (grid[0][0]).
→
32. Plan
1. Flatten the 2D grid to a 1D array of size m (row) * n (col).
2. Shift the 1D list by k steps.
3. Use modulo [k % (m * n)] to avoid extra rotations.
4. Convert the 1D array back into a 2D m x n array