Minimum number of colors required to color a Circular Array
Last Updated :
22 Feb, 2023
Given a circular array arr[] containing N integers, the task is to find the minimum number of colors required to color the array element such that two adjacent elements having different values must not be colored the same.
Examples:
Input: arr[] = {1, 2, 1, 1, 2}
Output: 2
Explanation:
Minimum 2 type of colors are required.
We can distribute color as {r, g, r, r, g} such that no adjacent element having different value are colored same.
Input: arr[] = {1, 2, 3, 4}
Output: 2
Explanation:
Minimum 2 type of colors are required.
We can distribute color as {r, g, r, g}.
Approach: This problem can be solved using Greedy Approach.
- If all the values are same then only 1 color is required.
- If there are more than one distinct elements and the total number of elements are even then, 2 colors are required.
- If there are more than one distinct elements and the total number of elements are odd then, check:
- If there exist adjacent elements having the same value, then 2 colors are required.
- Else 3 colors are required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > lst = { 1, 2, 3, 3, 4, 5, 5 };
bool flag1 = true ;
bool flag_adj = false ;
int count = 0;
int count_color = 0;
for ( int i = 0; i < lst.size() - 1; i++) {
if (lst[i] != lst[i + 1])
flag1 = false ;
else
count += 1;
}
if (flag1 == true ) {
count_color = 1;
}
else {
if ((lst.size() - count) % 2 != 0)
count_color = 3;
else
count_color = 2;
}
cout << count_color << "\n" ;
return 0;
}
|
Java
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<Integer> lst = new ArrayList<>();
lst.add( 1 );
lst.add( 2 );
lst.add( 3 );
lst.add( 3 );
lst.add( 4 );
lst.add( 5 );
lst.add( 5 );
boolean flag1 = true ;
int count = 0 ;
int count_color = 0 ;
for ( int i = 0 ; i < lst.size() - 1 ; i++) {
if (lst.get(i) != lst.get(i + 1 ))
flag1 = false ;
else
count += 1 ;
}
if (flag1) {
count_color = 1 ;
}
else {
if ((lst.size() - count) % 2 != 0 )
count_color = 3 ;
else
count_color = 2 ;
}
System.out.println(count_color);
}
}
|
Python3
lst = [ 1 , 2 , 3 , 3 , 4 , 5 , 5 ]
flag1 = True
flag_adj = False
count = 0
for i in range ( len (lst) - 1 ):
if lst[i] ! = lst[i + 1 ]:
flag1 = False
else :
count + = 1
if flag1 = = True :
count_color = 1
else :
if ( len (lst) - count) % 2 :
count_color = 3
else :
count_color = 2
print (count_color)
|
Javascript
<script>
let lst = [1,2,3,3,4,5,5]
let flag1 = true
let flag_adj = false
let count = 0
for (let i = 0; i < lst.length - 1; i++)
{
if (lst[i] != lst[i + 1])
flag1 = false
else
count += 1
}
if (flag1 == true )
count_color = 1
else {
if ((lst.length-count)%2)
count_color = 3
else
count_color = 2
}
document.write(count_color)
</script>
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static void Main( string [] args)
{
List< int > lst = new List< int >() { 1, 2, 3, 3, 4, 5, 5 };
bool flag1 = true ;
int count = 0;
int count_color = 0;
for ( int i = 0; i < lst.Count - 1; i++)
{
if (lst[i] != lst[i + 1])
flag1 = false ;
else
count += 1;
}
if (flag1)
{
count_color = 1;
}
else
{
if ((lst.Count - count) % 2 != 0)
count_color = 3;
else
count_color = 2;
}
Console.WriteLine(count_color);
}
}
|
Time Complexity: O(N), where N is the number of elements.
Auxiliary Space: O(1), as constant space is used.
Share your thoughts in the comments
Please Login to comment...