Problem:
Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)



import java.util.Arrays;
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		long[] a = new long[n];

		for (int i = 0; i < n; i++) {
			a[i] = Long.parseLong(sc.next());
		}

		long[] pow2 = new long[31];

		for (int i = 0; i < 31; i++) {
			pow2[i] = (long) Math.pow(2, i);
		}

		Arrays.sort(a);

		int ans = 0;
		boolean[] used = new boolean[n];

		for (int i = n - 1; i >= 0; i--) {
			if (used[i])
				continue;

			int left = 0;
			int right = i;
			long limit = pow2(a[i], pow2);

			for (int k = 0; k < 8; k++) {
				if (a[i] + a[(left + right) / 2] < limit) {
					left = (left + right) / 2;
				} else if (a[i] + a[(left + right) / 2] > limit) {
					right = (left + right) / 2 + 1;
				} else {
					left = (left + right) / 2;
					break;
				}
			}

			while (left > 0) {
				if (a[left - 1] < a[left])
					break;
				if (!used[left])
					break;
				left--;
			}
			while (right < i) {
				if (a[right] < a[right + 1])
					break;
				if (!used[right])
					break;
				right++;
			}

			for (int j = left; j < right; j++) {
				if (used[j])
					continue;

				if (judge(a[i] + a[j])) {
					ans++;
					used[j] = true;
					break;
				}
			}
		}

		System.out.println(ans);

	}

	static long pow2(long l, long[] pow) {
		for (int i = 0; i < 31; i++) {
			if (l < pow[i])
				return pow[i];
		}
		return -1;
	}

	static boolean judge(long sum) {
		long right = sum - 1;
		if ((sum & right) == 0)
			return true;
		else
			return false;

	}

}