📌  相关文章
📜  计算与K之和大于max元素的元素

📅  最后修改于: 2021-05-17 03:34:32             🧑  作者: Mango

给定一个数组arr []整数K ,我们的任务是确定数组中每个元素与K的和是否大于或等于数组arr [i] + k>中存在的最大元素。 = array的maxElement 。打印所有这些元素的总数。

例子:

方法:

为了解决上述问题,我们必须首先存储该数组具有的最大元素。然后,对于每个元素,检查元素和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


输出:
1

时间复杂度: O(n)
辅助空间: O(1)