Question: Given an integer array, find if an integer
If such an integer is found return
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
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