Advertisement

Continuous Subarray Sum - LeetCode 523 Solution

Continuous Subarray Sum - Complete Solution Guide

Continuous Subarray Sum is LeetCode problem 523, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise . A good subarray is a subarray where: its length is at least two , and the sum of the elements of the subarray is a multiple of k . Note that: A subarray is a contiguous part of the array. An integer x is a multiple of k if there exists an integer n such that x = n * k . 0 is always a multiple of k . Example 1: Input: nums = [23, 2,4 ,6,7], k = 6 Output: true Explanation: [2, 4] is a continu

Detailed Explanation

The problem asks us to determine if a given array of integers contains a contiguous subarray of length at least 2 whose sum is a multiple of a given integer 'k'. A multiple of 'k' means the sum can be expressed as n * k, where n is an integer. We need to return `true` if such a subarray exists, and `false` otherwise. Crucially, a subarray must be contiguous (elements are next to each other) and must have a length of at least 2.

Solution Approach

The solution utilizes a hash map (dictionary) to store the remainders of prefix sums modulo 'k', along with their corresponding indices. We iterate through the array, calculating the running sum and its remainder when divided by 'k'. If we encounter a remainder we've seen before, we check if the distance between the current index and the index where we first saw the remainder is at least 2. If so, we've found a valid subarray. If we haven't seen the remainder before, we store it in the hash map along with its index.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash map `remainder_map` to store remainders as keys and their first seen indices as values. Initialize the map with {0: -1} to handle the case where a subarray starting from the beginning is a multiple of k.
  2. Step 2: Initialize a variable `running_sum` to 0. This variable will store the cumulative sum of the array up to the current index.
  3. Step 3: Iterate through the array `nums` from left to right, with index `i` and value `num`.
  4. Step 4: Update `running_sum` by adding the current element `num` to it: `running_sum += num`.
  5. Step 5: Calculate the remainder of `running_sum` when divided by `k`: `remainder = running_sum % k`.
  6. Step 6: Check if the `remainder` exists as a key in `remainder_map`.
  7. Step 7: If the `remainder` exists in `remainder_map`, check if the difference between the current index `i` and the index stored in the map for that remainder, `remainder_map[remainder]`, is greater than or equal to 2. If it is, return `true` because we have found a valid subarray.
  8. Step 8: If the `remainder` does not exist in `remainder_map`, add it to the map with its current index `i`: `remainder_map[remainder] = i`.
  9. Step 9: After iterating through the entire array, if no valid subarray has been found, return `false`.

Key Insights

  • Insight 1: Using prefix sums allows efficient calculation of the sum of any subarray. Instead of repeatedly summing the subarray, we can subtract prefix sums.
  • Insight 2: If two prefix sums have the same remainder when divided by 'k', the subarray between their indices has a sum that is a multiple of 'k'. This is because (sum2 - sum1) % k == 0 if sum1 % k == sum2 % k.
  • Insight 3: We need to track the *first* occurrence of each remainder. If we encounter the same remainder again later, we check the length of the subarray between the first and current occurrences. If the length is >= 2, we have found a good subarray.
  • Insight 4: Initialize the hash map with {0: -1}. This handles the edge case where the prefix sum itself is a multiple of k from the beginning of the array. If the sum % k == 0 and the current index is i, then i - (-1) >= 2 will correctly identify a valid subarray.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(min(n, k))

Topics

This problem involves: Array, Hash Table, Math, Prefix Sum.

Companies

Asked at: Yandex.