Check if Array element can be incremented to X by using given operations
Last Updated :
15 Nov, 2022
Given an array A[] of size N, and an integer X, the following operation can be performed on that array:
- Choose any element, say Y from the array.
- Then every element in the array except Y are incremented.
- In the next step, only the value Y+1 can be chosen and the steps are repeated until Y+1 is not present in the array.
The task is to return “YES” if Y can be made strictly greater than X, otherwise, return “NO”.
Examples :
Input N = 9, A[] = { 2, 2, 1, 3, 4, 2, 1, 2, 2 }, X = 5
Output: YES
Explanation: Choose Y = 2 at index 0, then A becomes {2, 3, 2, 4, 5, 3, 2, 3, 3}
Then Y can only be 2 +1 = 3, so choose Y = 3 at index 1
Then A becomes {3, 3, 3, 5, 6, 4, 3, 4, 4}
Then Y can only be 3 +1 = 4, so choose Y = 4 at index 6
Then A becomes {4, 4, 4, 6, 7, 5, 4, 4, 5}
Then Y can only be 4 +1 = 5, so choose Y = 5 at index 7.
Then A becomes {5, 5, 5, 7, 8, 6, 5, 5, 5}
Then Y can only be 5 +1 = 6, so choose Y = 6 at index 4.
Which is greater than X (6 > 5), so return YES
Input: N = 4, A[] = {2, 3, 4, 1}, X = 4
Output: NO
Approach:
It can be observed in this problem that the maximum value that any element of the array a[i] can achieve would be a[i] + freq[a[i]] – 1, where freq[a[i]] is the frequency of the array element, and a hashing data structure can be used to calculate the frequency of elements of the array.
Follow the steps mentioned below to implement the idea:-
- Create a HashMap and count the frequency of each distinct element by iterating on arr[], and save them in a HashMap.
- Traverse on the map and apply the formula: a[i] + freq[a[i]] – 1 for each pair stored in the map.
- Now, If the maximum value achieved is strictly greater than X, then print “YES” or else print “NO”.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
string find_max( int N, int A[], int X)
{
unordered_map< int , int > map;
for ( int i = 0; i < N; i++) {
int freq = map[A[i]] == 0
? 1
: map[A[i]] + 1;
map[A[i]]=freq;
}
long max_value = 0;
for ( auto it:map) {
int current
= it.first + (it.second - 1);
max_value
= current > max_value ? current : max_value;
}
return max_value > X ? "YES" : "NO" ;
}
int main()
{
int N = 9;
int A[] = { 2, 2, 1, 3, 4, 2, 1, 2, 2 };
int X = 5;
cout<<(find_max(N, A, X))<<endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static String find_max( int N, int [] A, int X)
{
HashMap<Integer, Integer> map
= new HashMap<>();
for ( int i = 0 ; i < N; i++) {
int freq = map.get(A[i]) == null
? 1
: map.get(A[i]) + 1 ;
map.put(A[i], freq);
}
long max_value = 0 ;
for (Map.Entry<Integer, Integer> set :
map.entrySet()) {
int current
= set.getKey() + (set.getValue() - 1 );
max_value
= current > max_value ? current : max_value;
}
return max_value > X ? "YES" : "NO" ;
}
public static void main(String[] args)
{
int N = 9 ;
int [] A = { 2 , 2 , 1 , 3 , 4 , 2 , 1 , 2 , 2 };
int X = 5 ;
System.out.println(find_max(N, A, X));
}
}
|
Python3
def find_max(N, A, X):
map = {}
for i in range (N):
if A[i] in map :
map [A[i]] + = 1
else :
map [A[i]] = 1
max_value = 0
for first, second in map .items():
current = first + second - 1
if current > max_value:
max_value = current
if max_value > X:
return "YES"
else :
return "NO"
N = 9
A = [ 2 , 2 , 1 , 3 , 4 , 2 , 1 , 2 , 2 ]
X = 5
print (find_max(N, A, X))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static String find_max( int N, int [] A, int X)
{
Dictionary< int , int > map
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (map.ContainsKey(A[i])) {
map[A[i]] += 1;
}
else {
map.Add(A[i], 1);
}
}
long max_value = 0;
foreach ( var Set in map)
{
int current = Set.Key + (Set.Value - 1);
max_value
= current > max_value ? current : max_value;
}
return max_value > X ? "YES" : "NO" ;
}
static public void Main()
{
int N = 9;
int [] A = { 2, 2, 1, 3, 4, 2, 1, 2, 2 };
int X = 5;
Console.WriteLine(find_max(N, A, X));
}
}
|
Javascript
<script>
function find_max(N, A, X){
let map = new Map();
for (let i=0;i<N;i++){
if (map.has(A[i])){
map.set(A[i], map.get(A[i])+1);
}
else {
map.set(A[i], 1);
}
}
let max_value = 0;
map.forEach((values, keys)=>{
let current = keys + values - 1;
max_value = current>max_value ? current : max_value;
})
return (max_value>X) ? "YES" : "NO" ;
}
let N = 9;
let A = [ 2, 2, 1, 3, 4, 2, 1, 2, 2];
let X = 5;
document.write(find_max(N, A, X))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...