Longest Subarray with Sum K (Positives)
Two Pointers & Sliding Window DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Finding the Length of the Longest Subarray with Sum Equal to k, Arrays, Subarray, Loops, Time Complexity, Space Complexity, Big O Notation, Prefix Sum, Hashing, Map, Hash Tables, Two Pointers, Sliding Window, complexity analysis, brute force, subarray, hashing, prefix sum, two pointer technique, Subarrays, Prefix Sums, Hashing & Hash Maps, Two-Pointer Technique, Brute-Force Search.
Problem Statement Given an array A of N integers and an integer K, find the length of the longest subarray whose elements sum up to K. Input Specification N: An integer representing the size of the array. K: An integer representing the target sum. A: A list of N integers. Output Specification Return an integer representing the length of the longest subarray that sums to K. Examples Example 1: Input: N = 3, K = 5, A = [2, 3, 5] Output: 2 Explanation: The longest subarray with sum 5 is {2, 3}. Its length is 2. Example 2: Input: N = 5, K = 10, A = [2, 3, 5, 1, 9] Output: 3 Explanation: The longest subarray with sum 10 is {2, 3, 5}. Its length is 3.