SlideShare a Scribd company logo
Leetcode Study
Session 2
Resources
Neetcode: https://www.youtube.com/@NeetCode
Codepath: https://www.codepath.org/courses
ColorStack: https://www.colorstack.org/
STJ Developer Challenge : Registration Link
Leetcode Patterns: https://seanprashad.com/leetcode-patterns/
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.
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.
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
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.
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.
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.
Match
1. Linear scan / search
2. Tracking two largest values
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
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.
Implementation 1 - Java
Implementation 1 - Python
Implementation 2 - Java
Implementation 2 - Python
2D Array
Array of Arrays.
2-D array usually represented with rows and columns
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.
2-D Array Creation
marks = new int[][] // incorrect;
marks= new int[][4] // incorrect;
marks = new int[3][] // correct;
2-D array in form of rows and columns
marks=new int[3][4];
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}
}
How we can get array length
marks.length // tell us rows.
marks[0].length // tell us columns.
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.
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]
Match
This is a row-column swapping problem
Pattern: Iterate through each row and column and swap the
indices.
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]
Implement - Java
Implement - Python
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.
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.
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]).
→
Match
2D 1D array mapping
→
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
Implement
Java
Extra Problems
1. Check if N and Its Double Exists
2. Plus One
3. Diagonal Traverse
4. Spiral Matrix
5. Rotate Array
6. Set Matrix Zeros

More Related Content

Similar to Leetcode Session 2 - 2d array in java learn (20)

DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
PDF
programs on arrays.pdf
sowmya koneru
 
PPT
Chapter 10.ppt
MithuBose3
 
PDF
Computer java programs
ADITYA BHARTI
 
PPT
Algorithms with-java-advanced-1.0
BG Java EE Course
 
PPT
Array Presentation
Deep Prajapati Microplacer
 
PDF
Amcat automata questions
ESWARANM92
 
PDF
Amcat automata questions
ESWARANM92
 
PPTX
TCS Coding questions of all the required section
itsabhishekiesian
 
PDF
450 dsa (1).pdf
karansharma193921
 
PPT
Java™ (OOP) - Chapter 7: "Multidimensional Arrays"
Gouda Mando
 
PDF
2D Array
Swarup Boro
 
PPTX
Java-Programming.forBSITSTUDENTfreespptx
oliverrobertjames
 
PDF
Striver SDE Sheet (1).pdf
ManuManu692871
 
PPTX
Building Java Programas
ssuser4df5ef
 
PDF
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
PPTX
Algorithm Assignment Help
Programming Homework Help
 
PPT
Array 31.8.2020 updated
vrgokila
 
PDF
pyton Notes9
Amba Research
 
PPTX
lecture 2.2.2 2D array.pptx IUGLFHLHFJFY
gargh4711
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
programs on arrays.pdf
sowmya koneru
 
Chapter 10.ppt
MithuBose3
 
Computer java programs
ADITYA BHARTI
 
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Array Presentation
Deep Prajapati Microplacer
 
Amcat automata questions
ESWARANM92
 
Amcat automata questions
ESWARANM92
 
TCS Coding questions of all the required section
itsabhishekiesian
 
450 dsa (1).pdf
karansharma193921
 
Java™ (OOP) - Chapter 7: "Multidimensional Arrays"
Gouda Mando
 
2D Array
Swarup Boro
 
Java-Programming.forBSITSTUDENTfreespptx
oliverrobertjames
 
Striver SDE Sheet (1).pdf
ManuManu692871
 
Building Java Programas
ssuser4df5ef
 
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Algorithm Assignment Help
Programming Homework Help
 
Array 31.8.2020 updated
vrgokila
 
pyton Notes9
Amba Research
 
lecture 2.2.2 2D array.pptx IUGLFHLHFJFY
gargh4711
 

Recently uploaded (20)

PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Ad

Leetcode Session 2 - 2d array in java learn

  • 2. Resources Neetcode: https://www.youtube.com/@NeetCode Codepath: https://www.codepath.org/courses ColorStack: https://www.colorstack.org/ STJ Developer Challenge : Registration Link Leetcode Patterns: https://seanprashad.com/leetcode-patterns/
  • 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.
  • 9. Match 1. Linear scan / search 2. Tracking two largest values
  • 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]). →
  • 31. Match 2D 1D array mapping →
  • 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
  • 34. Extra Problems 1. Check if N and Its Double Exists 2. Plus One 3. Diagonal Traverse 4. Spiral Matrix 5. Rotate Array 6. Set Matrix Zeros