Problem Link :
Author: Sakchi Agarwal
Tester: Sparsh Kedia
Editorialist: Ayush Shukla
Difficulty :
Cakewalk
Problem :
You are given N gifts of red colour and M gifts of blue colour. You need to arrange them in such a way that no to adjacent gift are of same colour. Find the number of ways of possible arrangement satisfying above condition.
Explanation :
Lets assume you have X gifts of Red colour. Now lets see in what ways we can place
Y gifts of Blue colour in above specified arrangement.
Case 1 : X = Y :
First possible arrangement is :
R_1,B_1,R_2,B_2,R_3,B_3,....,R_X,B_Y
Second possible arrangement is :
B_1,R_1,B_2,R_2,B_3,R_3,....,B_Y,R_X
There will be no more then 2 arrangements in this case.
Case 2 : X=Y+1 or Y=X+1 :
In this cases there is only one arrangement possible in which first ball will be the ball with higher number and second will be ball with lower number. The last ball will be the ball with higher number. Example can be
R_1,B_1,R_2,B_2,R_3,B_3,....,R_X,B_Y,R_{X+1}
or
B_1,R_1,B_2,R_2,B_3,R_3,....,B_Y,R_X,B_{Y+1}
So in this case where the number of occurrence differs by 1, the possible ways will be 1 only.
Case 3 : Other than above
There will be no possible ways to have such arrangements. This can be proved as if X < Y then there is X+1 space to fill with other ball. Now above two cases are false it is always true that one space is to be filled by two same colour ball.
There are 0 ways possible.
SOLUTIONS :
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
long long int n,m;
cin>>n>>m;
if(n==m){
cout<<"2\n";
}
else if(n==m+1||m==n+1){
cout<<"1\n";
}
else{
cout<<"0\n";
}
}
return 0;
}
Tester's Solution
import math
t = int(input())
while t>0 :
n,m = [int(x) for x in input().split()]
if(n==m):
print(2)
elif(abs(n-m)==1):
print(1)
else:
print(0)
t = t-1
Feel free to share your approach. Happy Coding