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.
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Smallest subarray of size greater than K with sum greater than a given value
- Count of subarrays whose maximum element is greater than k
- Count of subsets having sum of min and max element less than K
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Sum of all array elements less than X and greater than Y for Q queries
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Minimum element whose n-th power is greater than product of an array of size n
- Count the subarray with sum strictly greater than the sum of remaining elements
- 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 the number of elements which are greater than any of element on right side of an array
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Sum of elements in an array with frequencies greater than or equal to that element
- Number of positions such that adding K to the element is greater than sum of all other elements
- Count elements such that there are exactly X elements with values greater than or equal to X
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.