Wednesday, 14 June 2017

Array | Noble integers in an array

Given an array, find a Noble integer in it.
Noble Integer
An integer x is said to be Noble in an array if the number of integers greater than x is equal to x. If there are many Noble integers, return any of them. If there is no, then return -1.
      
private static int findNobleInteger(int[] array) {
           int noble = -1;
           /**Sort the array. */
           Arrays.sort(array);

           int length  = array.length;
           for (int i = 0; i < length; i++) {
                if (array[i] == array[i+1]) {
                     continue;
                }
                /**Check wheter the small number count is greater than number.*/                
                if(array[i]<length-i) {
                     noble = array[i];
                     break;
                }
           }
           return noble;
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...