Created
March 4, 2015 20:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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