How To Check For Value Greater Than 1 In Ruby
Given an array Arr of positive integers and a value X. The task is to detect 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 electric current value is picked then all the remaining values in the array will be decreased past 1.
- If the electric current value is not picked so all the remaining values in the array will exist increased past ane.
Examples:
Input: arr[] = {ten, five, 5, four, nine}, X = 10
Output: 2
Explanation:
Arr = {10, 5, five, 4, 9}, pos = 0
10 is picked
Arr = {10, 4, four, iii, eight}, pos = ane
4 is not picked
Arr = {10, four, v, 4, 9}, pos = 2
5 is not picked
Arr = {10, 4, 5, v, 10}, pos = 3
5 is not picked
Arr = {ten, 4, 5, 5, 11}, pos = iv
11 is picked
Hence two elements are picked.Input: arr[] = {5, 4, 3, 2, 1}, 10 = 4
Output: one
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 10. If the ith value is less than required value then increment the value from (i+i)th to end of the array by one else If the ith value is greater or equal than required value X then subtract the value past i from (i+1)th to end of the array.
Below is the implementation of the higher up approach:
C++
#include <$.25/stdc++.h>
using namespace std;
int increaseDecreaseValue( int arr[],
int 10, int due north)
{
int TotalValue = 0;
for ( int i = 0; i < northward; i++)
{
if (arr[i] < 10)
{
for ( int j = i + one; j < northward; j++)
{
arr[j] += 1;
}
}
else
{
TotalValue += 1;
for ( int j = i + i; j < n; j++)
{
if (arr[j] == 0)
{
proceed ;
}
else
{
arr[j] -= 1;
}
}
}
}
return TotalValue;
}
int primary()
{
int ten = 4;
int arr[] = {v, four, 3, 2, 1};
int north = sizeof (arr) / sizeof (arr[0]);
int countValue = increaseDecreaseValue(arr, x, northward);
cout << countValue;
return 0;
}
Java
import java.util.*;
form GFG
{
static int increaseDecreaseValue( int []arr, int x)
{
int TotalValue = 0 ;
for ( int i = 0 ; i < arr.length; i++)
{
if (arr[i] < 10)
{
for ( int j = i + 1 ; j < arr.length; j++)
{
arr[j] += 1 ;
}
}
else
{
TotalValue += 1 ;
for ( int j = i + ane ; j < arr.length; j++)
{
if (arr[j] == 0 )
{
go on ;
}
else
{
arr[j] -= one ;
}
}
}
}
render TotalValue;
}
public static void main(String[] args)
{
int 10 = 4 ;
int [] arr = { five , 4 , 3 , ii , 1 };
int countValue = increaseDecreaseValue(arr, x);
System.out.println(countValue);
}
}
Python3
def increaseDecreaseValue(arr, x):
TotalValue = 0
for i in range ( len (arr)):
if arr[i] < x:
for j in range (i + one , len (arr)):
arr[j] + = 1
else :
TotalValue + = 1
for j in range (i + 1 , len (arr)):
if arr[j] = = 0 :
continue
arr[j] - = 1
return TotalValue
if __name__ = = "__main__" :
x = 4
arr = [ 5 , 4 , 3 , 2 , ane ]
countValue = \
increaseDecreaseValue(arr, x)
print (countValue)
C#
using System;
class GFG
{
static int increaseDecreaseValue( int []arr, int x)
{
int TotalValue = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (arr[i] < 10)
{
for ( int j = i + 1; j < arr.Length; j++)
{
arr[j] += ane;
}
}
else
{
TotalValue += 1;
for ( int j = i + one; j < arr.Length; j++)
{
if (arr[j] == 0)
{
continue ;
}
else
{
arr[j] -= 1;
}
}
}
}
return TotalValue;
}
public static void Main(String[] args)
{
int 10 = 4;
int [] arr = {five, 4, 3, two, one};
int countValue = increaseDecreaseValue(arr, x);
Panel.WriteLine(countValue);
}
}
Javascript
<script>
function increaseDecreaseValue(arr, x)
{
let TotalValue = 0;
for (let i = 0; i < arr.length; i++)
{
if (arr[i] < x)
{
for (permit j = i + 1; j < arr.length; j++)
{
arr[j] += 1;
}
}
else
{
TotalValue += one;
for (permit j = i + 1; j < arr.length; j++)
{
if (arr[j] == 0)
{
continue ;
}
else
{
arr[j] -= i;
}
}
}
}
return TotalValue;
}
permit x = 4;
let arr = [5, 4, 3, two, ane];
let countValue = increaseDecreaseValue(arr, x);
document.write(countValue);
</script>
Fourth dimension Complexity:
Auxiliary Space: O(ane), no extra space is required, so information technology is a constant.
Efficient Approach:
- This problem can further exist optimized to
. - Here the main thought is to bank check by how much this index value should change.
- This can exist done by using a temporary variable, here it is currentStatus that will keep the net consequence on the current index by the previous decisions.
- The outcome will be added to the value of that alphabetize and that will tell us the updated original value of the array.
Beneath is the implementation of the above approach:
C++
#include <$.25/stdc++.h>
using namespace std;
int increaseDecreaseValue(vector< int > arr, int x)
{
int currentStatus = 0;
int totalValue = 0;
int i;
int len = arr.size();
for (i = 0; i < len; i++) {
if (arr[i] + currentStatus < 10)
currentStatus += 1;
else {
currentStatus -= 1;
totalValue += 1;
}
}
return totalValue;
}
int main()
{
int x = 4;
vector< int > arr = { 5, iv, 3, two, one };
int countValue = increaseDecreaseValue(arr, x);
cout << (countValue);
}
Java
class GFG
{
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++ )
{
if (arr[i] + currentStatus < 10)
currentStatus += ane ;
else
{
currentStatus -= 1 ;
totalValue += i ;
}
}
return totalValue;
}
public static void master (String[] args)
{
int x = 4 ;
int arr[] = { 5 , 4 , 3 , two , one };
int countValue = increaseDecreaseValue(arr, x);
System.out.println(countValue);
}
}
Python3
def increaseDecreaseValue(arr, x):
currentStatus = 0
totalValue = 0
for i in arr:
if i + currentStatus < x:
currentStatus + = 1
else :
currentStatus - = one
totalValue + = ane
render totalValue
if __name__ = = "__main__" :
10 = four
arr = [ 5 , 4 , three , 2 , ane ]
countValue = increaseDecreaseValue(arr, ten)
impress (countValue)
C#
using System;
grade GFG
{
static int increaseDecreaseValue( int []arr,
int ten)
{
int currentStatus = 0;
int totalValue = 0;
int i;
int len = arr.Length;
for (i = 0; i < len ; i++ )
{
if (arr[i] + currentStatus < x)
currentStatus += i;
else
{
currentStatus -= one;
totalValue += 1;
}
}
return totalValue;
}
static public void Principal ()
{
int x = 4;
int []arr = {5, 4, three, 2, 1};
int countValue = increaseDecreaseValue(arr, x);
Console.Write(countValue);
}
}
Javascript
<script>
role increaseDecreaseValue(arr, x)
{
let currentStatus = 0;
allow totalValue = 0;
permit i;
let len = arr.length;
for (i = 0; i < len ; i++ )
{
if (arr[i] + currentStatus < x)
currentStatus += 1;
else
{
currentStatus -= 1;
totalValue += 1;
}
}
render totalValue;
}
let ten = 4;
let arr = [5, 4, iii, 2, i];
let countValue = increaseDecreaseValue(arr, ten);
document.write(countValue);
</script>
Time Complication:
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Source: https://www.geeksforgeeks.org/count-the-values-greater-than-x-in-the-modified-array/

0 Response to "How To Check For Value Greater Than 1 In Ruby"
Post a Comment