Given an array containing N integers and an integer K., Your task is to find the length of the longest Sub-Array with the sum of the elements equal to the given value K. 
 
Example 1:
 
Input :
A[] = {10, 5, 2, 7, 1, 9}
K = 15
Output : 4
Explanation:
The sub-array is {5, 2, 7, 1}.
Example 2:
Input : 
A[] = {-1, 2, 3}
K = 6
Output : 0
Explanation: 
There is no such sub-array with sum 6.
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function lenOfLongSubarr() that takes an array (A), sizeOfArray (n),  sum (K)and returns the required length of the longest Sub-Array. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
 
Constraints:
1<=N<=10^{5}
-10^{5}<=A[i], K<=10^{5}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Amortized analysis

Starter code: #User function Template for python3



class Solution:

    def lenOfLongSubarr (self, arr, n, k) : 

        #Complete the function

    



Solutions:
class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		maxLength = 0
		currentSum = 0
		sumMap = {}
		for i in range(len(arr)):
			currentSum += arr[i]
			if currentSum == k:
				maxLength = max(maxLength, i + 1)
			if currentSum - k in sumMap:
				maxLength = max(maxLength, i - sumMap[currentSum - k])
			if currentSum not in sumMap:
				sumMap[currentSum] = i
		return maxLength


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hashmap = {}
		cursum = 0
		maxlen = 0
		for i in range(n):
			cursum += arr[i]
			if cursum == k:
				maxlen = i + 1
			if cursum - k in hashmap:
				maxlen = max(maxlen, i - hashmap[cursum - k])
			if cursum not in hashmap:
				hashmap[cursum] = i
		return maxlen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		presummap = {}
		sum = 0
		maxlen = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxlen = max(maxlen, i + 1)
			rem = sum - k
			if rem in presummap:
				length = i - presummap[rem]
				maxlen = max(maxlen, length)
			if sum not in presummap:
				presummap[sum] = i
		return maxlen


