Prepin
Log in

Identify the longest sub-array that has the specified sum.

Approach / Explanation

Let's analyze the question. You are provided with an array of integers, which includes both positive and negative values, along with a target number (the given sum). The goal is to identify the longest sub-array whose sum precisely equals the specified sum. A sub-array refers to a consecutive segment of an array. Important considerations while formulating your response: Are you working with an array that has both positive and negative integers? Is your task to locate a sub-array or any combination of numbers that totals the given sum? Can you confirm that there cannot be more than one valid sub-array? What should be done if no sub-array exists that sums up to the given value? Grasping these aspects will help you provide an accurate and efficient solution.

Suggested Answer

A potential approach to address this issue is by utilizing Hashing. Below is a Python implementation: Let’s assume that arr represents our array of integers and targetSum denotes the specified sum. def maxLengthSubArray ( arr , targetSum ) : # create an empty dictionary hashMap = dict ( ) # to handle the case when the sub-array with given sum starts from index 0 hashMap [ 0 ] = - 1 # initialize the sum to 0 and maxLen to 0 sumOfElem , maxLen , end_index = 0 , 0 , - 1 # traverse the given array for i in range ( len ( arr ) ) : sumOfElem += arr [ i ] if sumOfElem not in hashMap : hashMap [ sumOfElem ] = i if ( sumOfElem - targetSum ) in hashMap and maxLen < i - hashMap [ sumOfElem - targetSum ] : maxLen = i - hashMap [ sumOfElem - targetSum ] end_index = i if end_index != - 1 : return arr [ end_index - maxLen + 1 : end_index + 1 ] else : return [ ] print ( maxLengthSubArray ( [ 10 , 5 , 2 , 7 , 2 , 5 , 6 , 8 ] , 14 ) ) In this solution, we employ a dictionary or hashmap to track the cumulative sum, sumOfElem , at each point along with its corresponding index, i . The length of the subarray is updated whenever we encounter a sum of elements that matches the targetSum .

Alternative Answer

Here is another Java example that employs a method similar to the one described above. import java . util . * ; class Main { static void maxLengthSubArray ( int arr [ ] , int targetSum ) { Map < Integer , Integer > hashMap = new HashMap < > ( ) ; int sum = 0 ; int length = 0 ; int ending_index = - 1 ; int start_index = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; if ( sum == targetSum ) { length = i + 1 ; ending_index = i ; } if ( hashMap . containsKey ( sum - targetSum ) ) { if ( length < i - hashMap . get ( sum - targetSum ) ) { length = i - hashMap . get ( sum - targetSum ) ; ending_index = i ; } } hashMap . putIfAbsent ( sum , i ) ; } System . out . println ( "Subarray [" + ( ending_index - length + 1 ) + ".." + ending_index + "]" ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 2 , 7 , 1 , 9 } ; int sum = 15 ; maxLengthSubArray ( arr , sum ) ; } } This Java solution functions similarly to the Python example by creating a hashmap and verifying whether a sum that equals the targetSum has been identified. The key distinction is that this implementation returns the indices of the sub-array rather than the sub-array itself.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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