A teacher in a class with N students has noticed that some students have formed their own groups and hence prevented intermingling of students. To get those students to mix with each other, the teacher has decided a seating pattern.
The seating pattern is very simplistic viz. every boy should sit next to a girl and every girl should next to a boy. They are all seated in one line. It is also mandatory that no two boys sit together, and no two girls sit together.
Your task is to make the above happen with minimum number of swaps between as-is situation to desired situation.
Constraints:
0 <= N <= 50
Number of boys and girls can be equal or at the most differ by 1.
Input:
Input consists of a single string of length N.
String comprises of characters ‘B’ and ‘G’, where B denotes a Boy and G denotes a Girl.
Single integer S which is the minimum number of swaps required to make boys and girls sit alternately.
Time Limit (secs):
1
Example1:
Input:
GGBBG
Output
1
Explanation:
The as-is state is GGBBG. The desired state is GBGBG. If Girl in seat 2 is swapped with Boy in seat 3 then the desired result is achieved in 1 swap. Hence, Output is 1.
#include <iostream>
#include <string>
using namespace std;
// Function to count the number of mismatches between characters 'c' and the string 's' in even positions.
int count(string& s, char c) {
int miss = 0;
for (int i = 0; i < (int)s.length(); i += 2) {
if (s[i] != c) miss++;
}
return miss;
}
// Function to calculate the minimum number of swaps required to achieve the desired seating pattern.
int miniSwaps(string s) {
int cnt0 = 0, cnt1 = 0;
// Count the number of '0's and '1's in the string.
for (char c : s) {
if (c == '0') cnt0 += 1;
else cnt1 += 1;
}
// Check if the absolute difference between counts is greater than 1.
if (abs(cnt0 - cnt1) > 1)
return -1;
// If '0's are more than '1's, count the swaps needed to make all '0's sit next to '1's.
if (cnt0 > cnt1)
return count(s, '0');
// If '1's are more than '0's, count the swaps needed to make all '1's sit next to '0's.
if (cnt0 < cnt1)
return count(s, '1');
// If the counts are equal, return the minimum of swaps needed for both '0' and '1'.
return min(count(s, '0'), count(s, '1'));
}
void solve() {
string s, bin;
cin >> s;
bin = "";
// Convert 'G' to '0' and 'B' to '1' in the input string.
for (int i = 0; i < (int)s.length(); i++) {
if (s[i] == 'G') bin += '0';
else bin += '1';
}
// Call the miniSwaps function and print the result.
cout << miniSwaps(bin);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
def count(s, c):
miss = 0
for i in range(0, len(s), 2):
if s[i] != c:
miss += 1
return miss
def mini_swaps(s):
cnt0 = s.count('0')
cnt1 = s.count('1')
if abs(cnt0 - cnt1) > 1:
return -1
if cnt0 > cnt1:
return count(s, '0')
if cnt0 < cnt1:
return count(s, '1')
return min(count(s, '0'), count(s, '1'))
def solve():
s = input()
bin_str = ""
for char in s:
if char == 'G':
bin_str += '0'
else:
bin_str += '1'
print(mini_swaps(bin_str))
import java.util.Scanner;
public class Main {
public static int count(String s, char c) {
int miss = 0;
for (int i = 0; i < s.length(); i += 2) {
if (s.charAt(i) != c) {
miss++;
}
}
return miss;
}
public static int miniSwaps(String s) {
int cnt0 = 0, cnt1 = 0;
for (char c : s.toCharArray()) {
if (c == '0') {
cnt0 += 1;
} else {
cnt1 += 1;
}
}
if (Math.abs(cnt0 - cnt1) > 1) {
return -1;
}
if (cnt0 > cnt1) {
return count(s, '0');
}
if (cnt0 < cnt1) {
return count(s, '1');
}
return Math.min(count(s, '0'), count(s, '1'));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
String bin = "";
for (char c : s.toCharArray()) {
if (c == 'G') {
bin += '0';
} else {
bin += '1';
}
}
System.out.println(miniSwaps(bin));
}
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count(char* s, char c) {
int miss = 0;
for (int i = 0; i < strlen(s); i += 2) {
if (s[i] != c) {
miss++;
}
}
return miss;
}
int miniSwaps(char* s) {
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '0') {
cnt0 += 1;
} else {
cnt1 += 1;
}
}
if (abs(cnt0 - cnt1) > 1) {
return -1;
}
if (cnt0 > cnt1) {
return count(s, '0');
}
if (cnt0 < cnt1) {
return count(s, '1');
}
return (count(s, '0') < count(s, '1')) ? count(s, '0') : count(s, '1');
}
int main() {
char s[51], bin[51];
scanf("%s", s);
strcpy(bin, "");
for (int i = 0; i < strlen(s); i++) {
if (s[i] == 'G') {
strcat(bin, "0");
} else {
strcat(bin, "1");
}
}
printf("%dn", miniSwaps(bin));
return 0;
}
Leave a Reply