PermCheck

Check whether array A is a permutation.


Solution:

Solution to Codility's Permutation Check problem which is from the Codility Lesson 4: Counting Elements and, is solved in Java 8 with 100% performance and correctness scores. The goal here is to check whether array a is a permutation. You can find the question of this PermCheck problem in the Codility website.


package Codility.Lesson4;

import java.util.*;

public class PermCheck {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int input[] = { 1, 3, 1, 1, 2, 3, 4, 7, 6, 5, 7, 8 };
		int result = solution(input);
		System.out.println(result);
	}

	public static int solution(int[] A) {
		// write your code in Java SE 8
		Arrays.sort(A);
		Set<Integer> set1 = new HashSet<Integer>();
		for (int a : A) {
			set1.add(a);
		}
		int sLen = set1.size();
		int iLen = A.length;
		if (sLen != iLen)
			return 0;

		if (A[iLen - 1] == sLen) {
			return 1;
		} else {
			return 0;
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *