Noble Integer - The Coding Shala

Home >> Interview Questions >> Noble Integer

Noble Integer InterviewBit Solution

Problem : 

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.


Noble Integer Problem Java Solution - The Coding Shala

Solution 1 (Java) :

public class Solution {
    public int solve(ArrayList<Integer> A) {
        Collections.sort(A);
        if(A.get(A.size()-1)==0) return 1;
        for(int i=0;i<A.size();i++){
            if(A.get(i)==(A.size()-i-1) && A.get(i)!=A.get(i+1)) return 1;
        }
        return -1;
    }
}



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

Anti Diagonals - The Coding Shala

Java Method Overloading - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala