0% found this document useful (0 votes)
100 views

Input Format: T T N N

This document describes a seating arrangement problem where the objective is to find the minimum number of seat exchanges needed to arrange men and women alternately. The input consists of test cases with the number of seats and their occupant identities. The output prints the minimum exchanges required or -1 if not possible. Sample inputs and outputs are provided with explanations of solutions.

Uploaded by

THULASI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Input Format: T T N N

This document describes a seating arrangement problem where the objective is to find the minimum number of seat exchanges needed to arrange men and women alternately. The input consists of test cases with the number of seats and their occupant identities. The output prints the minimum exchanges required or -1 if not possible. Sample inputs and outputs are provided with explanations of solutions.

Uploaded by

THULASI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Seating Arrangement

You’re given a row of seats. Each seat is either occupied by a woman (denoted by xx) or a man (denoted by xy).
The objective is to find the minimum number of seat exchanges required such that the men and women are seated
alternately.
Input Format
The first line of input consists of an integer  t  denoting the number of test cases.  t  test cases follow. Each test
case consists of two lines. The first line of each test case consists of an integer  n  denoting the number of seats.
Second line consists of  n  space separated tokens, each token denoting the occupant of that particular seat.
Output Format
For each test case, print the number of seat exchanges required. If an alternate seating is impossible, print  -1 .
Sample Input

5
3
xx xx xx
2
xx xy
4
xx xx xy xy
8
xx xx xy xy xy xx xx xy
7
xx xy xx xy xx xy xy

Sample Output

-1
0
1
2
3

Constraints
1 <= t <= 1000
1 <= n <= 10000
Explanation
For  xx xx xy xy , one can exchange seat 1 with seat 4 giving  xy xx xy xx  giving an alternate seating
arrangement.

You might also like