Check if N indices of given Array can be colored by M colors using one color at most K times
Last Updated :
01 Feb, 2022
Find an arrangement of M colors for N indices such that no two adjacent indices have same color and each color can be used at most K times. If no such arrangement exists, output -1.
Examples:
Input: N = 6, M = 4, K = 2
Output: 1 2 3 4 1 2
Explanation: Color 1 is assigned to the 1-st index.
Color 2 is assigned to the 2-nd index, color 3 to the 3-rd index,
color 4 to the 4-th index.
Again, color 1 is assigned to 5-th index and color 2 to 6-th index.
Input: N = 20, M = 6, K = 3
Output: -1
Explanation: No such arrangement exists in which 6 colors may be assigned to at most 3 indices.
Approach: Observe the following points:
- If the number of colors is only 1 and the number of indices is greater than 1, then no such assignment can exist.
- If the total number of indices is greater than what can be colored by the colors available (M*K), then no such assignment exists.
Follow the steps below to solve the above problem:
- If the number of colors is 1 and the number of indices is greater than 1, then output -1.
- If the total number of colors is greater than what can be colored by the available colors (M*K), then output -1.
- If both of the above conditions do not meet, then:
- Initialize variable x with 1.
- Run a loop till N and within the loop, output x. Increment x and check if x is greater than M. If x becomes greater than M, set x = 1.
- When x is set to 1, the loop again starts printing the number of colors from 1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void findAssignment( int N, int M, int K)
{
if (M == 1 && N > 1) {
cout << -1;
}
else if (N > M * K) {
cout << -1;
}
else {
int x = 1;
for ( int i = 0; i < N; ++i) {
cout << x << " " ;
x++;
if (x > M) {
x = 1;
}
}
}
}
int main()
{
int N = 6, M = 4, K = 2;
findAssignment(N, M, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void findAssignment( int N, int M, int K)
{
if (M == 1 && N > 1 ) {
System.out.print( "-1" );
}
else if (N > M * K) {
System.out.print( "-1" );
}
else {
int x = 1 ;
for ( int i = 0 ; i < N; ++i) {
System.out.print(x + " " );
x++;
if (x > M) {
x = 1 ;
}
}
}
}
public static void main (String[] args)
{
int N = 6 , M = 4 , K = 2 ;
findAssignment(N, M, K);
}
}
|
Python3
def findAssignment(N, M, K):
if (M = = 1 and N > 1 ):
print ( - 1 )
elif (N > M * K) :
print ( - 1 )
else :
x = 1 ;
for i in range (N):
print (x, end = " " );
x + = 1
if (x > M):
x = 1 ;
N = 6
M = 4
K = 2
findAssignment(N, M, K);
|
C#
using System;
class GFG
{
static void findAssignment( int N, int M, int K)
{
if (M == 1 && N > 1) {
Console.Write(-1);
}
else if (N > M * K) {
Console.Write(-1);
}
else {
int x = 1;
for ( int i = 0; i < N; ++i) {
Console.Write(x + " " );
x++;
if (x > M) {
x = 1;
}
}
}
}
public static void Main()
{
int N = 6, M = 4, K = 2;
findAssignment(N, M, K);
}
}
|
Javascript
<script>
function findAssignment(N, M, K)
{
if (M == 1 && N > 1) {
document.write(-1)
}
else if (N > M * K) {
document.write(-1)
}
else {
let x = 1;
for (let i = 0; i < N; ++i) {
document.write(x + " " );
x++;
if (x > M) {
x = 1;
}
}
}
}
let N = 6, M = 4, K = 2;
findAssignment(N, M, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...