Saturday, 18 August 2018

Find Noble Integer Program

Question: Given an integer array, find if an integer p exists in the array such that the number of integers greater than p in the array equals to p
If such an integer is found return 1 else return -1.

Solution:
static int checkNobleInteger(int[] arr) {
        for(int i=0;i<arr.length;i++) {
           
            int count = 0;
            for(int j=0;j<arr.length;j++) {
               
                if(arr.length-j+count<arr[i]) {
                    break;
                }
               
                if(arr[j]>arr[i]) {
                    count++;
                }
            }
            if(arr[i]==count) {
                return 1;
            }
        }
        return -1;
    }

input: {4, 3, 5, 6, 7, 8}
output: 1


input: {6, 3, 5, 6, 7, 8}
output: -1

Ref: https://www.interviewbit.com

No comments:

Post a Comment

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...