Number of positions such that adding K to the element is greater than sum of all other elements
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
Explaination: 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
Explaination: 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; } |
chevron_right
filter_none
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 */ |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
1
Recommended Posts:
- Adding elements of an array until every element becomes greater than or equal to k
- Count the number of elements which are greater than any of element on right side of an array
- Longest subsequence with first and last element greater than all other elements
- Find the element before which all the elements are smaller than it, and after which all are greater
- Elements greater than the previous and next element in an Array
- Sum of elements in an array with frequencies greater than or equal to that element
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Number of segments where all elements are greater than X
- Find the number of elements greater than k in a sorted array
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Find first and last positions of an element in a sorted array
- Longest Subarray with first element greater than or equal to Last element
- Convert given array to Arithmetic Progression by adding an element
- A permutation where each element indicates either number of elements before or after it
- Positive elements at even and negative at odd positions (Relative order not maintained)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



