Count the values greater than X in the modified array - GeeksforGeeks

20 May, 2021
Given an array Arr of positive integers and a value X. The task is to find the number of values that is greater than or equal to X.But the twist is that the values of the array are kept changing after every operation. There are two possibilities:  
If the current value is picked then all the remaining values in the array will be decreased by 1.
If the current value is not picked then all the remaining values in the array will be increased by 1.
Examples:  
Input: arr[] = {10, 5, 5, 4, 9}, X = 10 Output: 2Explanation: Arr = {10, 5, 5, 4, 9}, pos = 0 10 is pickedArr = {10, 4, 4, 3, 8}, pos = 1 4 is not pickedArr = {10, 4, 5, 4, 9}, pos = 2 5 is not pickedArr = {10, 4, 5, 5, 10}, pos = 3 5 is not pickedArr = {10, 4, 5, 5, 11}, pos = 4 11 is pickedHence two elements are picked.Input: arr[] = {5, 4, 3, 2, 1}, X = 4 Output: 1  
 
Naive Approach: The idea is to iterate through every value in an array and check whether the ith value is greater, lesser, or equal than required value X. If the ith value is less than required value then increase the value from (i+1)th to end of the array by 1 else If the ith value is greater or equal than required value X then decrease the value by 1 from (i+1)th to end of the array. Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to count number// of values greater or equal to xint increaseDecreaseValue(int arr[],                          int x, int n){    int TotalValue = 0;    for (int i = 0; i < n; i++)    {        if (arr[i] < x)        {             // Current value is less            // than required value            // then we need to increase            // the value from i + 1 to            // len(arr) by 1            for (int j = i + 1; j < n; j++)            {                arr[j] += 1;            }        }        else        {             // Current value is greater            // or equal to required            // value then we need to            // decrease the value from            // (i + 1) to len(arr)-1 by 1            TotalValue += 1;            for (int j = i + 1; j < n; j++)            {                if (arr[j] == 0)                {                    continue;                }                else                {                    arr[j] -= 1;                }            }        }    }    return TotalValue;} // Driver Codeint main(){    int x = 4;    int arr[] = {5, 4, 3, 2, 1};    int n = sizeof(arr) / sizeof(arr[0]);    int countValue = increaseDecreaseValue(arr, x, n);    cout << countValue;    return 0;} // This code is contributed by Rajput-Ji
// Java implementation of the approachimport java.util.*; class GFG{ // Function to count number// of values greater or equal to xstatic int increaseDecreaseValue(int []arr, int x){    int TotalValue = 0;    for (int i = 0; i < arr.length; i++)    {        if (arr[i] < x)        {             // Current value is less            // than required value            // then we need to increase            // the value from i + 1 to            // len(arr) by 1            for (int j = i + 1; j < arr.length; j++)            {                arr[j] += 1;            }        }        else        {             // Current value is greater            // or equal to required            // value then we need to            // decrease the value from            // (i + 1) to len(arr)-1 by 1            TotalValue += 1;            for (int j = i + 1; j < arr.length; j++)            {                if (arr[j] == 0)                {                    continue;                }                else                {                    arr[j] -= 1;                }            }        }    }    return TotalValue;} // Driver Codepublic static void main(String[] args){    int x = 4;    int[] arr = {5, 4, 3, 2, 1};    int countValue = increaseDecreaseValue(arr, x);    System.out.println(countValue);}} // This code is contributed by Rajput-Ji
# Python3 implementation# of the approach # Function to count number# of values greater or equal to xdef increaseDecreaseValue(arr, x):    TotalValue = 0    for i in range(len(arr)):        if arr[i] < x:                         # Current value is less            # than required value            # then we need to increase            # the value from i + 1 to            # len(arr) by 1            for j in range(i + 1, len(arr)):                arr[j] += 1        else:                         # Current value is greater            # or equal to required            # value then we need to            # decrease the value from            # (i + 1) to len(arr)-1 by 1            TotalValue += 1                         for j in range(i + 1, len(arr)):                if arr[j] == 0:                    continue                arr[j] -= 1    return TotalValue  # Driver Codeif __name__ == "__main__":    x = 4    arr = [5, 4, 3, 2, 1]    countValue =\            increaseDecreaseValue(arr, x)    print(countValue)
// C# implementation of the approachusing System;     class GFG{ // Function to count number// of values greater or equal to xstatic int increaseDecreaseValue(int []arr, int x){    int TotalValue = 0;    for (int i = 0; i < arr.Length; i++)    {        if (arr[i] < x)        {             // Current value is less            // than required value            // then we need to increase            // the value from i + 1 to            // len(arr) by 1            for (int j = i + 1; j < arr.Length; j++)            {                arr[j] += 1;            }        }        else        {             // Current value is greater            // or equal to required            // value then we need to            // decrease the value from            // (i + 1) to len(arr)-1 by 1            TotalValue += 1;            for (int j = i + 1; j < arr.Length; j++)            {                if (arr[j] == 0)                {                    continue;                }                else                {                    arr[j] -= 1;                }            }        }    }    return TotalValue;} // Driver Codepublic static void Main(String[] args){    int x = 4;    int[] arr = {5, 4, 3, 2, 1};    int countValue = increaseDecreaseValue(arr, x);    Console.WriteLine(countValue);}} // This code is contributed by PrinciRaj1992
<script>    // Javascript implementation of the approach         // Function to count number    // of values greater or equal to x    function increaseDecreaseValue(arr, x)    {        let TotalValue = 0;        for (let i = 0; i < arr.length; i++)        {            if (arr[i] < x)            {                 // Current value is less                // than required value                // then we need to increase                // the value from i + 1 to                // len(arr) by 1                for (let j = i + 1; j < arr.length; j++)                {                    arr[j] += 1;                }            }            else            {                 // Current value is greater                // or equal to required                // value then we need to                // decrease the value from                // (i + 1) to len(arr)-1 by 1                TotalValue += 1;                for (let j = i + 1; j < arr.length; j++)                {                    if (arr[j] == 0)                    {                        continue;                    }                    else                    {                        arr[j] -= 1;                    }                }            }        }        return TotalValue;    }     let x = 4;    let arr = [5, 4, 3, 2, 1];    let countValue = increaseDecreaseValue(arr, x);    document.write(countValue);     </script>
1
 
