Advertisement
pratiyush7

Untitled

Mar 23rd, 2022
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void mainSolve()
  5. {
  6.   int n = 0;
  7.   string s;
  8.   cin >> n >> s;
  9.  
  10.   int n_0 = 0, n_1 = 0;
  11.   for (char c : s)
  12.   {
  13.     if (c == '1')
  14.       n_1++;
  15.     else
  16.       n_0++;
  17.   }
  18.   int ans = 2 * min(n_0, n_1) + ((n_1 == n_0) ? 0 : 1);
  19.   cout << ans << endl;
  20. }
  21.  
  22. int main()
  23. {
  24.   int t;
  25.   cin >> t;
  26.   while (t--)
  27.   {
  28.     mainSolve();
  29.   }
  30.   return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement