Check if it is possible to color N objects such that for ith object, exactly arr[i] distinct colors are used
Last Updated :
15 Dec, 2021
Given an array arr[] consisting of N positive integers, the task is to check if it is possible to color the N objects such that for ith element of the array there exist exactly arr[i] distinct colors used in coloring all the objects except for the ith object.
Examples:
Input: arr[] = {1, 2, 2}
Output: Yes
Explanation:
One of the possible ways to color is: {“Red”, “blue”, “blue”}.
- For arr[0](=1), there is exactly 1 distinct color, which is “blue”.
- For arr[1](=2), there are exactly 2 distinct colors, which are “blue” and “red”.
- For arr[2](=3), there are exactly 2 distinct colors, which are “blue” and “red”.
Input: arr[] = {4, 3, 4, 3, 4}
Output: No
Approach: The problem can be solved based on the following observations:
- For 2 objects, there are N-2 objects common while calculating the number of distinct colors. Therefore, there can be a difference of at most 1 between the maximum and minimum element of the array arr[].
- Now there are two cases:
- If the maximum and minimum elements are equal, then the answer is “Yes”, only if the element is N – 1 or the element is less than or equal to N/2, because every color can be used more than once.
- If the difference between the maximum and minimum element is 1, then the number of distinct colors in the N object must be equal to the maximum element, because the minimum element is 1 less than the maximum element.
- Now, assuming the frequency of the minimum element as X and the frequency of the maximum element as Y, then the answer is “Yes” if and only if X+1 ≤ A ≤ X+ Y/2 (observation-based).
Follow the steps below to solve the problem:
- First sort the array in ascending order.
- If the difference between arr[N-1] and arr[0] is greater than 1, then print “No“.
- Else, if arr[N-1] is equal to arr[0], then check the following:
- If arr[N-1] = N-1 or 2*arr[N-1] <= N, then print “Yes“.
- Otherwise, print “No“.
- Else, count the frequencies of min and max elements and store them in variables, say X and Y, and then do the following:
- If arr[N-1] is greater than X and arr[N-1] is less than or equal to X+Y/2, then print “Yes“.
- Otherwise, print “No“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string checkValid( int arr[], int N)
{
sort(arr, arr + N);
if (arr[N - 1] - arr[0] > 1)
return "No" ;
else if (arr[N - 1] == arr[0]) {
if (arr[N - 1] == N - 1
|| 2 * arr[N - 1] <= N)
return "Yes" ;
else
return "No" ;
}
else {
int x = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == arr[0])
x++;
}
int y = N - x;
if ((arr[N - 1] >= x + 1)
and (arr[N - 1] <= x + y / 2))
return "Yes" ;
else
return "No" ;
}
}
int main()
{
int arr[] = { 1, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << checkValid(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String checkValid( int arr[], int N)
{
Arrays.sort(arr);
if (arr[N - 1 ] - arr[ 0 ] > 1 )
return "No" ;
else if (arr[N - 1 ] == arr[ 0 ])
{
if (arr[N - 1 ] == N - 1
|| 2 * arr[N - 1 ] <= N)
return "Yes" ;
else
return "No" ;
}
else
{
int x = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == arr[ 0 ])
x++;
}
int y = N - x;
if ((arr[N - 1 ] >= x + 1 ) &&
(arr[N - 1 ] <= x + y / 2 ))
return "Yes" ;
else
return "No" ;
}
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 2 };
int N = arr.length;
System.out.print(checkValid(arr, N));
}
}
|
Python3
def checkValid(arr, N):
arr = sorted (arr)
if (arr[N - 1 ] - arr[ 0 ] > 1 ):
return "No"
elif (arr[N - 1 ] = = arr[ 0 ]):
if (arr[N - 1 ] = = N - 1 or
2 * arr[N - 1 ] < = N):
return "Yes"
else :
return "No"
else :
x = 0
for i in range (N):
if (arr[i] = = arr[ 0 ]):
x + = 1
y = N - x
if ((arr[N - 1 ] > = x + 1 ) and
(arr[N - 1 ] < = x + y / / 2 )):
return "Yes"
else :
return "No"
if __name__ = = '__main__' :
arr = [ 1 , 2 , 2 ]
N = len (arr)
print (checkValid(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string checkValid( int []arr, int N)
{
Array.Sort(arr);
if (arr[N - 1] - arr[0] > 1)
return "No" ;
else if (arr[N - 1] == arr[0]) {
if (arr[N - 1] == N - 1
|| 2 * arr[N - 1] <= N)
return "Yes" ;
else
return "No" ;
}
else {
int x = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == arr[0])
x++;
}
int y = N - x;
if ((arr[N - 1] >= x + 1)
&& (arr[N - 1] <= x + y / 2))
return "Yes" ;
else
return "No" ;
}
}
public static void Main()
{
int []arr = { 1, 2, 2 };
int N = arr.Length;
Console.Write(checkValid(arr, N));
}
}
|
Javascript
<script>
function checkValid(arr, N)
{
arr.sort((a, b) => a - b);
if (arr[N - 1] - arr[0] > 1)
return "No" ;
else if (arr[N - 1] == arr[0])
{
if (arr[N - 1] == N - 1 ||
2 * arr[N - 1] <= N)
return "Yes" ;
else
return "No" ;
}
else
{
let x = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] == arr[0])
x++;
}
let y = N - x;
if ((arr[N - 1] >= x + 1) &&
(arr[N - 1] <= x + y / 2))
return "Yes" ;
else
return "No" ;
}
}
let arr = [ 1, 2, 2 ];
let N = arr.length;
document.write(checkValid(arr, N));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...