ime Complexity: Efficient Approach:  
This problem can further be optimized to .
Here the main idea is to check by how much this index value should change.
This can be done by using a temporary variable, here it is currentStatus that will keep the net effect on the current index by the previous decisions.
The effect will be added to the value of that index and that will tell us the updated original value of the array.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>#include <vector>using namespace std; // Function to count number// of values greater or equal to xint increaseDecreaseValue(int arr[],                          int x, int n){    int TotalValue = 0;    for (int i = 0; i < n; i++)    {        if (arr[i] < x)        {             // Current value is less            // than required value            // then we need to increase            // the value from i + 1 to            // len(arr) by 1            for (int j = i + 1; j < n; j++)            {                arr[j] += 1;            }        }        else        {             // Current value is greater            // or equal to required            // value then we need to            // decrease the value from            // (i + 1) to len(arr)-1 by 1            TotalValue += 1;            for (int j = i + 1; j < n; j++)            {                if (arr[j] == 0)                {                    continue;                }                else                {                    arr[j] -= 1;                }            }        }    }    return TotalValue;} // Driver Codeint main(){    int x = 4;    int arr[] = {5, 4, 3, 2, 1};    int n = sizeof(arr) / sizeof(arr[0]);    int countValue = increaseDecreaseValue(arr, x, n);    cout << countValue;     return 0;} // This code is contributed by 29AjayKumar
// Java implementation of the approachclass GFG{             // Function to count number    // of students got selected    static int increaseDecreaseValue(int arr[], int x)    {        int currentStatus = 0;        int totalValue = 0;                 int i;        int len = arr.length;                 for (i = 0; i < len ; i++ )        {                         // Adding currentStatus to the            // value of that index to get            // the original value                         // if it is less than X            if (arr[i] + currentStatus < x)                currentStatus += 1;                         else            {                currentStatus -= 1;                totalValue += 1;            }        }        return totalValue;    }         // Driver Code    public static void main (String[] args)    {        int x = 4;        int arr[] = {5, 4, 3, 2, 1};        int countValue = increaseDecreaseValue(arr, x);        System.out.println(countValue);    }} // This code is contributed by AnkitRai01
# Python3 implementation# of the approach # Function to count number# of students got selecteddef increaseDecreaseValue(arr, x):         currentStatus = 0    totalValue = 0         for i in arr:                 # Adding currentStatus to the        # value of that index to get        # the original value                 # if it is less than X        if i + currentStatus < x:            currentStatus += 1                 else:            currentStatus -= 1            totalValue += 1                 return totalValue # Drivers Codeif __name__ == "__main__":    x = 4    arr = [5, 4, 3, 2, 1]    countValue = increaseDecreaseValue(arr, x)    print(countValue)
// C# implementation of the approachusing System; class GFG{    // Function to count number    // of students got selected    static int increaseDecreaseValue(int []arr,                                     int x)    {        int currentStatus = 0;        int totalValue = 0;                 int i;        int len = arr.Length;                 for (i = 0; i < len ; i++ )        {                         // Adding currentStatus to the            // value of that index to get            // the original value                         // if it is less than X            if (arr[i] + currentStatus < x)                currentStatus += 1;                         else            {                currentStatus -= 1;                totalValue += 1;            }        }        return totalValue;    }         // Driver Code    static public void Main ()    {        int x = 4;        int []arr = {5, 4, 3, 2, 1};        int countValue = increaseDecreaseValue(arr, x);        Console.Write(countValue);    }} // This code is contributed by ajit.
<script>    // Javascript implementation of the approach         // Function to count number    // of students got selected    function increaseDecreaseValue(arr, x)    {        let currentStatus = 0;        let totalValue = 0;                  let i;        let len = arr.length;                  for (i = 0; i < len ; i++ )        {                          // Adding currentStatus to the            // value of that index to get            // the original value                          // if it is less than X            if (arr[i] + currentStatus < x)                currentStatus += 1;                          else            {                currentStatus -= 1;                totalValue += 1;            }        }        return totalValue;    }         let x = 4;    let arr = [5, 4, 3, 2, 1];    let countValue = increaseDecreaseValue(arr, x);    document.write(countValue);     </script>
1
 
Time Complexity:  
ankthon
jit_t
Rajput-Ji
princiraj1992
29AjayKumar
nidhi_biet
arorakashish0911
suresh07
divyeshrabadiya07
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Quadratic Probing in Hashing
Converting Roman Numerals to Decimal lying between 1 to 3999
Arrays in Java
Arrays in C/C++
Program for array rotation
Stack Data Structure (Introduction and Program)
Largest Sum Contiguous Subarray