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++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Given array 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; } // Printing the answer cout << count_color << "\n" ; return 0; } // This code is contributed by Taranpreet |
Java
import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args) { // Given array 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 ; } // Printing the answer System.out.println(count_color); } } |
Python3
# Python code for the above approach 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) // This code is contributed by shinjanpatra </script> |
C#
// C# code for the above approach using System; using System.Collections.Generic; public class Program { public static void Main( string [] args) { // Given array 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; } // Printing the answer Console.WriteLine(count_color); } } |
3
Time Complexity: O(N), where N is the number of elements.
Auxiliary Space: O(1), as constant space is used.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!