Longest Subarray with Sum K (Mixed)
Two Pointers & Sliding Window DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Finding the length of the longest subarray with sum equal to k in a given array, Arrays, Subarray, Time Complexity, Space Complexity, Big O Notation, Loops, Hashing, Map, Prefix Sum, sliding window, brute force, subarray, prefix sum, hash map, time complexity analysis, Sliding Window Technique.
Given an array of integers and an integer k, find the length of the longest subarray whose elements sum up to k. Input Format: N = size of the array k = target sum array[] = elements of the array Output Format: Length of the longest subarray with sum k Examples: Example 1: Input Format: N = 3, k = 5, array[] = {2,3,5} Result: 2 Explanation: The longest subarray with sum 5 is {2, 3}. And its length is 2. Example 2: Input Format: N = 3, k = 1, array[] = { 1, 1, 1} Result: 3 Explanation: The longest subarray with 1 is { 1, 1, 1}. And its length is 3.