Count elements whose sum with K is greater than max element

Given an array arr[] and integer K, our task is to determine if the sum of each element in the array and K is greater than or equal to the maximum element that is present in the array that is arr[i] + k >= maxElement of array. Print the total count of all such elements.

Examples:

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.

Approach:



To solve the problem mentioned above we have to first store that maximum element that the array has. Then for every element check if the sum of the element and K gives a value greater than the maximum element then increment the count otherwise go to next element.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

1

Time Complexity: O(n)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.