Skip to content

Instantly share code, notes, and snippets.

@Visgean
Created May 10, 2017 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Visgean/194518425de30ea47fc1d8aed7965433 to your computer and use it in GitHub Desktop.
Save Visgean/194518425de30ea47fc1d8aed7965433 to your computer and use it in GitHub Desktop.
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Beads {
public static int rotated_index(int position, int length) {
return position % length;
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
for (String num : args) {
nums.add(Integer.parseInt(num));
}
System.out.println(
findMax(allSums(nums))
);
}
public static Set<Integer> sums(ArrayList<Integer> beads, int n) {
Set<Integer> solutions = new HashSet<Integer>();
if (n < 1 || beads.size() == 0) {
return solutions;
}
for (int i = 0; i < beads.size(); i++) {
ArrayList<Integer> single_solution = new ArrayList<Integer>();
for (int j = 0; j < n; j++) {
single_solution.add(
beads.get(rotated_index(i + j , beads.size()))
);
}
Integer sum = 0;
for (Integer integer : single_solution) {
sum += integer;
}
solutions.add(sum);
}
return solutions;
}
public static Set<Integer> allSums (ArrayList<Integer> beads) {
Set<Integer> solutions = new HashSet<Integer>();
if (beads.size() == 0) {
return solutions;
}
for (int n = 0; n < beads.size() + 1; n++) {
for (Integer solution : sums(beads, n)) {
solutions.add(solution);
}
}
return solutions;
}
public static int findMax (Set<Integer> nums) {
Integer max = 0;
for (Integer n: nums) {
if (n > max) {
max = n;
}
}
if (nums.size()==0) {
return 0;
}
for (int i = 1; i <= max; i++) {
if (!nums.contains(i)) {
return i-1;
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment