geekgeekmoregeek
BAN USERRefer to:
Programming Interviews Exposed/Chapter 4/List Flattening
How about this?
public class largestSubArray
{
private static int[] arr = {7, -7, 3, 4, 1, -1, 0, 0, 0, 0};
private static int N = 7;
public static boolean findN(int[]dp, int sum, int start, int end)
{
if ( start > end )
return false;
if ( dp[start] == end )
return false;
if ( sum == N )
{
System.out.print("The largest contiguous sub-array is starting from index " + start + ", ");
System.out.println("ending at index " + end);
return true;
}
dp[start] = end;
return findN(dp, sum-arr[start], start+1, end) || findN(dp, sum-arr[end], start, end-1);
}
public static void main(String[] args)
{
int[] dp = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int start = 0;
int end = arr.length-1;
int sum = 0;
for ( int i = start; i <= end; i++ )
sum += arr[i];
boolean result = findN(dp, sum, start, end);
if ( result == false )
System.out.println("Sorry, N is not reachable by the elements");
}
}
Refer to article "Detecting kernel memory leaks" [LWN.net]
- geekgeekmoregeek September 07, 2012it may help