Advanced Java Programming - Old Questions

4. A non-empty array A of length n is called on array of all possibilities if it contains all numbers between 0 and A.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0. (15)

5 marks | Asked in 2077

public static int isAllPossibilities(int[] a)

        {

            int isAllPosibilities = 1;

            if (a.Length == 0) isAllPosibilities = 0;

            for (int i = 0; i < a.Length && isAllPosibilities==1; i++)

            {

                int index = -1;

                for (int j = 0; j < a.Length && index==-1; j++)

                {

                    if (i == a[j]) index = j;

                }

                if (index == -1)

                    isAllPosibilities = 0;

            }

            return isAllPosibilities;

        }