Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created March 4, 2015 20:43
Show Gist options
  • Save dmnugent80/7e4050f328f65b6e2ad8 to your computer and use it in GitHub Desktop.
Save dmnugent80/7e4050f328f65b6e2ad8 to your computer and use it in GitHub Desktop.
Given a sequence of positive integers A and an integer T, return whether there is a continuous sequence of A that sums up to exactly T
/*
Question: Given a sequence of positive integers A and an integer T, return whether there is a continuous sequence of A that sums up to exactly T
*/
public class SequenceSum
{
public static void main(String[] args)
{
int[] arr = {23, 5, 4, 7, 2, 11};
boolean bool = findSum(arr, 9);
System.out.print(bool);
}
public static boolean findSum (int[] A ,int T){
int sum = 0 ;
int j = 0;
for (int i = 0 ; i < A.length ; ++i) {
while (j < A.length && sum < T) {
sum += A[j] ;
j++;
}
if (sum == T) {
return true ;
}
sum -= A[i] ;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment