Find maximum length sub-array having given sum

Given an array of integers, find the maximum length of the sub-array having the given sum.

Example Solution

Let array arr equal to [ 5, 6, -5, 5, 3, 5, 3, -2, 0 ] and integer sum equal to 8.

Sub-arrays with sum 8 are:

[ -5, 5, 3, 5 ]
[ 3, 5 ]
[ 5, 3 ]

The longest subarray is [ -5, 5, 3, 5 ], having the length 4. The solution should return the integer 4.

Contributed by Murat Sütunç