**this was a practice question for Ajira Tech. can anyone solve this question** --------------------------------------------------------------------------------

Red and Green Balls You have a square grid (NxN). Each cell of the grid has either a red ball or a green ball. Your job is to arrange the balls in such a way that all the red balls are either on or below the main diagonal. The main diagonal starts from cell 1x1 and ends at cell NxN. You have only one move which is to swap adjacent rows. You need to achieve the final arrangement in minimal number of moves. If it is not possible to come to a resolution by swapping then print -1 Input: First line of input is the number of rows in grid. Rest are the lines in the grid Ouput: Minimum number of moves

```
Input
2
RG
RR
Output
0
```

```
Input 2
GR
RG
Output
1
```

Auto comment: topic has been updated by Xy9px (previous revision, new revision, compare).Just store the last index in every row in an array and perform bubble sort on the array and count number of swaps. If after sorting value of index is greater than index then output -1 otherwise number of swaps.

last index of what

Last index of occurrence of 'R' in each row. For example, for input such as:

we will store values as {0,1,2,2,1} now we will perform bubble sort on this array to count number of swaps. The idea of bubble sort comes from "swap adjacent rows".

If after sorting value of index is greater than index then output -1 how to do this i dnt understand this

this gives 4 but the answer should be 6

this is wt i came up with

This is my program. It got accepted. ~~~~~

~~~~~

Xy9px First of all you should always put a link of the task, so people could know if it's from an ongoing contest (since Goku submitted it somewhere, and already gave you a solution, I'll assume it's ok). The example you gave us was 5x4 instead of 5x5 so I don't know what's problem with that either. Your code lacks checking whether it's impossible to arrange in such a way (checking whether there's v[I]>I after sorting should suffice)

sry for that test case. its was a practice problem not an ongoing contest

here is the test case.