Number of positions such that adding K to the element is greater than sum of all other elements Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] and a number K. The task is to find out the number of valid positions i such that (arr[i] + K) is greater than sum of all elements of array excluding arr[i].Examples: Input: arr[] = {2, 1, 6, 7} K = 4 Output: 1 Explanation: There is only 1 valid position i.e 4th. After adding 4 to the element at 4th position it is greater than the sum of all other elements of the array. Input: arr[] = {2, 1, 5, 4} K = 2 Output: 0 Explanation: There is no valid position. Approach: First of all find the sum of all the elements of the array and store it in a variable say sum.Now, traverse the array and for every position i check if the condition (arr[i] + K) > (sum - arr[i]) holds.If YES then increase the counter and finally print the value of counter. Below is the implementation of the above approach: C++ // C++ program to implement above approach #include <bits/stdc++.h> using namespace std; // Function that will find out // the valid position int validPosition(int arr[], int N, int K) { int count = 0, sum = 0; // find sum of all the elements for (int i = 0; i < N; i++) { sum += arr[i]; } // adding K to the element and check // whether it is greater than sum of // all other elements for (int i = 0; i < N; i++) { if ((arr[i] + K) > (sum - arr[i])) count++; } return count; } // Driver code int main() { int arr[] = { 2, 1, 6, 7 }, K = 4; int N = sizeof(arr) / sizeof(arr[0]); cout << validPosition(arr, N, K); return 0; } Java // Java implementation of the approach class GFG { // Function that will find out // the valid position static int validPosition(int arr[], int N, int K) { int count = 0, sum = 0; // find sum of all the elements for (int i = 0; i < N; i++) { sum += arr[i]; } // adding K to the element and check // whether it is greater than sum of // all other elements for (int i = 0; i < N; i++) { if ((arr[i] + K) > (sum - arr[i])) count++; } return count; } // Driver code public static void main(String[] args) { int arr[] = { 2, 1, 6, 7 }, K = 4; int N = arr.length; System.out.println(validPosition(arr, N, K)); } } /* This code contributed by PrinciRaj1992 */ Python3 # Python3 program to implement # above approach # Function that will find out # the valid position def validPosition(arr, N, K): count = 0; sum = 0; # find sum of all the elements for i in range(N): sum += arr[i]; # adding K to the element and check # whether it is greater than sum of # all other elements for i in range(N): if ((arr[i] + K) > (sum - arr[i])): count += 1; return count; # Driver code arr = [2, 1, 6, 7 ]; K = 4; N = len(arr); print(validPosition(arr, N, K)); # This code is contributed by 29AjayKumar C# // C# implementation of the approach using System; class GFG { // Function that will find out // the valid position static int validPosition(int []arr, int N, int K) { int count = 0, sum = 0; // find sum of all the elements for (int i = 0; i < N; i++) { sum += arr[i]; } // adding K to the element and check // whether it is greater than sum of // all other elements for (int i = 0; i < N; i++) { if ((arr[i] + K) > (sum - arr[i])) count++; } return count; } // Driver code public static void Main(String[] args) { int []arr = { 2, 1, 6, 7 };int K = 4; int N = arr.Length; Console.WriteLine(validPosition(arr, N, K)); } } // This code has been contributed by 29AjayKumar PHP <?php // PHP program to implement above approach // Function that will find out // the valid position function validPosition($arr, $N, $K) { $count = 0; $sum = 0; // find sum of all the elements for ($i = 0; $i < $N; $i++) { $sum += $arr[$i]; } // adding K to the element and check // whether it is greater than sum of // all other elements for ($i = 0; $i < $N; $i++) { if (($arr[$i] + $K) > ($sum - $arr[$i])) $count++; } return $count; } // Driver code $arr = array( 2, 1, 6, 7 ); $K = 4; $N = count($arr) ; echo validPosition($arr, $N, $K); // This code is contributed by AnkitRai01 ?> JavaScript <script> // Javascript program to implement above approach // Function that will find out // the valid position function validPosition(arr, N, K) { var count = 0, sum = 0; // find sum of all the elements for (var i = 0; i < N; i++) { sum += arr[i]; } // adding K to the element and check // whether it is greater than sum of // all other elements for (var i = 0; i < N; i++) { if ((arr[i] + K) > (sum - arr[i])) count++; } return count; } // Driver code var arr = [ 2, 1, 6, 7 ], K = 4; var N = arr.length; document.write( validPosition(arr, N, K)); </script> Output: 1 Time Complexity : O(N)Auxiliary Space : O(1) Comment More infoAdvertise with us Next Article Number of positions such that adding K to the element is greater than sum of all other elements N Naman_Garg Follow Improve Article Tags : Mathematical C++ Programs DSA Arrays school-programming +1 More Practice Tags : ArraysMathematical Similar Reads Minimum replacements required to have at most K distinct elements in the array Given an array arr[] consisting of N positive integers and an integer K, the task is to find the minimum number of array elements required to be replaced by the other array elements such that the array contains at most K distinct elements. Input: arr[] = { 1, 1, 2, 2, 5 }, K = 2 Output: 1 Explanatio 13 min read Maximum value of X such that difference between any array element and X does not exceed K Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the maximum possible integer X, such that the absolute difference between any array element and X is at most K. If no such value of X exists, then print "-1". Examples: Input: arr[] = {6, 4, 8, 5}, K 10 min read Maximum subsequence sum with adjacent elements having atleast K difference in index Given an array arr[] consisting of integers of length N and an integer K (1 ? k ? N), the task is to find the maximum subsequence sum in the array such that adjacent elements in that subsequence have at least a difference of K in their indices in the original array. Examples: Input: arr[] = {1, 2, - 8 min read Minimize insertions in an array to obtain all sums upto N Given a sorted array arr[] of positive integers of size K, and an integer N. The task is to find the minimum number of elements to be inserted in this array, such that all positive integers in range [1, N] can be obtained as a sum of subsets of the modified array. Note: The array won't have any dupl 10 min read Find maximum Subsequence Sum according to given conditions Given an integer array nums and an integer K, The task is to find the maximum sum of a non-empty subsequence of the array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= K is satisfied. A subsequence of an array is obtai 5 min read Length of Smallest Subsequence such that sum of elements is greater than equal to K Given an array arr[] of size N and a number K, the task is to find the length of the smallest subsequence such that the sum of the subsequence is greater than or equal to number K.Example: Input: arr[] = {2, 3, 1, 5, 6, 3, 7, 9, 14, 10, 2, 5}, K = 35 Output: 4 Smallest subsequence with the sum great 6 min read Find a number K such that exactly K array elements are greater than or equal to K Given an array a[] of size N, which contains only non-negative elements, the task is to find any integer K for which there are exactly K array elements that are greater than or equal to K. If no such K exists, then print -1. Examples: Input: a[] = {7, 8, 9, 0, 0, 1}Output: 3Explanation:Since 3 is le 10 min read Count elements that are greater than at least K elements on its left and right Given an array arr[] of size n (1 <= n <= 10^5) and a positive integer k, the task is to count all indices i ( 1<= i < n) in the array such that at least k elements to the left of i and at least k elements to the right of i, which are strictly smaller than the value at the ith index (i.e 10 min read Min sum of set of positive integers so that sum of no two elements is K Given two positive integers N and K. Create a set of size N containing distinct positive integers such that the sum of no two integers is K, the task is to find the minimum possible sum of the set that meets the above criteria. Since the answer may be large, print it modulo 10^9+7. Note: It is guara 6 min read Count elements whose sum with K is greater than max element 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 7 min read Like