Array Mastering in 5 Days
Array Mastering in 5 Days
Understanding Arrays
🔶 Learn the basics of arrays: indexing, access, and memory allocation.
🔶 Study the advantages and limitations of arrays.
Day 2
01
Day 3
Day 4
Sorting Algorithms
🔷 Study sorting algorithms applicable to arrays: bubble sort, selection
sort, insertion sort.
🔷 Analyze time complexity and compare these algorithms.
02
Day 5
Day 6
03
Day 7
Day 8
04
Day 9
Day 10
05
Important Practice Questions
01. Set Matrix Zeroes
Given an m x n integer matrix matrix, if an element is 0,
set its entire row and column to 0's.
Example 1:
Practice
06
02. Two Sum
Given an array of integers nums and an integer target,
return indices of the two numbers such that they add up
to target.
You may assume that each input would have exactly one
solution, and you may not use the same element twice.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Practice
07
03. Best Time to Buy and Sell Stock II
You are given an integer array prices where prices[i] is the
price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock.
You can only hold at most one share of the stock at any
time. However, you can buy it then immediately sell it on
the same day.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 7
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Practice
08
04. Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price
of a given stock on the ith day.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Practice
09
05. Sort Colors
Given an array nums with n objects colored red, white, or
blue, sort them in-place so that objects of the same color
are adjacent, with the colors in the order red, white, and
blue.
Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Example 2:
Input: nums = [2,0,1]
Output: [0,1,2]
Practice
10
06. Find All Duplicates in an Array
Given an integer array nums of length n where all the
integers of nums are in the range [1, n] and each integer
appears once or twice, return an array of all the integers
that appears twice.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]
Example 2:
Input: nums = [1,1,2]
Output: [1]
Example 3:
Input: nums = [1]
Output: []
Practice
11
07. 3Sum
Given an integer array nums, return all the triplets
[nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k,
and nums[i] + nums[j] + nums[k] == 0.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = [0,1,1]
Output: []
Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Practice
12
08. 4Sum
Given an array nums of n integers, return an array of all
the unique quadruplets [nums[a], nums[b], nums[c],
nums[d]] such that:
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
Practice
13
09. Search a 2D Matrix
You are given an m x n integer matrix matrix with the
following two properties:
Each row is sorted in non-decreasing order.
The first integer of each row is greater than the last
integer of the previous row.
Given an integer target, return true if target is in matrix or
false otherwise.
You must write a solution in O(log(m * n)) time complexity.
Example 1:
Practice
14
10. Longest Consecutive Sequence
Given an unsorted array of integers nums, return the
length of the longest consecutive elements sequence.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Practice
15
11. Find Peak Element
A peak element is an element that is strictly greater than
its neighbors.
Example 1:
Input: nums = [1,2,3,1]
Output: 2
Example 2:
Input: nums = [1,2,1,3,5,6,4]
Output: 5
Practice
16
12. Single Element in a Sorted Array
You are given a sorted array consisting of only integers
where every element appears exactly twice, except for
one element which appears exactly once.
Example 1:
Input: nums = [1,1,2,3,3,4,4,8,8]
Output: 2
Example 2:
Input: nums = [3,3,7,7,10,11,11]
Output: 10
Practice
17
13. Find Minimum in Rotated Sorted
Array
Suppose an array of length n sorted in ascending order is
rotated between 1 and n times. For example, the array
nums = [0,1,2,4,5,6,7] might become:
[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.
Example 1:
Input: nums = [3,4,5,1,2]
Output: 1
Practice
18
14. Search in Rotated Sorted Array II
There is an integer array nums sorted in non-decreasing
order (not necessarily with distinct values).
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Practice
19
15. Search in Rotated Sorted Array
There is an integer array nums sorted in ascending order
(with distinct values).
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Practice
20
16. Find First and Last Position of
Element in Sorted Array
Given an array of integers nums sorted in non-decreasing
order, find the starting and ending position of a given
target value.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Practice
21
17. Search a 2D Matrix II
Write an efficient algorithm that searches for a value
target in an m x n integer matrix matrix. This matrix has
the following properties:
Integers in each row are sorted in ascending from left
to right.
Integers in each column are sorted in ascending from top
to bottom.
Example 1:
Practice
22
18. Find a Peak Element II
A peak element in a 2D grid is an element that is strictly
greater than all of its adjacent neighbors to the left, right,
top, and bottom.
Practice
23
19. Search Insert Position
Given a sorted array of distinct integers and a target
value, return the index if the target is found.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Practice
24
20. Binary Search
Given an array of integers nums which is sorted in
ascending order, and an integer target, write a function to
search target in nums. If target exists, then return its
index. Otherwise, return -1.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Practice
25