class Solution:

	def lenOfLongSubarr(self, arr, n, k) -> int:
		prefixMap = {0: -1}
		sumSoFar = 0
		maxWindowSize = 0
		for i in range(len(arr)):
			sumSoFar += arr[i]
			prefixSum = sumSoFar - k
			if prefixSum in prefixMap:
				left = prefixMap[prefixSum]
				right = i
				windowSize = right - left
				maxWindowSize = max(maxWindowSize, windowSize)
			if sumSoFar not in prefixMap:
				prefixMap[sumSoFar] = i
		return maxWindowSize


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		d = {}
		sum = 0
		maxLen = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxLen = i + 1
			elif sum - k in d:
				maxLen = max(maxLen, i - d[sum - k])
			if sum not in d:
				d[sum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		d = {}
		s = 0
		mx = 0
		l = 0
		rem = 0
		for i in range(len(arr)):
			s += arr[i]
			if s == k:
				mx = max(mx, i + 1)
			rem = s - k
			if rem in d:
				l = i - d[rem]
				mx = max(mx, l)
			if s not in d:
				d[s] = i
		return mx


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		mx = 0
		s = 0
		i = 0
		di = dict()
		for j in range(n):
			s += arr[j]
			if s not in di:
				di[s] = j
			if s == k:
				mx = max(mx, j + 1)
			elif s - k in di:
				mx = max(mx, j - di[s - k])
		return mx


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		d = {}
		s = 0
		ans = 0
		for i in range(n):
			s += arr[i]
			if s == k:
				ans = max(ans, i + 1)
			rem = s - k
			if rem in d:
				ans = max(ans, i - d[rem])
			if s not in d:
				d[s] = i
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		a = 0
		s = 0
		d = {0: -1}
		for i in range(n):
			s = s + arr[i]
			if s - k in d:
				if i - d[s - k] > a:
					a = i - d[s - k]
			if s not in d:
				d[s] = i
		return a


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		map = {}
		sum = 0
		maxiLen = 0
		for i in range(len(arr)):
			sum += arr[i]
			if sum == k:
				maxiLen = max(maxiLen, i + 1)
			rem = sum - k
			if rem in map:
				length = i - map[rem]
				maxiLen = max(maxiLen, length)
			if sum not in map:
				map[sum] = i
		return maxiLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		prefix_sum = {}
		prefix_sum[0] = -1
		max_len = 0
		curr_sum = 0
		for i in range(len(arr)):
			curr_sum += arr[i]
			if curr_sum - k in prefix_sum:
				max_len = max(max_len, i - prefix_sum[curr_sum - k])
			if curr_sum not in prefix_sum:
				prefix_sum[curr_sum] = i
		return max_len


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		for i in range(1, n):
			arr[i] += arr[i - 1]
		dict_ = {}
		ans = 0
		for i in range(n):
			if arr[i] not in dict_:
				dict_[arr[i]] = [i]
			if arr[i] - k in dict_:
				ans = max(ans, i - dict_[arr[i] - k][0])
			if arr[i] == k:
				ans = i + 1
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum = 0
		max_len = 0
		map = dict()
		for i in range(n):
			sum += arr[i]
			if sum == k:
				max_len = max(max_len, i + 1)
			rem = sum - k
			if rem in map:
				len = i - map[rem]
				max_len = max(max_len, len)
			if sum not in map:
				map[sum] = i
		return max_len


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		dic = {0: [-1]}
		count = 0
		for i in range(n):
			count += arr[i]
			if count in dic:
				dic[count].append(i)
			else:
				dic[count] = [i]
		ans = 0
		for num in dic:
			if num - k in dic:
				ans = max(ans, max(dic[num]) - min(dic[num - k]))
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		mp = {}
		sum = 0
		ans = 0
		mp[0] = -1
		for i in range(n):
			sum += arr[i]
			if sum not in mp:
				mp[sum] = i
			toFind = sum - k
			if toFind in mp:
				ans = max(ans, i - mp[toFind])
		return ans


class Solution:

	def lenOfLongSubarr(self, a, n, k):
		n = len(a)
		preSumMap = {}
		Sum = 0
		maxLen = 0
		for i in range(n):
			Sum += a[i]
			if Sum == k:
				maxLen = max(maxLen, i + 1)
			rem = Sum - k
			if rem in preSumMap:
				length = i - preSumMap[rem]
				maxLen = max(maxLen, length)
			if Sum not in preSumMap:
				preSumMap[Sum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hashMap = {}
		res = 0
		sum = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				res = i + 1
			remain = sum - k
			if remain in hashMap:
				res = max(res, i - hashMap[remain])
			if sum not in hashMap:
				hashMap[sum] = i
		return res
		res = 0
		for i in range(n):
			sum = 0
			for j in range(i, n):
				sum += arr[j]
				if sum == k:
					res = max(res, j - i + 1)
		return res


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		max_len = 0
		prefix_sum = {}
		sum = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				max_len = max(max_len, i + 1)
			else:
				j = prefix_sum.get(sum - k, i)
				max_len = max(max_len, i - j)
			if prefix_sum.get(sum, True) == True:
				prefix_sum[sum] = i
		return max_len


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		max = 0
		sum_arr = [0] * n
		sum_arr[0] = arr[0]
		index_dict = {}
		index_dict[sum_arr[0]] = 0
		for i in range(1, n):
			sum_arr[i] = sum_arr[i - 1] + arr[i]
			if index_dict.get(sum_arr[i], True) == True:
				index_dict[sum_arr[i]] = i
		for i in range(n):
			diff = sum_arr[i] - k
			if diff == 0:
				result = i + 1
			else:
				j = index_dict.get(diff, -1)
				if j == -1 or j >= i:
					continue
				else:
					result = i - j
			if max < result:
				max = result
		return max


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		m = {0: -1}
		leng = 0
		summ = 0
		for i in range(n):
			summ += arr[i]
			if summ not in m:
				m[summ] = i
			if summ - k in m:
				leng = max(leng, i - m[summ - k])
		return leng


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		map = {}
		ll = 0
		sum = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				l = i + 1
				if l > ll:
					ll = l
			else:
				more = sum - k
				if more in map:
					l = i - map[more]
					if l > ll:
						ll = l
			if sum not in map:
				map[sum] = i
		return ll


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum_indices = {0: -1}
		s = 0
		max_len = 0
		for i in range(n):
			s += arr[i]
			if s - k in sum_indices:
				max_len = max(max_len, i - sum_indices[s - k])
			if s not in sum_indices:
				sum_indices[s] = i
		return max_len


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum_hash = {}
		sum = 0
		maxlen = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxlen = max(maxlen, i + 1)
			temp = sum - k
			if temp in sum_hash:
				maxlen = max(maxlen, i - sum_hash[temp])
			if sum not in sum_hash:
				sum_hash[sum] = i
		return maxlen


import math

class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		(curr_sum, res) = (0, 0)
		pref_sum = {0: -1}
		for i in range(len(arr)):
			curr_sum += arr[i]
			if curr_sum - k in pref_sum:
				res = max(res, i - pref_sum[curr_sum - k])
			if curr_sum not in pref_sum:
				pref_sum[curr_sum] = i
		return res


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		cursum = 0
		reslen = 0
		dic = {}
		for i in range(n):
			cursum += arr[i]
			if cursum == k:
				reslen = max(reslen, i + 1)
			diff = cursum - k
			if diff in dic:
				reslen = max(reslen, i - dic[cursum - k])
			if cursum not in dic:
				dic[cursum] = i
		return reslen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum = 0
		window = 0
		di = dict()
		for i in range(n):
			sum = sum + arr[i]
			if sum not in di:
				di[sum] = i
			if sum == k:
				window = max(window, i + 1)
			elif sum - k in di:
				window = max(window, i - di[sum - k])
		return window


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum_ = 0
		length = 0
		dict_ = {0: -1}
		for (i, number) in enumerate(arr):
			sum_ += number
			index = dict_.get(sum_ - k, -2)
			if index != -2:
				length = max(length, i - index)
			if sum_ not in dict_:
				dict_[sum_] = i
		return length


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		(mx, current_mx) = (0, 0)
		d = {}
		for index in range(n):
			current_mx += arr[index]
			if current_mx == k:
				mx = max(mx, index - 0 + 1)
			if current_mx - k in d:
				mx = max(mx, index - d[current_mx - k])
			if current_mx not in d:
				d[current_mx] = index
		return mx


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hm = {0: -1}
		maxLen = 0
		prefSum = 0
		for (i, num) in enumerate(arr):
			prefSum += num
			if prefSum - k in hm:
				currLen = i - hm[prefSum - k]
				maxLen = max(maxLen, currLen)
			if prefSum not in hm:
				hm[prefSum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		preSumMap = {}
		sum = 0
		maxLen = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxLen = max(maxLen, i + 1)
			rem = sum - k
			if rem in preSumMap:
				length = i - preSumMap[rem]
				maxLen = max(maxLen, length)
			if sum not in preSumMap:
				preSumMap[sum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		objmap = {}
		maxlen = 0
		l = n
		sum = 0
		for i in range(l):
			sum = sum + arr[i]
			if sum not in objmap:
				objmap[sum] = i
			if sum == k:
				maxlen = i + 1
			rem = sum - k
			if rem in objmap:
				prev = objmap[rem]
				n = i - prev
				if n > maxlen:
					maxlen = n
		return maxlen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		seen = {}
		maxLen = 0
		sum = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxLen = i + 1
			target = sum - k
			if target in seen:
				maxLen = max(maxLen, i - seen[target])
			if sum not in seen:
				seen[sum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		dic = {}
		sums = 0
		longsub = 0
		for i in range(n):
			sums += arr[i]
			r = sums - k
			if r == 0:
				longsub = i + 1
			if r in dic:
				longsub = max(longsub, i - dic[r])
			if sums not in dic:
				dic[sums] = i
		return longsub


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		dicti = dict()
		maxi = 0
		ans = 0
		for i in range(n):
			maxi = maxi + arr[i]
			if maxi == k:
				ans = max(ans, i + 1)
				if maxi not in dicti:
					dicti[maxi] = i
				continue
			rem = maxi - k
			if rem in dicti:
				temp = i - dicti[rem]
				ans = max(temp, ans)
			if maxi not in dicti:
				dicti[maxi] = i
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		presum_dict = {}
		max_len = 0
		pre_sum = 0
		for i in range(n):
			pre_sum += arr[i]
			if pre_sum == k:
				max_len = max(max_len, i + 1)
			if pre_sum - k in presum_dict:
				max_len = max(max_len, i - presum_dict[pre_sum - k])
			if pre_sum not in presum_dict:
				presum_dict[pre_sum] = i
		return max_len


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		presum = {}
		s = 0
		presum[s] = -1
		for i in range(len(arr)):
			s += arr[i]
			if s not in presum:
				presum[s] = i
		maxlen = 0
		s = 0
		for i in range(len(arr)):
			s += arr[i]
			if s - k in presum and presum[s - k] <= i:
				maxlen = max(maxlen, i - presum[s - k])
		return maxlen


from collections import defaultdict

class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		map = defaultdict(lambda : None)
		maxlen = 0
		prefixs = 0
		for i in range(len(arr)):
			prefixs += arr[i]
			if prefixs == k:
				maxlen = max(maxlen, i + 1)
			if map[prefixs - k] != None:
				maxlen = max(maxlen, i - map[prefixs - k])
			if map[prefixs] == None:
				map[prefixs] = i
		return maxlen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		maxi = 0
		sum_ = 0
		d = {}
		for i in range(n):
			sum_ += arr[i]
			if sum_ == k:
				maxi = max(maxi, i + 1)
			if sum_ - k in d:
				maxi = max(maxi, i - d[sum_ - k])
			if sum_ not in d:
				d[sum_] = i
		return maxi


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		mp = {}
		maxLen = 0
		Sum = 0
		for i in range(n):
			Sum += arr[i]
			if Sum == k:
				maxLen = max(maxLen, i + 1)
			x = Sum - k
			if x in mp:
				maxLen = max(i - mp[x], maxLen)
			if Sum not in mp:
				mp[Sum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		cumul_sum = 0
		sum_map = {}
		answer = 0
		for (index, num) in enumerate(arr):
			cumul_sum += num
			if cumul_sum == k:
				answer = max(answer, index + 1)
			if cumul_sum - k in sum_map:
				answer = max(answer, index + 1 - sum_map[cumul_sum - k])
			if cumul_sum in sum_map:
				sum_map[cumul_sum] = min(sum_map[cumul_sum], index + 1)
			else:
				sum_map[cumul_sum] = index + 1
		return answer


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		(c, lon) = (0, 0)
		dic = {}
		for i in range(n):
			c += arr[i]
			if c == k:
				lon = max(i + 1, lon)
			if c - k in dic:
				lon = max(i + 1 - dic[c - k], lon)
			if c not in dic:
				dic[c] = i + 1
		return lon


class Solution(object):

	def lenOfLongSubarr(self, arr, n, k):
		prefixMap = {}
		currSum = maxWindow = 0
		for index in range(n):
			currSum += arr[index]
			if currSum == k:
				maxWindow = max(maxWindow, index + 1)
			remainingValue = currSum - k
			if remainingValue in prefixMap:
				maxWindow = max(maxWindow, index - prefixMap[remainingValue])
			if currSum not in prefixMap:
				prefixMap[currSum] = index
		return maxWindow


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hash = {}
		cur = 0
		ans = 0
		for (i, el) in enumerate(arr):
			cur += el
			if cur == k:
				ans = i + 1
			if cur - k in hash:
				ans = max(ans, i - hash[cur - k])
			if cur not in hash:
				hash[cur] = i
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		pref = dict()
		(ans, res) = (0, 0)
		pref[0] = -1
		for i in range(n):
			res += arr[i]
			if res - k in pref.keys():
				ans = max(ans, i - pref[res - k])
			if res not in pref.keys():
				pref[res] = i
		return ans


from collections import defaultdict

class Solution:

	def lenOfLongSubarr(self, nums, n, k):
		sum_ = 0
		mp = {0: -1}
		ans = 0
		for i in range(n):
			sum_ += arr[i]
			if sum_ - k in mp:
				ans = max(ans, i - mp[sum_ - k])
			if sum_ not in mp:
				mp[sum_] = i
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		ans = 0
		cur = 0
		dic = {0: 0}
		for i in range(n):
			cur += arr[i]
			if cur - k == 0:
				ans = i + 1
			elif cur - k in dic:
				ans = max(ans, i + 1 - dic[cur - k])
			if cur not in dic:
				dic[cur] = i + 1
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		if n == 1:
			return 1 if k == arr[0] else 0
		sm = 0
		ans = 0
		mp = dict()
		for i in range(n):
			sm += arr[i]
			if sm == k:
				ans = i + 1
			if sm - k in mp:
				correction = 0
				ans = max(ans, i - mp[sm - k] + correction)
			if sm not in mp:
				mp[sm] = i
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		h = {0: -1}
		l = 0
		s = 0
		for (idx, i) in enumerate(arr):
			s += i
			if s - k in h:
				l = max(l, idx - h[s - k])
			if s not in h:
				h[s] = idx
		return l


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hashmap = {}
		hashmap[0] = -1
		ps = 0
		longest = 0
		for i in range(len(arr)):
			ps += arr[i]
			if ps - k in hashmap:
				length = i - hashmap[ps - k]
				longest = max(longest, length)
			if ps not in hashmap:
				hashmap[ps] = i
		return longest


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		prefix_sum = [0] * n
		prefix_sum[0] = arr[0]
		for i in range(1, n):
			prefix_sum[i] = prefix_sum[i - 1] + arr[i]
		hash_map = {}
		max_length = 0
		for i in range(n):
			if prefix_sum[i] == k:
				max_length = i + 1
			elif prefix_sum[i] - k in hash_map:
				max_length = max(max_length, i - hash_map[prefix_sum[i] - k])
			if prefix_sum[i] not in hash_map:
				hash_map[prefix_sum[i]] = i
		return max_length


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum = 0
		res = 0
		dict1 = {}
		for i in range(n):
			sum = sum + arr[i]
			if sum == k:
				res = i + 1
			if sum not in dict1:
				dict1[sum] = i
			if sum - k in dict1:
				res = max(res, i - dict1[sum - k])
		return res


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		di = {}
		maxlen = 0
		s = 0
		for i in range(n):
			s += arr[i]
			if s == k:
				maxlen = max(maxlen, i + 1)
			rem = s - k
			if rem in di:
				lenn = i - di[rem]
				maxlen = max(maxlen, lenn)
			if s not in di:
				di[s] = i
		return maxlen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		prefixsum = {}
		valsum = 0
		maxlen = 0
		for i in range(n):
			valsum += arr[i]
			if valsum == k:
				maxlen = max(maxlen, i + 1)
			else:
				remaining = valsum - k
				if remaining in prefixsum.keys():
					maxlen = max(maxlen, i - prefixsum[remaining])
			if valsum not in prefixsum.keys():
				prefixsum[valsum] = i
		return maxlen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		mp = {}
		pfs = [0]
		for i in range(n):
			pfs.append(pfs[-1] + arr[i])
		ans = 0
		for i in range(n + 1):
			if pfs[i] - k in mp:
				ans = max(ans, i - mp[pfs[i] - k])
			if pfs[i] not in mp:
				mp[pfs[i]] = i
		return ans


from collections import defaultdict

class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		um = defaultdict(lambda : 0)
		sum = 0
		maxLen = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxLen = i + 1
			if sum not in um:
				um[sum] = i
			if sum - k in um:
				if maxLen < i - um[sum - k]:
					maxLen = i - um[sum - k]
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		maxi = 0
		sm = 0
		hash = {0: 0}
		for i in range(n):
			sm += arr[i]
			if sm == k:
				maxi = max(maxi, i + 1)
			if sm - k in hash:
				maxi = max(i - hash[sm - k], maxi)
			if sm not in hash:
				hash[sm] = i
		return maxi


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		dic = dict()
		largest = 0
		summ = 0
		for i in range(n):
			summ += arr[i]
			if summ == k:
				largest = max(largest, i + 1)
			more = summ - k
			if more in dic:
				largest = max(largest, i - dic[more])
			if summ not in dic:
				dic[summ] = i
		return largest


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		mydict = dict()
		sum = 0
		lenn = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				lenn = i + 1
			elif sum - k in mydict:
				lenn = max(lenn, i - mydict[sum - k])
			if sum not in mydict:
				mydict[sum] = i
		return lenn


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		dc = {}
		(sm, ans) = (0, 0)
		for i in range(n):
			sm += arr[i]
			if sm == k:
				ans = max(ans, i + 1)
			rem = sm - k
			if rem in dc:
				temp = i - dc[rem]
				ans = max(ans, temp)
			if sm not in dc:
				dc[sm] = i
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		maxi = sumi = 0
		hmap = {}
		for i in range(n):
			sumi += arr[i]
			if sumi == k:
				maxi = max(maxi, i + 1)
			rem = sumi - k
			if rem in hmap:
				length = i - hmap[rem]
				maxi = max(maxi, length)
			if sumi not in hmap:
				hmap[sumi] = i
		return maxi


class Solution:

	def lenOfLongSubarr(self, a, n, k):
		ans = 0
		total = 0
		di = {}
		for i in range(n):
			total += a[i]
			if total not in di:
				di[total] = i
			if total == k:
				ans = max(ans, i + 1)
			elif total - k in di:
				ans = max(ans, i - di[total - k])
		return ans


class Solution:

	def lenOfLongSubarr(self, arr, n, target):
		prefix_sum = 0
		prefix_map = {}
		res = 0
		for (index, element) in enumerate(arr):
			prefix_sum += element
			if prefix_sum == target:
				res = max(res, index + 1)
			if prefix_sum not in prefix_map:
				prefix_map[prefix_sum] = index
			if prefix_sum - target in prefix_map:
				res = max(res, index - prefix_map[prefix_sum - target])
		return res


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hmap = {}
		su = 0
		result = 0
		for i in range(n):
			su += arr[i]
			if su == k:
				result = i + 1
			if su - k in hmap:
				result = max(result, i - hmap[su - k])
			if su not in hmap:
				hmap[su] = i
		return result


class Solution:

	def lenOfLongSubarr(self, a, n, k):
		count = 0
		s = 0
		d = {}
		for i in range(n):
			s += a[i]
			if s == k:
				count = i + 1
			if s - k in d:
				l = i - d[s - k]
				count = max(l, count)
			if s not in d:
				d[s] = i
		return count


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		l = 0
		sumd = {}
		sum1 = 0
		for i in range(n):
			sum1 += arr[i]
			if sum1 not in sumd:
				sumd[sum1] = i
			if sum1 == k and l < i + 1:
				l = i + 1
			if sum1 - k in sumd:
				c = i - sumd[sum1 - k]
				if c > l:
					l = c
		return l


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		a = arr.copy()
		sum = 0
		d = {}
		maxi_len = 0
		for i in range(0, len(a)):
			sum = sum + a[i]
			if sum == k:
				maxi_len = max(maxi_len, i + 1)
			if sum - k in d:
				maxi_len = max(maxi_len, i - d[sum - k])
			if sum not in d:
				d[sum] = i
		return maxi_len


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		hmap = {}
		maxLen = 0
		sum = 0
		for i in range(len(arr)):
			sum += arr[i]
			if sum == k:
				maxLen = max(maxLen, i + 1)
			rem = sum - k
			if rem in hmap:
				maxLen = max(maxLen, i - hmap[rem])
			if sum not in hmap:
				hmap[sum] = i
		return maxLen


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		sum = 0
		maxi = 0
		d = {}
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxi = max(maxi, i + 1)
			rem = sum - k
			if rem in d:
				l = i - d[rem]
				maxi = max(maxi, l)
			if sum not in d:
				d[sum] = i
		return maxi


class Solution:

	def lenOfLongSubarr(self, arr, n, k):
		n = len(arr)
		sum = 0
		hashMap = {}
		result = 0
		for (idx, num) in enumerate(arr):
			sum += num
			if sum == k:
				result = max(result, idx + 1)
			rem = sum - k
			if rem in hashMap:
				result = max(result, idx - hashMap[rem])
			if sum not in hashMap:
				hashMap[sum] = idx
		return result