给定数组arr [],在其中找到一个Noble整数。如果大于x的整数数量等于x,则在arr []中将整数x称为Noble。如果有许多Noble整数,则返回其中的任何一个。如果没有,则返回-1。
例子 :
Input : [7, 3, 16, 10]
Output : 3
Number of integers greater than 3
is three.
Input : [-1, -9, -2, -78, 0]
Output : 0
Number of integers greater than 0
is zero.
方法1(蛮力)
遍历数组。对于每个元素arr [i],找到大于arr [i]的元素数量。
CPP
// C++ program to find Noble elements
// in an array.
#include
using namespace std;
// Returns a Noble integer if present,
// else returns -1.
int nobleInteger(int arr[], int size)
{
for (int i = 0; i < size; i++ )
{
int count = 0;
for (int j = 0; j < size; j++)
if (arr[i] < arr[j])
count++;
// If count of greater elements
// is equal to arr[i]
if (count == arr[i])
return arr[i];
}
return -1;
}
// Driver code
int main()
{
int arr[] = {10, 3, 20, 40, 2};
int size = sizeof(arr) / sizeof(arr[0]);
int res = nobleInteger(arr, size);
if (res != -1)
cout<<"The noble integer is "<< res;
else
cout<<"No Noble Integer Found";
}
// This code is contributed by Smitha.
Java
// Java program to find Noble elements
// in an array.
import java.util.ArrayList;
class GFG {
// Returns a Noble integer if present,
// else returns -1.
public static int nobleInteger(int arr[])
{
int size = arr.length;
for (int i = 0; i < size; i++ )
{
int count = 0;
for (int j = 0; j < size; j++)
if (arr[i] < arr[j])
count++;
// If count of greater elements
// is equal to arr[i]
if (count == arr[i])
return arr[i];
}
return -1;
}
// Driver code
public static void main(String args[])
{
int [] arr = {10, 3, 20, 40, 2};
int res = nobleInteger(arr);
if (res != -1)
System.out.println("The noble "
+ "integer is "+ res);
else
System.out.println("No Noble "
+ "Integer Found");
}
}
Python3
# Python3 program to find Noble
# elements in an array.
# Returns a Noble integer if
# present, else returns -1.
def nobleInteger(arr, size):
for i in range(0, size):
count = 0
for j in range(0, size):
if (arr[i] < arr[j]):
count += 1
# If count of greater
# elements is equal
# to arr[i]
if (count == arr[i]):
return arr[i]
return -1
# Driver code
arr = [10, 3, 20, 40, 2]
size = len(arr)
res = nobleInteger(arr,size)
if (res != -1):
print("The noble integer is ",
res)
else:
print("No Noble Integer Found")
# This code is contributed by
# Smitha.
C#
// C# program to find Noble elements
// in an array.
using System;
class GFG {
// Returns a Noble integer if present,
// else returns -1.
public static int nobleInteger(int [] arr)
{
int size = arr.Length;
for (int i = 0; i < size; i++ )
{
int count = 0;
for (int j = 0; j < size; j++)
if (arr[i] < arr[j])
count++;
// If count of greater elements
// is equal to arr[i]
if (count == arr[i])
return arr[i];
}
return -1;
}
// Driver code
public static void Main()
{
int [] arr = {10, 3, 20, 40, 2};
int res = nobleInteger(arr);
if (res != -1)
Console.Write("The noble integer"
+ " is "+ res);
else
Console.Write("No Noble Integer"
+ " Found");
}
}
// This code is contributed by Smitha.
PHP
C++
// C++ program to find Noble elements
// in an array.
#include
using namespace std;
// Returns a Noble integer if present,
// else returns -1.
int nobleInteger(int arr[], int n)
{
sort(arr, arr + n);
// Return a Noble element if present
// before last.
for (int i = 0; i < n - 1; i++)
{
if (arr[i] == arr[i + 1])
continue;
// In case of duplicates, we
// reach last occurrence here.
if (arr[i] == n - i - 1)
return arr[i];
}
if (arr[n - 1] == 0)
return arr[n - 1];
return -1;
}
// Driver code
int main()
{
int arr[] = {10, 3, 20, 40, 2};
int res = nobleInteger(arr, 5);
if (res != -1)
cout << "The noble integer is " << res;
else
cout << "No Noble Integer Found";
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find Noble elements
// in an array.
import java.util.Arrays;
public class Main
{
// Returns a Noble integer if present,
// else returns -1.
public static int nobleInteger(int arr[])
{
Arrays.sort(arr);
// Return a Noble element if present
// before last.
int n = arr.length;
for (int i=0; i
Python
# Python3 code to find Noble elements
# in an array
def nobleInteger(arr):
arr.sort()
# Return a Noble element if
# present before last
n = len(arr)
for i in range(n - 1):
if arr[i] == arr[i + 1]:
continue
# In case of duplicates we reach
# last occurrence here
if arr[i] == n - i - 1:
return arr[i]
if arr[n - 1] == 0:
return arr[n - 1]
return -1
# Driver code
arr = [10, 3, 20, 40, 2]
res = nobleInteger(arr)
if res != -1:
print("The noble integer is", res)
else:
print("No Noble Integer Found")
# This code is contributed
# by Mohit Kumar
C#
// C# program to find Noble elements
// in an array.
using System;
public class GFG {
public static int nobleInteger(int[] arr)
{
Array.Sort(arr);
// Return a Noble element if present
// before last.
int n = arr.Length;
for (int i = 0; i < n-1; i++)
{
if (arr[i] == arr[i+1])
continue;
// In case of duplicates, we
// reach last occurrence here.
if (arr[i] == n-i-1)
return arr[i];
}
if (arr[n-1] == 0)
return arr[n-1];
return -1;
}
// Driver code
static public void Main ()
{
int [] arr = {10, 3, 20, 40, 2};
int res = nobleInteger(arr);
if (res != -1)
Console.Write("The noble integer is "
+ res);
else
Console.Write("No Noble Integer "
+ "Found");
}
}
// This code is contributed by Shrikant13.
PHP
C++
// C++ program to find Noble elements
// in an array.
#include
using namespace std;
int nobleInteger(int arr[], int n)
{
// Declare a countArr which keeps
// count of all elements
// greater than or equal to arr[i].
// Initialize it with zero.
int countArr[n + 1] = { 0 };
// Iterating through the given array
for (int i = 0; i < n; i++) {
// If current element is less
// than zero, it cannot
// be a solution so we skip it.
if (arr[i] < 0) {
continue;
}
// If current element is >= size of input array, if
// will be greater than all elements which can be
// considered as our solution, as it cannot be
// greater than size of array.
else if (arr[i] >= n) {
countArr[n]++;
}
// Else we increase the count
// of elements >= our
// current array in countArr
else {
countArr[arr[i]]++;
}
}
// Initially, countArr[n] is
// count of elements greater
// than all possible solutions
int totalGreater = countArr[n];
// Iterating through countArr
for (int i = n - 1; i >= 0; i--) {
// If totalGreater = current index, means we found
// arr[i] for which count of elements >= arr[i] is
// equal to arr[i]
if (totalGreater == i && countArr[i] > 0) {
return i;
}
// If at any point count of elements greater than
// arr[i] becomes more than current index, then it
// means we can no longer have a solution
else if (totalGreater > i) {
return -1;
}
// Adding count of elements >= arr[i] to
// totalGreater.
totalGreater += countArr[i];
}
return -1;
}
// Driver Code
int main()
{
int arr[] = { 10, 3, 20, 40, 2 };
int res = nobleInteger(arr, 5);
if (res != -1)
cout << "The noble integer is " << res;
else
cout << "No Noble Integer Found";
return 0;
}
输出
The noble integer is 3
方法2(使用排序)
- 以升序对数组arr []进行排序。此步骤需要(O(nlogn))。
- 遍历数组。将索引i的值与索引i之后的元素数进行比较。如果arr [i]等于arr [i]之后的元素数,则它是一个高尚的整数。检查条件:(A [i] == length-i-1)。此步骤需要O(n)。
注意:数组可能具有重复的元素。因此,我们应该跳过相同的元素(排序数组中的相邻元素)。
C++
// C++ program to find Noble elements
// in an array.
#include
using namespace std;
// Returns a Noble integer if present,
// else returns -1.
int nobleInteger(int arr[], int n)
{
sort(arr, arr + n);
// Return a Noble element if present
// before last.
for (int i = 0; i < n - 1; i++)
{
if (arr[i] == arr[i + 1])
continue;
// In case of duplicates, we
// reach last occurrence here.
if (arr[i] == n - i - 1)
return arr[i];
}
if (arr[n - 1] == 0)
return arr[n - 1];
return -1;
}
// Driver code
int main()
{
int arr[] = {10, 3, 20, 40, 2};
int res = nobleInteger(arr, 5);
if (res != -1)
cout << "The noble integer is " << res;
else
cout << "No Noble Integer Found";
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find Noble elements
// in an array.
import java.util.Arrays;
public class Main
{
// Returns a Noble integer if present,
// else returns -1.
public static int nobleInteger(int arr[])
{
Arrays.sort(arr);
// Return a Noble element if present
// before last.
int n = arr.length;
for (int i=0; i
Python
# Python3 code to find Noble elements
# in an array
def nobleInteger(arr):
arr.sort()
# Return a Noble element if
# present before last
n = len(arr)
for i in range(n - 1):
if arr[i] == arr[i + 1]:
continue
# In case of duplicates we reach
# last occurrence here
if arr[i] == n - i - 1:
return arr[i]
if arr[n - 1] == 0:
return arr[n - 1]
return -1
# Driver code
arr = [10, 3, 20, 40, 2]
res = nobleInteger(arr)
if res != -1:
print("The noble integer is", res)
else:
print("No Noble Integer Found")
# This code is contributed
# by Mohit Kumar
C#
// C# program to find Noble elements
// in an array.
using System;
public class GFG {
public static int nobleInteger(int[] arr)
{
Array.Sort(arr);
// Return a Noble element if present
// before last.
int n = arr.Length;
for (int i = 0; i < n-1; i++)
{
if (arr[i] == arr[i+1])
continue;
// In case of duplicates, we
// reach last occurrence here.
if (arr[i] == n-i-1)
return arr[i];
}
if (arr[n-1] == 0)
return arr[n-1];
return -1;
}
// Driver code
static public void Main ()
{
int [] arr = {10, 3, 20, 40, 2};
int res = nobleInteger(arr);
if (res != -1)
Console.Write("The noble integer is "
+ res);
else
Console.Write("No Noble Integer "
+ "Found");
}
}
// This code is contributed by Shrikant13.
的PHP
输出
The noble integer is 3
方法3(使用计数数组)
保持一个计数数组countArr [],使所有元素的计数都大于或等于arr [i]。
- 声明一个大小为n + 1(其中n是给定数组arr的大小)的整数数组countArr [],并将其初始化为零。
- 遍历数组arr,如果arr [i] <0,则忽略它,如果arr [i]> = n,则递增countArr [n],否则仅递增countArr [arr [i]]。
- 声明一个整数totalGreater,该元素使元素数大于当前元素数,并将其初始化为countArr [arr [n]]。
- 从最后一个索引到第一个索引遍历count数组countArr,如果在任何时候我们发现对于countArr [i]> 0,totalGreater = i,我们就找到了解决方案。否则,继续使用countArr [i]来增加totalGreater。
C++
// C++ program to find Noble elements
// in an array.
#include
using namespace std;
int nobleInteger(int arr[], int n)
{
// Declare a countArr which keeps
// count of all elements
// greater than or equal to arr[i].
// Initialize it with zero.
int countArr[n + 1] = { 0 };
// Iterating through the given array
for (int i = 0; i < n; i++) {
// If current element is less
// than zero, it cannot
// be a solution so we skip it.
if (arr[i] < 0) {
continue;
}
// If current element is >= size of input array, if
// will be greater than all elements which can be
// considered as our solution, as it cannot be
// greater than size of array.
else if (arr[i] >= n) {
countArr[n]++;
}
// Else we increase the count
// of elements >= our
// current array in countArr
else {
countArr[arr[i]]++;
}
}
// Initially, countArr[n] is
// count of elements greater
// than all possible solutions
int totalGreater = countArr[n];
// Iterating through countArr
for (int i = n - 1; i >= 0; i--) {
// If totalGreater = current index, means we found
// arr[i] for which count of elements >= arr[i] is
// equal to arr[i]
if (totalGreater == i && countArr[i] > 0) {
return i;
}
// If at any point count of elements greater than
// arr[i] becomes more than current index, then it
// means we can no longer have a solution
else if (totalGreater > i) {
return -1;
}
// Adding count of elements >= arr[i] to
// totalGreater.
totalGreater += countArr[i];
}
return -1;
}
// Driver Code
int main()
{
int arr[] = { 10, 3, 20, 40, 2 };
int res = nobleInteger(arr, 5);
if (res != -1)
cout << "The noble integer is " << res;
else
cout << "No Noble Integer Found";
return 0;
}
输出
The noble integer is 3
- 复杂度分析:
- 时间复杂度:O(n)。当我们遍历输入数组和countArr一次时。
- 空间复杂度:O(n)。由于我们使用的countArr大小与给定数组相同。