## Grooving Monkeys solution

N **monkeys** are invited to a party where they start dancing. They dance in a circular formation, very similar to a Gujarati Garba or a Drum Circle. The dance requires the **monkeys** to constantly change positions after every 1 second.

The change of position is not random & you, in the audience, observe a pattern. Monkeys are very disciplined & follow a specific pattern while dancing.

Consider N = 6, and an array monkeys = {3,6,5,4,1,2}.

This array (1-indexed) is the dancing pattern. The value at monkeys[i], indicates the new of position of the monkey who is standing at the ith position.

Given N & the array monkeys[ ], find the time after which all monkeys are in the initial positions for the 1st time.

**Constraints**

1<=t<=10 (test cases)

1<=N<=10000 (Number of monkeys)

**Input Format**

First-line contains single integer t, denoting the number of test cases.

Each test case is as follows –

Integer N denoting the number of monkeys.

Next line contains N integer denoting the dancing pattern array, monkeys[].

**Output**

t lines,

Each line must contain a single integer T, where T is the minimum number of seconds after which all the monkeys are in their initial position.

**Test Case**

**Explanation :**

## Grooving Monkeys solution [TCS – Codevita]

**Example 1 :**

**Input**

1

6

3 6 5 4 1 2

Output

6

### Explanation

Consider N = 6, and an array monkeys = {3,6,5,4,1,2}.

Suppose monkeys are a,b,c,d,e,f, & Initial position (at t = 0) -> a,b,c,d,e,f

At t = 1 -> e,f,a,d,c,b

a will move to 3rd position, b will move to 6th position, c will move to 5th position, d will move to 4th position, e will move to 1st position and f will move to 2nd position. Thus from a,b,c,d,e,f at t =0, we get e,f,a,d,c,b at t =1. Recursively applying same transpositions, we get following positions for different values of t.

At t = 2 -> c,b,e,d,a,f

At t = 3 -> a,f,c,d,e,b

At t = 4 -> e,b,a,d,c,f

At t = 5 -> c,f,e,d,a,b

At t = 6 -> a,b,c,d,e,f

Since at t = 6, we got the original position, therefore the answer is 6.

Here, we will gonna solve this** grooving monkey problem by using Python** :

### What is CodeVita 2020

Hey coders, CodeVita is an online coding contest coordinated by TATA Consultancy Services. The main aim of this contest is to find passionate coders for their Company.

### Why you should Participate?

Individual encounters of certain understudies who had before qualified Codevita.

- To Compete with the Best Coders in the World.
- To Earn Peer Recognition and Honor.
- To Explore Exciting Career with World’s Most Powerful IT Brand.
- To Showcase your Programming Knowledge.

### FAQ [Grooving Monkeys Solution]

### I have only 7 days left, how to prepare for CodeVita?

Don’t worry keep this in mind “Clear Your basics”.**Tip**: try to solve easy problems from SPOJ, Codechef, Hackerank, etc.

### What is the level of questions being asked in CodeVita?

It depends on your practice you can check last year questions here and analyze your level.

### How to crack CodeVita 2020?

You will even now not be fulfilled and ask some more people (no offense however) “How to crack CodeVita”. Be that as it may, by simply perusing answers you can’t split it. You need to really jump into coding and get your hands dirty.

I hope that this guide helped you in solving your problems. I will try to publish the upcoming TCS CodeVita 2020 problems & solutions.

You may also like: Google Shocking Results

Do not forget to write a clean code in a competitive coding test. this is very necessary for your better understanding and you can solve this grooving monkey problem using python, **Grooving monkeys solution using C++,** and any in other languages too with this same logic.

I hope this Grooving Monkeys Solution helped, clear your issues. Share this article with your friends and comment down if you have any questions. See you in our next article.

Cheers! ðŸ˜‰

## Leave a Reply