Problem Description

Given an integer array A, find if an integer p exists in the array such that the number of integers greater than p in the array equals to p.



Input Format
First and only argument is an integer array A.



Output Format
Return 1 if any such integer p is found else return -1.



Example Input
Input 1:

 A = [3, 2, 1, 3]
Input 2:

 A = [1, 1, 3, 3]


Example Output
Output 1:

 1
Output 2:

 -1



import java.util.ArrayList;
import java.util.Collections;

public class NextPermutation {

	public static void main(String[] args) {
		//A = [3, 2, 1, 3]
		ArrayList<Integer> a = new ArrayList<Integer>();
		a.add(3);
		a.add(2);
		a.add(1);
		a.add(3);
		System.out.println(solve(a));
		
	}

	public static int solve(ArrayList<Integer> A) {

		if (null == A || A.isEmpty())
			return 0;

		Collections.sort(A);

		int idx = 0;
		int n = A.size();

		while (idx < n) {
			int num = A.get(idx);
			while (idx < n && A.get(idx) == num) {
				idx++;
			}
			if (num == A.size() - idx) {
				return 1;
			}

		}

		return -1;

	}

}