Prepin
Log in

Suppose you have an integer array called nums and an integer k. Return true if there exists a subarray within nums that has a length of at least two and the sum of its elements is a multiple of k.

Approach / Explanation

This problem is designed to assess your understanding of array manipulation and modular arithmetic, particularly concerning subarray computations. To effectively analyze this question, we need to identify the key components: You have an integer array named nums . You are provided with a single integer, k . Your task is to return true if there exists a subarray within nums that meets the following criteria: The subarray must contain at least two elements. The sum of the elements in the subarray must be a multiple of k . This problem can typically be approached in several ways, including a brute force method where all possible subarrays are evaluated, or by using a map to track mods and indexes. However, the most efficient solution usually involves calculating prefix sums iteratively and monitoring the changes of these sums modulo k .

Suggested Answer

By utilizing the Python programming language, we can implement an efficient solution through the prefix sum method. This approach employs a Python dictionary to keep track of 'mod', and for each prefix sum, it checks if there is a prior 'mod' that corresponds to the current one, where 'mod' is determined by taking the prefix sum modulo k. def checkSubarraySum ( nums , k ) : sums = { 0 : - 1 } prefix_sum = 0 for i , num in enumerate ( nums ) : prefix_sum = ( prefix_sum + num ) % k if prefix_sum in sums : if i - sums [ prefix_sum ] > 1 : return True else : sums [ prefix_sum ] = i return False In this example, we traverse the array while calculating the cumulative prefix sum of the elements. We find the remainder of each prefix sum when divided by k and keep track of the index of the first occurrence of each remainder. If we see the same remainder again, it indicates the existence of a subarray whose sum is a multiple of k .

Alternative Answer

In Java, we can also implement an efficient solution using the prefix sum method. public boolean checkSubarraySum ( int [ ] nums , int k ) { Map < Integer , Integer > map = new HashMap < > ( ) { { put ( 0 , - 1 ) ; } } ; int runningSum = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) { runningSum += nums [ i ] ; if ( k != 0 ) runningSum %= k ; Integer prev = map . get ( runningSum ) ; if ( prev != null ) { if ( i - prev > 1 ) return true ; } else map . put ( runningSum , i ) ; } return false ; } In this Java implementation, we create a HashMap that correlates the remainders of the prefix sum (after dividing by k) with their respective indices. When a remainder is encountered again, it signifies that the numbers between the two occurrences of this remainder sum to a multiple of k. Throughout the process, we only update the HashMap if the remainder is absent, which ensures that the distance between remainders remains maximized and exceeds one.

Question Details

Difficulty & Category

Medium

Companies

Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.