Count elements whose sum with K is greater than max element
Last Updated :
15 Mar, 2023
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.
Efficient Approach:
- Define a variable maxi and initialize it with the minimum integer value using INT_MIN.
- Iterate over the array arr and store the maximum element in the array in the variable maxi.
- Define a variable cnt and initialize it with 0.
- 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.
- If the sum is greater than or equal to maxi, increment the cnt variable.
- If the sum is less than maxi, continue to the next iteration.
- 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.
- In the main function, declare an array arr and initialize it with some values.
- Declare an integer k and initialize it with some value.
- Calculate the size of the array arr using sizeof() operator and store it in the variable n.
- Call the function countNum with the array arr, integer k, and integer n as parameters, and store the returned value in a variable.
- Print the value of the variable as the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNum( int arr[], int K, int n)
{
int maxi = INT_MIN;
for ( int i = 0; i < n; i++) {
if (arr[i] > maxi)
maxi = arr[i];
}
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] + K >= maxi)
cnt++;
else
continue ;
}
return cnt;
}
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
class GFG{
public static int countNum( int arr[], int K, int n)
{
int maxi = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > maxi)
maxi = arr[i];
}
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] + K >= maxi)
cnt++;
else
continue ;
}
return cnt;
}
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));
}
}
|
Python3
import sys
def countNum(arr, K, n):
maxi = - sys.maxsize
for i in range (n) :
if arr[i] > maxi:
maxi = arr[i]
cnt = 0
for i in range (n):
if (arr[i] + K) > = maxi:
cnt + = 1
else :
continue
return cnt
if __name__ = = '__main__' :
arr = [ 4 , 2 , 1 , 1 , 2 ]
k = 1
n = len (arr)
print (countNum(arr, k, n))
|
C#
using System;
class GFG{
public static int countNum( int [] arr, int K,
int n)
{
int maxi = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > maxi)
maxi = arr[i];
}
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] + K >= maxi)
cnt++;
else
continue ;
}
return cnt;
}
public static void Main()
{
int [] arr = { 4, 2, 1, 1, 2 };
int k = 1;
int n = arr.Length;
Console.Write(countNum(arr, k, n));
}
}
|
Javascript
<script>
function countNum(arr, K, n)
{
var maxi = -1000000000;
for ( var i = 0; i < n; i++) {
if (arr[i] > maxi)
maxi = arr[i];
}
var cnt = 0;
for ( var i = 0; i < n; i++) {
if (arr[i] + K >= maxi)
cnt++;
else
continue ;
}
return cnt;
}
var arr = [4, 2, 1, 1, 2];
var k = 1;
var n = arr.length;
document.write( countNum(arr, k, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...