Open In App

Count elements whose sum with K is greater than max element

Improve
Improve
Like Article
Like
Save
Share
Report

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 :
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 :
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.

Efficient Approach:

  1. Define a variable maxi and initialize it with the minimum integer value using INT_MIN.
  2. Iterate over the array arr and store the maximum element in the array in the variable maxi.
  3. Define a variable cnt and initialize it with 0.
  4. Iterate over the array arr and check if the current element arr[i] and the integer K gives a greater sum than the maximum element maxi.
  5. If the sum is greater than or equal to maxi, increment the cnt variable.
  6. If the sum is less than maxi, continue to the next iteration.
  7. After iterating over the entire array, return the final value of cnt as the count of all the elements whose summation with K returns a value that is greater than or equal to the maximum value present in the array.
  8. In the main function, declare an array arr and initialize it with some values.
  9. Declare an integer k and initialize it with some value.
  10. Calculate the size of the array arr using sizeof() operator and store it in the variable n.
  11. Call the function countNum with the array arr, integer k, and integer n as parameters, and store the returned value in a variable.
  12. Print the value of the variable as the output.

Below is the implementation of the above approach:
 

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 <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;
}


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




<script>
 
// Javascript 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
 
// Function to count all the elements
function countNum(arr, K, n)
{
    var maxi = -1000000000;
 
    // Store the maximum array element
    for (var i = 0; i < n; i++) {
        if (arr[i] > maxi)
            maxi = arr[i];
    }
 
    var cnt = 0;
 
    // Iterate in array
    for (var 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
var arr = [4, 2, 1, 1, 2];
var k = 1;
var n = arr.length;
document.write( countNum(arr, k, n));
 
</script>


Output: 

1

 

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



Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads