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

easy
We 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

easy
The array-form of an integer num is an array representing its digits in left to right order.

Adjacent Increasing Subarrays Detection I

easy
Given 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

easy
There is a circle of red and blue tiles.

Ant on the Boundary

easy
An ant is on a boundary.

Apple Redistribution into Boxes

easy
You are given an array apple of size n and an array capacity of size m.

Apply Operations to an Array

easy
You are given a 0-indexed array nums of size n consisting of non-negative integers.

Array Partition

easy
Given 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

easy
Assume you are an awesome parent and want to give your children some cookies.

Available Captures for Rook

easy
You are given an 8 x 8 matrix representing a chessboard.

Average Salary Excluding the Minimum and Maximum Salary

easy
You 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

easy
Given an integer array nums of positive integers, return the average value of all even integers that are divisible by 3.

Best Poker Hand

easy
You are given an integer array ranks and a character array suits.

Best Time to Buy and Sell Stock

easy
You are given an array prices where prices[i] is the price of a given stock on the ith day.

Binary Prefix Divisible By 5

easy
You are given a binary array nums (0-indexed).

Binary Search

easy
Given 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

easy
Given 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

easy
You 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

easy
You are given an integer array prices representing the prices of various chocolates in a store.

Calculate Amount Paid in Taxes

easy
You 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

easy
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Can Place Flowers

easy
You have a long flowerbed in which some of the plots are planted, and some are not.

Cells with Odd Values in a Matrix

easy
There is an m x n matrix that is initialized to all 0's.

Check Array Formation Through Concatenation

easy
You 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

easy
You 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

easy
Given 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

easy
Given 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

easy
You are given a 2D integer array ranges and two integers left and right.

Check if Any Element Has Prime Frequency

easy
You are given an integer array nums.

Check if Array is Good

easy
You are given an integer array nums.

Check if Array Is Sorted and Rotated

easy
Given 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

easy
You are given an array of positive integers nums.

Check if Every Row and Column Contains All Numbers

easy
An 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

easy
You are given a 2D matrix grid of size m x n.

Check If It Is a Straight Line

easy
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point.

Check if Matrix Is X-Matrix

easy
A 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

easy
Given an array arr of integers, check if there exist two indices i and j such that :

Check If String Is a Prefix of Array

easy
Given 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

easy
Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

Concatenation of Array

easy
Given 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

easy
You are given an array nums consisting of n prime integers.

Contains Duplicate

easy
Given 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

easy
Given 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

easy
You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n.

Count Common Words With One Occurrence

easy
Given 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

easy
You are given a positive integer n, that is initially placed on a board.

Count Elements With Maximum Frequency

easy
You are given an array nums consisting of positive integers.

Count Elements With Strictly Smaller and Greater Elements

easy
Given 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

easy
Given an array of integers arr, and three integers a, b and c.

Count Hills and Valleys in an Array

easy
You are given a 0-indexed integer array nums.

Count Items Matching a Rule

easy
You 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

easy
Given 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

easy
Given 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

easy
You are given a 0-indexed string array words.

Count Pairs That Form a Complete Day I

easy
Given 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

easy
You are given an integer array nums of length n.

Count Prefix and Suffix Pairs I

easy
You are given a 0-indexed string array words.

Count Prefixes of a Given String

easy
You are given a string array words and a string s, where words[i] and s comprise only of lowercase English letters.

Count Special Quadruplets

easy
Given 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

easy
Given 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.