Related Articles
Minimum number of colors required to color a Circular Array
• Difficulty Level : Easy
• Last Updated : 12 Jun, 2020

Given a circular array arr[] containing N integers, the task is to find the minimum number of colours required to colour the array element such that two adjacent elements having different values must not be coloured 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}.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: This problem can be solved using Greedy Approach.

1. If all the values are same then only 1 color is required.
2. If there are more than one distinct elements and the total number of elements are even then, 2 colors are required.
3. 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++ implementation of above approach`` ` `#include ``using` `namespace` `std;`` ` `// Function that finds minimum number of``// colors required``void` `colorRequired(``int` `arr[], ``int` `n)``{`` ` `    ``// To check that if all the elements``    ``// are same or not``    ``bool` `all_same = ``true``;`` ` `    ``// To check if only one adjacent exist``    ``bool` `one_adjacent_same = ``false``;`` ` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < n - 1; i++) {`` ` `        ``// If adjacent elements found``        ``// different means all are not``        ``// same``        ``if` `(arr[i] != arr[i + 1]) {``            ``all_same = ``false``;``        ``}`` ` `        ``// If two adjacent elements found``        ``// to be same then make``        ``// one_adjacent_same true``        ``if` `(arr[i] == arr[i + 1]) {``            ``one_adjacent_same = ``true``;``        ``}``    ``}`` ` `    ``// If all elements are same``    ``// then print 1``    ``if` `(all_same == ``true``) {``        ``cout << 1 << endl;``        ``return``;``    ``}`` ` `    ``// If total number of elements are``    ``// even or there exist two adjacent``    ``// elements that are same``    ``// then print 2``    ``if` `(n % 2 == 0``        ``|| one_adjacent_same == ``true``) {``        ``cout << 2 << endl;``        ``return``;``    ``}`` ` `    ``// Else 3 type of colors``    ``// are required``    ``cout << 3 << endl;``}`` ` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 1, 1, 2 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);`` ` `    ``// Function call``    ``colorRequired(arr, n);``    ``return` `0;``}`

## Java

 `// Java implementation of above approach``import` `java.util.*;`` ` `class` `GFG{`` ` `// Function that finds minimum number of``// colors required``static` `void` `colorRequired(``int` `arr[], ``int` `n)``{`` ` `    ``// To check that if all the elements``    ``// are same or not``    ``boolean` `all_same = ``true``;`` ` `    ``// To check if only one adjacent exist``    ``boolean` `one_adjacent_same = ``false``;`` ` `    ``// Traverse the array``    ``for``(``int` `i = ``0``; i < n - ``1``; i++)``    ``{``        ` `       ``// If adjacent elements found``       ``// different means all are not``       ``// same``       ``if` `(arr[i] != arr[i + ``1``]) ``       ``{``           ``all_same = ``false``;``       ``}``        ` `       ``// If two adjacent elements found``       ``// to be same then make``       ``// one_adjacent_same true``       ``if` `(arr[i] == arr[i + ``1``]) ``       ``{``           ``one_adjacent_same = ``true``;``       ``}``    ``}`` ` `    ``// If all elements are same``    ``// then print 1``    ``if` `(all_same == ``true``)``    ``{``        ``System.out.print(``1` `+ ``"\n"``);``        ``return``;``    ``}`` ` `    ``// If total number of elements are``    ``// even or there exist two adjacent``    ``// elements that are same``    ``// then print 2``    ``if` `(n % ``2` `== ``0` `|| ``        ``one_adjacent_same == ``true``)``    ``{``        ``System.out.print(``2` `+ ``"\n"``);``        ``return``;``    ``}`` ` `    ``// Else 3 type of colors``    ``// are required``    ``System.out.print(``3` `+ ``"\n"``);``}`` ` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``1``, ``2``, ``1``, ``1``, ``2` `};``    ``int` `n = arr.length;`` ` `    ``// Function call``    ``colorRequired(arr, n);``}``}`` ` `// This code is contributed by Rohit_ranjan`

## Python3

 `# Python3 implementation of the above approach`` ` `# Function that finds minimum number ``# of colors required``def` `colorRequired(arr, n):`` ` `    ``# To check that if all the elements``    ``# are same or not``    ``all_same ``=` `True`` ` `    ``# To check if only one adjacent exist``    ``one_adjacent_same ``=` `False`` ` `    ``# Traverse the array``    ``for` `i ``in` `range``(n ``-` `1``):`` ` `        ``# If adjacent elements found``        ``# different means all are not``        ``# same``        ``if``(arr[i] !``=` `arr[i ``+` `1``]):``            ``all_same ``=` `False`` ` `        ``# If two adjacent elements ``        ``# found to be same then make``        ``# one_adjacent_same true``        ``if``(arr[i] ``=``=` `arr[i ``+` `1``]):``            ``one_adjacent_same ``=` `True`` ` `    ``# If all elements are same``    ``# then print 1``    ``if``(all_same ``=``=` `True``):``        ``print``(``1``)``        ``return`` ` `    ``# If total number of elements are``    ``# even or there exist two adjacent``    ``# elements that are same``    ``# then print 2``    ``if``(n ``%` `2` `=``=` `0` `or` `one_adjacent_same ``=``=` `True``):``        ``print``(``2``)``        ``return`` ` `    ``# Else 3 type of colors``    ``# are required``    ``print``(``3``)`` ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:`` ` `    ``arr ``=` `[ ``1``, ``2``, ``1``, ``1``, ``2` `]``    ``n ``=` `len``(arr)`` ` `    ``# Function call``    ``colorRequired(arr, n)`` ` `# This code is contributed by Shivam Singh`

## C#

 `// C# implementation of above approach``using` `System;``class` `GFG{`` ` `// Function that finds minimum number of``// colors required``static` `void` `colorRequired(``int` `[]arr, ``int` `n)``{`` ` `    ``// To check that if all the elements``    ``// are same or not``    ``bool` `all_same = ``true``;`` ` `    ``// To check if only one adjacent exist``    ``bool` `one_adjacent_same = ``false``;`` ` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < n - 1; i++)``    ``{``         ` `        ``// If adjacent elements found``        ``// different means all are not``        ``// same``        ``if` `(arr[i] != arr[i + 1]) ``        ``{``            ``all_same = ``false``;``        ``}``             ` `        ``// If two adjacent elements found``        ``// to be same then make``        ``// one_adjacent_same true``        ``if` `(arr[i] == arr[i + 1]) ``        ``{``            ``one_adjacent_same = ``true``;``        ``}``    ``}`` ` `    ``// If all elements are same``    ``// then print 1``    ``if` `(all_same == ``true``)``    ``{``        ``Console.Write(1 + ``"\n"``);``        ``return``;``    ``}`` ` `    ``// If total number of elements are``    ``// even or there exist two adjacent``    ``// elements that are same``    ``// then print 2``    ``if` `(n % 2 == 0 || ``        ``one_adjacent_same == ``true``)``    ``{``        ``Console.Write(2 + ``"\n"``);``        ``return``;``    ``}`` ` `    ``// Else 3 type of colors``    ``// are required``    ``Console.Write(3 + ``"\n"``);``}`` ` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `[]arr = { 1, 2, 1, 1, 2 };``    ``int` `n = arr.Length;`` ` `    ``// Function call``    ``colorRequired(arr, n);``}``}`` ` `// This code is contributed by sapnasingh4991`
Output:
```2
```

Time Complexity: O(N), where N is the number of elements.

My Personal Notes arrow_drop_up