Array-Related Coding Problems (Without Sorting)
1. Two Sum Problem
Problem: Given an array of integers, return indices of the two numbers such that they add up to a
specific target.
Example: Input: [2, 7, 11, 15], target: 9. Output: [0, 1].
2. Find the Missing Number
Problem: Given an array containing n distinct numbers from 0, 1, 2, ..., n, find the missing number.
Example: Input: [3, 0, 1]. Output: 2.
3. Subarray Sum Equals k
Problem: Given an array of integers and an integer k, find the total number of continuous subarrays
whose sum equals k.
Example: Input: nums = [1, 1, 1], k = 2. Output: 2.
4. Maximum Subarray
Problem: Find the contiguous subarray within an array (containing at least one number) which has
the largest sum.
Example: Input: [-2,1,-3,4,-1,2,1,-5,4]. Output: 6. (Explanation: [4,-1,2,1] has the largest sum = 6.)
5. Product of Array Except Self
Problem: Given an array nums of n integers where n > 1, return an array output such that output[i] is
equal to the product of all the elements of nums except nums[i].
Example: Input: [1, 2, 3, 4]. Output: [24, 12, 8, 6].
6. Maximum Product Subarray
Problem: Find the contiguous subarray within an array (containing at least one number) which has
the largest product.
Example: Input: [2, 3, -2, 4]. Output: 6. (Explanation: [2, 3] has the largest product.)
7. Find All Duplicates in an Array
Problem: Given an array of integers, some elements appear twice and others appear once. Find all
the elements that appear twice.
Example: Input: [4,3,2,7,8,2,3,1]. Output: [2, 3].
8. Find the Intersection of Two Arrays
Problem: Given two arrays, write a function to compute their intersection.
Example: Input: nums1 = [1,2,2,1], nums2 = [2,2]. Output: [2,2].
9. Longest Consecutive Sequence
Problem: Given an unsorted array of integers, find the length of the longest consecutive elements
sequence.
Example: Input: [100, 4, 200, 1, 3, 2]. Output: 4. (Explanation: The longest consecutive elements
sequence is [1, 2, 3, 4].)
10. Move Zeros
Problem: Given an array, write a function to move all 0's to the end while maintaining the relative
order of the non-zero elements.
Example: Input: [0,1,0,3,12]. Output: [1,3,12,0,0].
11. Find the Majority Element
Problem: Given an array of size n, find the majority element. The majority element is the element
that appears more than n/2 times.
Example: Input: [3, 2, 3]. Output: 3.
12. Maximize Distance to Closest Person
Problem: You are given an array representing a row of seats where 1 is a person sitting in a seat,
and 0 is an empty seat. Find the maximum distance to the closest person.
Example: Input: [1,0,0,0,1,0,1]. Output: 2.
13. Remove Duplicates from Sorted Array
Problem: Given a sorted array nums, remove the duplicates in-place such that each element
appears only once and return the new length.
Example: Input: [1,1,2]. Output: 2.
14. Contiguous Array (Equal 0s and 1s)
Problem: Given a binary array, find the maximum length of a contiguous subarray with an equal
number of 0 and 1.
Example: Input: [0,1,0]. Output: 2. (Explanation: [0, 1] is the longest subarray.)
15. Find the Maximum Sum of a Circular Subarray
Problem: Given a circular array, find the subarray that has the maximum sum.
Example: Input: [1,-2,3,-2]. Output: 3.