Design a data structure that can efficiently store and check if the sum of any three successfully added elements is equal to a given sum.
Given code:
public class Sum3 {
/**
* Adds/appends list of integers at the end of internal list.
*/
public void addLast(int[] list) {
throw new UnsupportedOperationException("Waiting to be implemented.");
}
/**
* Returns boolean representing if any three consecutive integers in the
* internal list have given sum.
*/
public boolean containsSum3(int sum) {
throw new UnsupportedOperationException("Waiting to be implemented.");
}
public static void main(String[] args) {
Sum3 s = new Sum3();
s.addLast(new int[] { 1, 2, 3 });
System.out.println(s.containsSum3(6));
System.out.println(s.containsSum3(9));
s.addLast(new int[] { 4 });
System.out.println(s.containsSum3(9));
}
}
Aucun commentaire:
Enregistrer un commentaire