I was asked the following question in an interview and I couldn't give the optimal answer to this.
Problem: Write a program that can find the length of the largest contiguous sub-array that sums up to S. Given an array of variable size and an Integer.
Input: 1. An array of variable size, which can have only {-1, 0, 1} elements.
Example: A[] = {1, 0, 0, 1, -1, 1, 1, 1, 1}
- An integer S,
Example: S = 4
Output: 8
Explanation: Largest contiguous sub-array of A that adds up to S=4: {1, 0, 0, 1, -1, 1, 1, 1} or {0, 0, 1, -1, 1, 1, 1, 1}
Constraint: Should complete in O(N)
I have solved the problem, but couldn't meet the time complexity. Can anyone help with a solution that can solve this in O(N).
PS: No Copyright issues with the question that I have asked.