Saturday, May 28, 2016

Codility PermMissingElem C# Solution

Codility PermMissingElem C# Solution

Find the missing element in a given permutation.

Solution:

public static int MissingInt(int[] A)
        {
            if (A.Length != 0 && A.Length == 1)
                return A[0] == 1 ? 2 : 1;
            else if (A.Length != 0 && A.Length > 1)
            {
                Array.Sort(A);
                if (A[0] != 1)
                    return 1;
                else
                {
                    for (int i = 0; i < A.Length - 1; i++)
                    {
                        if (A[i + 1] - A[i] > 1)
                            return A[i] + 1;
                    }
                }
                return A.Length + 1;
            }
            return 1;
        }
And Here goes the Result, Got 100%



Perm Missing Element

No comments:

Post a Comment