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++ 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 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 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# 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 |
<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> |
1
Time Complexity: O(n)
Auxiliary Space: O(1)