Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given an array arr[] consisting of N integers, the task is to check if all subarrays of the array
- // have at least one unique element in it or not. If found to be true, then print “Yes”.else print “No”.
- // https://www.geeksforgeeks.org/check-if-all-subarrays-contains-at-least-one-unique-element/
- #include <bits/stdc++.h>
- // #include <atcoder/all>
- using namespace std;
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- typedef pair<int, int> pi;
- #define f first
- #define s second
- #define pb push_back
- #define endl "\n"
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- int dx[] = {-1, 0, 1, 0};
- int dy[] = {0, 1, 0, -1};
- const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- // -----------------------------------------------------------------------------
- vi a;
- bool solve(int l, int r)
- {
- if (l >= r)
- {
- return true;
- }
- map<int, int> mpp;
- for (int i = l; i <= r; i++)
- {
- mpp[a[i]]++;
- }
- // am1, am2, am3,..., amn -> unique elements, divide & conquer in each range
- int last = l;
- bool unique = false;
- for (int i = l; i <= r; i++)
- {
- if (mpp[a[i]] == 1)
- {
- unique = true;
- if (!solve(last, i - 1))
- {
- return false;
- }
- last = i + 1;
- }
- }
- return (unique && (solve(last, r)));
- }
- void solve()
- {
- int n;
- cin >> n;
- a.resize(n);
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- if (solve(0, n - 1))
- {
- yes;
- return;
- }
- no;
- return;
- }
- signed main()
- {
- // __START__;
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- // __END__;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment