Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Maximum Size Subarray Sum Equals k (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an array of integers nums and an integer k, find the length of the longest subarray that sums to k. If no such subarray exists, return 0.

Examples

Example 1:

  • Input: nums = [1, 2, 3, -2, 5], k = 5
  • Output: 2
  • Explanation: The longest subarray with a sum of 5 is [2, 3], which has a length of 2.

Example 2:

  • Input: nums = [-2, -1, 2, 1], k = 1
  • Output: 2
  • Explanation: The longest subarray with a sum of 1 is [-1, 2], which has a length of 2.

Example 3:

  • Input:

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible