Arrays
Arrays
Sixty questions on array manipulation, ordered from easy fundamentals to hard, interview-caliber problems — the foundation every other pattern builds on.
1-bit and 2-bit Characters
easyWe have two special characters: Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
Add to Array-Form of Integer
easyThe array-form of an integer num is an array representing its digits in left to right order.
Adjacent Increasing Subarrays Detection I
easyGiven an array nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing.
Alternating Groups I
easyThere is a circle of red and blue tiles.
Ant on the Boundary
easyAn ant is on a boundary.
Apple Redistribution into Boxes
easyYou are given an array apple of size n and an array capacity of size m.
Apply Operations to an Array
easyYou are given a 0-indexed array nums of size n consisting of non-negative integers.
Array Partition
easyGiven an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized.
Assign Cookies
easyAssume you are an awesome parent and want to give your children some cookies.
Available Captures for Rook
easyYou are given an 8 x 8 matrix representing a chessboard.
Average Salary Excluding the Minimum and Maximum Salary
easyYou are given an array of unique integers salary where salary[i] is the salary of the ith employee.
Average Value of Even Numbers That Are Divisible by Three
easyGiven an integer array nums of positive integers, return the average value of all even integers that are divisible by 3.
Best Poker Hand
easyYou are given an integer array ranks and a character array suits.
Best Time to Buy and Sell Stock
easyYou are given an array prices where prices[i] is the price of a given stock on the ith day.
Binary Prefix Divisible By 5
easyYou are given a binary array nums (0-indexed).
Binary Search
easyGiven an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.
Build Array from Permutation
easyGiven a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.
Button with Longest Push Time
easyYou are given a 2D array events which represents a sequence of events where a child pushes a series of buttons on a keyboard.
Buy Two Chocolates
easyYou are given an integer array prices representing the prices of various chocolates in a store.
Calculate Amount Paid in Taxes
easyYou are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the ith tax bracket has an upper bound of upperi and is taxed at a rate of...
Can Make Arithmetic Progression From Sequence
easyA sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Can Place Flowers
easyYou have a long flowerbed in which some of the plots are planted, and some are not.
Cells with Odd Values in a Matrix
easyThere is an m x n matrix that is initialized to all 0's.
Check Array Formation Through Concatenation
easyYou are given an array of distinct integers arr and an array of integer arrays pieces, where the integers in pieces are distinct.
Check Distances Between Same Letters
easyYou are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice.
Check if a String Is an Acronym of Words
easyGiven an array of strings words and a string s, determine if s is an acronym of words.
Check If All 1's Are at Least Length K Places Away
easyGiven an binary array nums and an integer k, return true if all 1's are at least k places away from each other, otherwise return false.
Check if All the Integers in a Range Are Covered
easyYou are given a 2D integer array ranges and two integers left and right.
Check if Any Element Has Prime Frequency
easyYou are given an integer array nums.
Check if Array is Good
easyYou are given an integer array nums.
Check if Array Is Sorted and Rotated
easyGiven an array nums, return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero).
Check if Bitwise OR Has Trailing Zeros
easyYou are given an array of positive integers nums.
Check if Every Row and Column Contains All Numbers
easyAn n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).
Check if Grid Satisfies Conditions
easyYou are given a 2D matrix grid of size m x n.
Check If It Is a Straight Line
easyYou are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point.
Check if Matrix Is X-Matrix
easyA square matrix is said to be an X-Matrix if both of the following conditions hold: Given a 2D integer array grid of size n x n representing a square matrix, return true if grid...
Check If N and Its Double Exist
easyGiven an array arr of integers, check if there exist two indices i and j such that :
Check If String Is a Prefix of Array
easyGiven a string s and an array of strings words, determine whether s is a prefix string of words.
Check If Two String Arrays are Equivalent
easyGiven two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
Concatenation of Array
easyGiven an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).
Construct the Minimum Bitwise Array I
easyYou are given an array nums consisting of n prime integers.
Contains Duplicate
easyGiven an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Contains Duplicate II
easyGiven an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
Convert 1D Array Into 2D Array
easyYou are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n.
Count Common Words With One Occurrence
easyGiven two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.
Count Distinct Numbers on Board
easyYou are given a positive integer n, that is initially placed on a board.
Count Elements With Maximum Frequency
easyYou are given an array nums consisting of positive integers.
Count Elements With Strictly Smaller and Greater Elements
easyGiven an integer array nums, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums.
Count Good Triplets
easyGiven an array of integers arr, and three integers a, b and c.
Count Hills and Valleys in an Array
easyYou are given a 0-indexed integer array nums.
Count Items Matching a Rule
easyYou are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item.
Count Negative Numbers in a Sorted Matrix
easyGiven a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.
Count Number of Pairs With Absolute Difference K
easyGiven an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
Count Pairs Of Similar Strings
easyYou are given a 0-indexed string array words.
Count Pairs That Form a Complete Day I
easyGiven an integer array hours representing times in hours, return an integer denoting the number of pairs i, j where i < j and hours[i] + hours[j] forms a complete day.
Count Partitions with Even Sum Difference
easyYou are given an integer array nums of length n.
Count Prefix and Suffix Pairs I
easyYou are given a 0-indexed string array words.
Count Prefixes of a Given String
easyYou are given a string array words and a string s, where words[i] and s comprise only of lowercase English letters.
Count Special Quadruplets
easyGiven a 0-indexed integer array nums, return the number of distinct quadruplets (a, b, c, d) such that:
Count Subarrays of Length Three With a Condition
easyGiven an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.