0% found this document useful (0 votes)
36 views2 pages

Arrays

The document outlines various array-based problems along with their descriptions and test cases. Key problems include finding two numbers that sum to a target, calculating the maximum subarray sum, merging sorted arrays, moving zeros to the end, and finding duplicates. Each problem is accompanied by specific input-output examples to illustrate the expected results.

Uploaded by

Devabn Nirmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Arrays

The document outlines various array-based problems along with their descriptions and test cases. Key problems include finding two numbers that sum to a target, calculating the maximum subarray sum, merging sorted arrays, moving zeros to the end, and finding duplicates. Each problem is accompanied by specific input-output examples to illustrate the expected results.

Uploaded by

Devabn Nirmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array-Based Problems

1. Two Sum

Description: Find two numbers in an array that add up to a specific target value.

Test Cases: - Input: nums = [2, 7, 11, 15], target = 9 → Output: [0, 1]
- Input: nums = [3, 2, 4], target = 6 → Output: [1, 2]
- Input: nums = [3, 3], target = 6 → Output: [0, 1]

2. Kadane’s Algorithm (Maximum Subarray Sum)

Description: Find the maximum sum of a contiguous subarray.

Test Cases: - Input: [-2,1,-3,4,-1,2,1,-5,4] → Output: 6


- Input: [5,4,-1,7,8] → Output: 23
- Input: [-3,-1,-2] -> Output: -1

3. Merge Two Sorted Arrays

Description: Merge two sorted arrays into one sorted array.

Test Cases: - Input: [1,2,3], [2,5,6] → Output: [1,2,2,3,5,6]


- Input: [], [1, 2] → Output: [1, 2]
- Input: [4,5,6],[1,2,3] → Output: [1,2,3,4,5,6]

4. Move Zeroes to End


Move all zeros in an array to the end while maintaining the order of
Description:
non-zero elements.

Test Cases: - Input: [0, 1, 0, 3, 12] → Output: [1, 3, 12, 0, 0]


- Input: [0, 0, 0] → Output: [0, 0, 0]
- Input: [1,2, 3] → Output: [1, 2, 3]

5. Find Duplicate Number

Description: Find the single duplicated number in an array of integers.

Test Cases: - Input: [1, 3, 4, 2, 2] → Output: 2


- Input: [3, 1, 3, 4, 2] → Output: 3
- Input: [1, 1] → Output: 1

1
6. Rotate Array

Description: Rotate the array to the right by k steps.

Test Cases: - Input: [1,2,3,4,5,6], k = 2 → Output: [5,6,1,2,3,4]


- Input: [1], k = 0 → Output: [1]
- Input: [1,2], k = 3 → Output: [2,1]

7. Find Missing Number


Description: Find the missing number from an array containing numbers from 0 to
n.

Test Cases: - Input: [0, 1, 3] → Output: 2


- Input: [3, 0, 1] → Output: 2
- Input: [1] → Output: 0

8. Trapping Rain Water

Description: Given an elevation map, compute how much water it is able to trap
after raining.

Test Cases: - Input: [0,1,0,2,1,0,1,3,2,1,2,1] → Output: 6


- Input: [4,2,0,3,2,5] → Output: 9
- Input: [3,3,3] → Output: 0

9. Next Greater Element


Description: For each element in an array, find the next greater element to its right.

Test Cases: - Input: nums1 = [4,1,2], nums2 = [1,3,4,2] → Output: [-1, 3, -1]
- Input: nums1 = [2,4], nums2= [1,2,3,4] → Output: [3,-1]

10.Subarray With Given Sum

Description: Find a subarray with a given sum in an unsorted array.

Test Cases: - Input: [1, 2, 3, 7, 5], sum = 12 → Output: (2, 4)


- Input: [1, 2, 3, 4, 5], sum = 9 → Output: (2,4)
- Input: [1, 2, 3], sum = 7 → Output: -1 (not found)

You might also like