algos
algos copied to clipboard
Longest Subarray with Sum K [Java]
To Implement the algorithm to find the length of the longest subarray with sum equal to a given value k. The array may contain both positive and negative numbers.
Problem Statement Given an array of integers and a number k, return the length of the longest subarray whose sum equals k.
Examples
Input: N = 3, k = 5, array[] = {2, 3, 5}
Output: 2
Explanation: The longest subarray with sum 5 is {2, 3}. Its length is 2.
Input: N = 3, k = 1, array[] = {-1, 1, 1}
Output: 3
Explanation: The longest subarray with sum 1 is {-1, 1, 1}. Its length is 3.
@raj-j-shah can you please assign this issue to me under Hacktoberfest 2025