Triples

Given a sequence of positive integers. You need to find the number of triples in that sequence. For this problem, (x, y, z) constructs a triple if and only if x + y = z. So, (1, 2, 3) is a triple, where (3, 4, 5) is not.

Input
Each input set starts with a positive integer N. Next few lines contain N positive integers. Input is terminated by EOF.

Output
For each case, print the number of triples in a line.

Constraints
3 ≤ N ≤ 5000

Sample Input
6

1 2 3 4 5 6

6

1 2 4 8 16 32

3

100000000 200000000 100000000

5

1 1 1 2 2

Output for Sample Input
6

0

1

6

Source Code code format="java5" public class Triples { public static void main(String[] args) { int[] n = {1, 2, 3, 4, 5, 6}; System.out.println(getNumOfTriple(n));

n = new int[]{1, 2, 4, 8, 16, 32}; System.out.println(getNumOfTriple(n));

n = new int[]{100000000, 200000000, 100000000}; System.out.println(getNumOfTriple(n));

n = new int[]{1, 1, 1, 2, 2}; System.out.println(getNumOfTriple(n)); }

public static int getNumOfTriple(int[] n) { int count = 0; for(int i = 0; i < n.length; i++) { for(int j = i + 1; j < n.length; j++) { int t = n[i] + n[j]; for(int k = 0; k < n.length; k++) { if(n[k] == t) { count++; }               }            }        }        return count; } } code

Comment