给定一个数组 arr[]和整数 K ,我们的任务是确定数组中每个元素与 K 的总和是否大于或等于数组中存在的最大元素arr[i] + k > = 数组的最大元素。打印所有这些元素的总数。
例子:
Input : arr = [2, 3, 5, 1, 3], k = 3
Output : 4
Explanations :
In the given array the elements 2, 3, 5, 3 satisfy the condition because all of them on adding up with 3(=K) yields a value that is greater than the maximum element of the array which is 5.
Input : arr = [4, 2, 1, 1, 2], k = 1
Output : 1
Explanations :
In the given array the element 4 satisfy the condition because on adding 4 with 1(=K) we get a value that is greater than the maximum element of the array which is 4 itself.
方法:
为了解决上面提到的问题,我们必须首先存储数组具有的最大元素。然后对于每个元素检查元素和 K 的总和是否给出大于最大元素的值,然后增加计数,否则转到下一个元素。
下面是上述方法的实现:
C++
// C++ implementation to Count of all the elements
// in the array whose summation with integer K returns
// a value that is greater than or equal to the
// maximum value present in the array
#include
using namespace std;
// Function to count all the elements
int countNum(int arr[], int K, int n)
{
int maxi = INT_MIN;
// Store the maximum array element
for (int i = 0; i < n; i++) {
if (arr[i] > maxi)
maxi = arr[i];
}
int cnt = 0;
// Iterate in array
for (int i = 0; i < n; i++) {
// Check if current element and k gives
// a greater sum than max element
if (arr[i] + K >= maxi)
// Increment the count
cnt++;
else
continue;
}
// Return the final result
return cnt;
}
// Driver code
int main()
{
int arr[] = { 4, 2, 1, 1, 2 };
int k = 1;
int n = sizeof(arr) / sizeof(arr[0]);
cout << countNum(arr, k, n) << endl;
return 0;
}
Java
// Java implementation to count of all the elements
// in the array whose summation with integer K returns
// a value that is greater than or equal to the
// maximum value present in the array
class GFG{
// Function to count all the elements
public static int countNum(int arr[], int K, int n)
{
int maxi = 0;
// Store the maximum array element
for(int i = 0; i < n; i++)
{
if (arr[i] > maxi)
maxi = arr[i];
}
int cnt = 0;
// Iterate in array
for(int i = 0; i < n; i++)
{
// Check if current element and k gives
// a greater sum than max element
if (arr[i] + K >= maxi)
// Increment the count
cnt++;
else
continue;
}
// Return the final result
return cnt;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 4, 2, 1, 1, 2 };
int k = 1;
int n = arr.length;
System.out.println(countNum(arr, k, n));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 implementation to Count of all the elements
# in the array whose summation with integer K returns
# a value that is greater than or equal to the
# maximum value present in the array
import sys
# Function to count all the elements
def countNum(arr, K, n):
maxi = -sys.maxsize
# Store the maximum array element
for i in range(n) :
if arr[i] > maxi:
maxi = arr[i]
cnt = 0
# Iterate in array
for i in range(n):
# Check if current element and k gives
# a greater sum than max element
if (arr[i] + K) >= maxi:
# Increment the count
cnt += 1
else :
continue
# Return the final result
return cnt
# Driver code
if __name__=='__main__':
arr = [ 4, 2, 1, 1, 2 ]
k = 1
n = len(arr)
print(countNum(arr, k, n))
# This code is contributed by rutvik_56
C#
// C# implementation to count of all
// the elements in the array whose
// summation with integer K returns
// a value that is greater than or
// equal to the maximum value present
// in the array
using System;
class GFG{
// Function to count all the elements
public static int countNum(int[] arr, int K,
int n)
{
int maxi = 0;
// Store the maximum array element
for(int i = 0; i < n; i++)
{
if (arr[i] > maxi)
maxi = arr[i];
}
int cnt = 0;
// Iterate in array
for(int i = 0; i < n; i++)
{
// Check if current element and k
// gives a greater sum than max
// element
if (arr[i] + K >= maxi)
// Increment the count
cnt++;
else
continue;
}
// Return the final result
return cnt;
}
// Driver code
public static void Main()
{
int[] arr = { 4, 2, 1, 1, 2 };
int k = 1;
int n = arr.Length;
Console.Write(countNum(arr, k, n));
}
}
// This code is contributed by chitranayal
Javascript
1
时间复杂度: O(n)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live