Mastering Competitive Coding: Array
Published: 2024-05-11 18:27:27

**Easy level Questions :**


---

### Array Easy level Questions

#### 1. Rotate Array

   **Example**:
   - Input: nums = [1, 2, 3, 4, 5], k = 2
   - Output: [4, 5, 1, 2, 3]
   - Explanation: Rotating the array [1, 2, 3, 4, 5] by 2 steps to the right results in [4, 5, 1, 2, 3].

#### 2. Two Sum

   **Example**:
   - Input: nums = [2, 7, 11, 15], target = 9
   - Output: [0, 1]
   - Explanation: The elements at indices 0 and 1 (2 and 7) sum up to the target value 9.

#### 3. Merge Intervals

   **Example**:
   - Input: intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
   - Output: [[1, 6], [8, 10], [15, 18]]
   - Explanation: Merging overlapping intervals results in [[1, 6], [8, 10], [15, 18]].

#### 4. Product of Array Except Self

   **Example**:
   - Input: nums = [1, 2, 3, 4]
   - Output: [24, 12, 8, 6]
   - Explanation: The product of all elements except self at index i is calculated for each index i.

#### 5. Container With Most Water

   **Example**:
   - Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
   - Output: 49
   - Explanation: The maximum amount of water that can be trapped is between the bars at indices 1 and 8.

#### 6. Maximum Subarray

   **Example**:
   - Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
   - Output: 6
   - Explanation: The contiguous subarray with the largest sum is [4, -1, 2, 1], which sums up to 6.

#### 7. Next Permutation

   **Example**:
   - Input: nums = [1, 2, 3]
   - Output: [1, 3, 2]
   - Explanation: The next lexicographically greater permutation of [1, 2, 3] is [1, 3, 2].

#### 8. Search in Rotated Sorted Array

   **Example**:
   - Input: nums = [4, 5, 6, 7, 0, 1, 2], target = 0
   - Output: 4
   - Explanation: The target value 0 is found at index 4 after rotation.

#### 9. Kth Largest Element in an Array

   **Example**:
   - Input: nums = [3, 2, 1, 5, 6, 4], k = 2
   - Output: 5
   - Explanation: The second largest element in the array is 5.

#### 10. Spiral Matrix

   **Example**:
   - Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
   - Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
   - Explanation: Traversing the matrix in a spiral order results in the given output.


#### 11. Sliding Window Maximum
   **Input**: nums = [1,3,-1,-3,5,3,6,7], k = 3
   **Output**: [3,3,5,5,6,7]
   **Explanation**: The maximum value in each subarray of size 3 is [3,3,5,5,6,7].

#### 12. Longest Substring Without Repeating Characters
   **Input**: s = "abcabcbb"
   **Output**: 3
   **Explanation**: The longest substring without repeating characters is "abc", which has a length of 3.

#### 13. Three Sum
   **Input**: nums = [-1,0,1,2,-1,-4]
   **Output**: [[-1,-1,2],[-1,0,1]]
   **Explanation**: The unique triplets that sum up to zero are [-1,-1,2] and [-1,0,1].

#### 14. Meeting Rooms II
   **Input**: intervals = [[0, 30],[5, 10],[15, 20]]
   **Output**: 2
   **Explanation**: Minimum two conference rooms are required: one for [0, 30] and another for [5, 10], [15, 20].

#### 15. Valid Parentheses
   **Input**: s = "()[]{}"
   **Output**: true
   **Explanation**: The input string is valid as all parentheses are closed in the correct order.

#### 16. Move Zeroes
   **Input**: nums = [0,1,0,3,12]
   **Output**: [1,3,12,0,0]
   **Explanation**: All zeroes are moved to the end of the array while maintaining the relative order of non-zero elements.

#### 17. Majority Element
   **Input**: nums = [2,2,1,1,1,2,2]
   **Output**: 2
   **Explanation**: The majority element that appears more than ⌊ n/2 ⌋ times is 2.

#### 18. Reverse Words in a String
   **Input**: s = "the sky is blue"
   **Output**: "blue is sky the"
   **Explanation**: The words in the string are reversed.

#### 19. Trapping Rain Water
   **Input**: height = [0,1,0,2,1,0,1,3,2,1,2,1]
   **Output**: 6
   **Explanation**: The total trapped rainwater between the bars is 6 units.

#### 20. Remove Duplicates from Sorted Array
   **Input**: nums = [0,0,1,1,1,2,2,3,3,4]
   **Output**: 5
   **Explanation**: After removing duplicates, the new length of the array is 5, and the first five elements are [0,1,2,3,4].