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.
Input : arr = [2, 3, 5, 1, 3], k = 3
Output : 4
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
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.
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:
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.
- Count of greater elements for each element in the Array
- Count the number of elements which are greater than any of element on right side of an array
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count of array elements which are greater than all elements on its left
- Find the element before which all the elements are smaller than it, and after which all are greater
- Longest subsequence with first and last element greater than all other elements
- Elements greater than the previous and next element in an Array
- Count of subarrays whose maximum element is greater than k
- Sum of elements in an array with frequencies greater than or equal to that element
- Smallest subarray having an element with frequency greater than that of other elements
- Number of positions such that adding K to the element is greater than sum of all other elements
- Adding elements of an array until every element becomes greater than or equal to k
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Count subarrays with all elements greater than K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.