# Minimum number of colors required to color a Circular Array

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:2Explanation:

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:2Explanation:

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:

## 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)` `// This code is contributed by shinjanpatra` `</script>` |

**Output:**

2

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