Note from Author:
Congrats to all the Winners. Glad to see so much participation. Hope to see you all participate in the next biweekly as well. You can contact panda_2500 (Abhijit Mishra) or adityagi02 (Aditya Tyagi) for any queries related to the contest or problem discussion.
I would recommend first looking at the hints for the problem if your facing logical issue, if you are absolutely clear with logic then move on to look at the solution and the code. Efforts will be made towards making code for other languages (Python) available as well.
A — Xeros
Since only entries in array are binary digits, all 0s must be before 1s. So we can use two pointers to interchange 0s to 1s and count the operations. The maintain 2 pointers the left pointer will be looking for the first 1 and the right pointer will be looking for the first 0. if they both don't cross each other, then simply replace. loop ends if left pointer becomes greater than right pointer or they become equal.
// Aur Bhai Dekhne aagaye ;) // Author: Abhijit Mishra
include <bits/stdc++.h>
using namespace std;
define nl cout << "\n"
define all(x) x.begin(), x.end()
define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long int ll; typedef vector vi;
int main() { fast; ll t = 1; cin >> t; while (t--) { ll n; cin >> n; vi a(n); for (int i = 0; i < n; i++) { cin >> a[i]; }
if (is_sorted(all(a))) { cout << 0; } else { ll ans = 0; ll l = 0, r = n - 1; while (l != r) { if (a[l] == 0) { l++; } else if (a[r] == 1) { r--; } else if (a[l] == 1 && a[r] == 0) { a[l] = 0; a[r] = 1; ans++; } } if (is_sorted(all(a))) { cout << ans; } } nl; } return 0;
}
import java.util.*;
public class Rebellion {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0){
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i<n;i++) arr[i] = sc.nextInt();
int p1 = 0;
int p2 = n-1;
int ans = 0;
while(p1<p2){
if(arr[p1] == 0){
p1++;
continue;
}
if(arr[p2] == 1){
p2--;
continue;
}
if(arr[p1] == 1 && arr[p2] == 0){
ans++;
p1++;
p2--;
}
}
System.out.println(ans);
t--;
}
}
}
B — Best Friend Game
[submission:185389438]
C — Happy New Year
[submission:185390659]
D — Favourite Drink
[submission:185391352]
E — Good number
[submission:185393751]
F — Punishment
[submission:185421029]
G — Treasure
[submission:185394097]
H — Count Pairs
[submission:185426903]
I — Transform Array
[submission:185394223]