Skip to content
Related Articles
Open in App
Not now

Related Articles

Count elements whose sum with K is greater than max element

Improve Article
Save Article
  • Last Updated : 21 May, 2021
Improve Article
Save Article

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!