Problem Link : Equal Arrays
Category : Math
Contest : December Circuits'23
#include "bits/stdc++.h" using namespace std; #define int long long int #define endl '\n' void solve() { int n, m; cin >> n >> m; deque<int> arr(n); for (int i = 0; i < n; i++) { cin >> arr[i]; } deque<int> brr(m); for (int i = 0; i < m; i++) { cin >> brr[i]; } int ans = 0; while (arr.size() and brr.size()) { int sum1 = arr.front(); arr.pop_front(); int sum2 = brr.front(); brr.pop_front(); ans++; while (sum1 != sum2) { if (sum1 < sum2 and arr.size()) { sum1 += arr.front(); arr.pop_front(); } else if (sum2 < sum1 and brr.size()) { sum2 += brr.front(); brr.pop_front(); } else { break; } } if (sum1 ^ sum2) { cout << -1 << endl; return; } } if (arr.size() or brr.size()) { ans = -1; } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.precision(12); bool FILEIO = 1; if (FILEIO and fopen("in.txt", "r")) { freopen("C3.txt", "r", stdin); } int tc; cin >> tc; for (int tcase = 1; tcase <= tc; tcase++) { solve(); } }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.