Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions program/CPP/FlipCards.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
//Flip the cards
/* Simple solution to flip the cards Code Chef problem
https://www.codechef.com/submit/FLIPCARDS
*/
#include <iostream>
using namespace std;

/*
Problem: Flip the Cards (CodeChef - FLIPCARDS)
-----------------------------------------------
There are N cards, X of them face up and (N - X) face down.
In one operation, you can flip all cards — turning face up cards face down and vice versa.
You want all cards to face the same side (all up or all down).

We need the minimum number of cards to flip manually.

Logic:
- If X == 0 or X == N → already same side → output 0
- If X <= N/2 → better to flip X face-up cards
- Otherwise → better to flip (N - X) face-down cards
*/

int main() {

int t,n,x;
cin>>t;
if(t>=1 && t<=5000)
{ for(int i=0;i<t;i++)
{
cin>>n>>x;
if(n>=2 && n<=100 && x>=0 && x<=n)
{
if(x==0 || x==n)
cout<<0<<endl;
else if(x<=n/2)
cout<<x<<endl;
else
cout<<n-x<<endl;
}
}

}
return 0;
ios::sync_with_stdio(false);
cin.tie(nullptr);

int t;
cin >> t;

while (t--) {
int n, x;
cin >> n >> x;

// Validate constraints (optional for CodeChef)
if (n < 2 || n > 100 || x < 0 || x > n) continue;

int result = min(x, n - x); // minimal flips required
cout << result << "\n";
}

return 0;
}