Ad

Quipu

Quipu is an ancient recording device from Andean South America. It was likely used as an accounting record.

Quipu article in Wikipedia

We chose to represent each knot as a number, and the positions as the position in an array, being the rightmost item the furthest knot in the rope.

It also had an error prevention method, being the first number the sum of the rest.

Return the number if the sum checks out, if not return -1.

import java.util.ArrayList;
public class Quipu {

	public static int calculate(ArrayList<int[]> strings){
        if (strings==null||strings.isEmpty())return -1;
        int primero=0;
        int suma=0;
        for (int i = 0; i < strings.size(); i++) {
            StringBuilder sb1 = new StringBuilder();
            for (int num : strings.get(i)) {
                    sb1.append(num);
                }
            if (i == 0) {
                primero=Integer.parseInt(sb1.toString());
            }else{
                suma+=Integer.parseInt(sb1.toString());  
            }
        }
        if(primero==suma)
        return primero;
        else return -1;
    }
}