Practice — Two Pointers (6 questions)
Pair Sum - Sorted
Given an array of integers nums that is sorted in non-decreasing
order, and an integer target, return the indices of the two numbers
such that they add up to target.
You may assume that each input has exactly one solution, and you may not use the same element twice. Return the two indices as a list, in increasing order.
Example 1
Input: nums = [1, 2, 4, 6, 10], target = 8
Output: [1, 3]
Explanation: nums[1] + nums[3] = 2 + 6 = 8
Example 2
Input: nums = [2, 3, 4], target = 6
Output: [0, 2]
Explanation: nums[0] + nums[2] = 2 + 4 = 6
Constraints
2 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9numsis sorted in non-decreasing order.- Exactly one valid answer exists.
Share this question
Triplet Sum
Given an integer array nums, return all the unique triplets
[nums[i], nums[j], nums[k]] such that i != j, i != k, j != k,
and nums[i] + nums[j] + nums[k] == 0.
The solution set must not contain duplicate triplets. The order of the triplets, and the order of numbers within each triplet, does not matter.
Example 1
Input: nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]
Explanation: These are the only two combinations of three numbers
that sum to 0. Note that [-1, 2, -1] is the same triplet as
[-1, -1, 2] and is not repeated in the output.
Example 2
Input: nums = [0, 1, 1]
Output: []
Explanation: No triplet sums to 0.
Constraints
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5
Share this question
Is Palindrome Valid
Given a string s, determine if it is a palindrome after converting
all uppercase letters to lowercase and removing all non-alphanumeric
characters (spaces, punctuation, etc.). An empty string (after
filtering) is considered a valid palindrome.
Solve it using O(1) extra space — do not build a cleaned copy of the string and compare it to its reverse; scan the original string in place with two pointers instead.
Example 1
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: After filtering and lowercasing: "amanaplanacanalpanama",
which reads the same forwards and backwards.
Example 2
Input: s = "race a car"
Output: false
Explanation: Filtered: "raceacar", which is not a palindrome.
Constraints
1 <= s.length <= 2 * 10^5sconsists of printable ASCII characters.
Share this question
Largest Container
You are given an integer array height of length n, where each
element represents the height of a vertical line drawn at that index
on the x-axis. Together with the x-axis, two of these lines form a
container.
Find two lines that, together with the x-axis, form a container that holds the most water, and return the maximum amount of water it can hold. The container's width is the distance between the two chosen indices, and its height is the shorter of the two lines (water can't rise above the shorter wall).
Example 1
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Explanation: The lines at index 1 (height 8) and index 8 (height 7)
form a container of width 7 and height min(8, 7) = 7, holding
7 * 7 = 49 units of water — the maximum possible.
Example 2
Input: height = [1, 1]
Output: 1
Constraints
2 <= height.length <= 10^50 <= height[i] <= 10^4
Share this question
Shift Zeros to the End
Given an integer array nums, move all 0s to the end of the array
while maintaining the relative order of the non-zero elements. You
must do this in place without making a copy of the array, and you
should minimize the total number of write operations.
Example 1
Input: nums = [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Example 2
Input: nums = [0, 0, 1]
Output: [1, 0, 0]
Constraints
1 <= nums.length <= 10^5-2^31 <= nums[i] <= 2^31 - 1
Share this question
Next Lexicographical Sequence
Given an array of integers nums representing a permutation, rearrange
it into the next lexicographically greater permutation of its
elements, in place and using only O(1) extra space.
If no such permutation exists (the array is already the highest possible permutation), rearrange it into the lowest possible order (i.e., sorted in ascending order).
Example 1
Input: nums = [1, 2, 3]
Output: [1, 3, 2]
Example 2
Input: nums = [3, 2, 1]
Output: [1, 2, 3]
Explanation: [3, 2, 1] is the highest permutation, so we wrap around
to the lowest one.
Example 3
Input: nums = [1, 1, 5]
Output: [1, 5, 1]
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 100
Share this question