Skip to content

Longest Subarray with Sum K [Java] #885

@Syed0206

Description

@Syed0206

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions