so ... I will be sharing all solutions here (recent solutions at the top, or find by ctrl+f)
900 Rated Problems Link : ProblemSet
Video Explanations : Check Here
100. Little Elephant and Rozdil
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- ll n; cin>>n;
- pair<ll,ll> a[n];
- for(int i=1; i<=n; i++){
- cin>>a[i].first;
- a[i].second = i;
- }
- sort(a+1,a+n+1);
- if(a[1].first==a[2].first) cout<<"Still Rozdil";
- else cout<<a[1].second;
- }
99. BAN BAN
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- cout<<n/2 + n%2<<endl;
- int l=1, r=3*n;
- while(l<r){
- cout<<l<<" "<<r<<endl;
- l += 3;
- r -= 3;
- }
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- vector<int> p(n), q(n);
- for(int i=0; i<n; i++) cin>>p[i];
- for(int i=0; i<n; i++) cin>>q[i];
- vector<vector<int>> inv;
- for(int i=0; i<n; i++)
- inv.push_back({p[i]-1+q[i]-1, p[i], q[i]});
- sort(inv.begin(), inv.end());
- for(auto i: inv) cout<<i[1]<<" ";
- cout<<endl;
- for(auto i: inv) cout<<i[2]<<" ";
- cout<<endl;
- }
- }
97. Burenka Plays with Fractions
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll a, b, c, d;
- cin>>a>>b>>c>>d;
- ll x = a*d, y = b*c;
- if(x==y) cout<<0<<endl;
- else if(x==0 || y==0 || x%y==0 || y%x==0) cout<<1<<endl;
- else cout<<2<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int n, m; cin>>n>>m;
- ll a[n],s1[n], s2[n];
- for(int i=1; i<=n; i++) cin>>a[i];
- for(int i=1; i<=n; i++){
- s1[i]=s1[i-1];
- if(a[i]<a[i-1]) s1[i]+=a[i-1]-a[i];
- }
- for(int i=n; i>=1; i--){
- s2[i]=s2[i+1];
- if(a[i+1]>a[i]) s2[i]+=a[i+1]-a[i];
- }
- while(m--){
- int s,t;
- cin>>s>>t;
- if(s<t) cout<<s1[t]-s1[s]<<endl;
- else cout<<s2[t]-s2[s]<<endl;
- }
- }
95. Cookies
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int c, sum=0, even=0, odd=0;
- for(int i=0; i<n; i++) {
- cin>>c;
- sum+=c;
- if(c%2==0) even++;
- else odd++;
- }
- if(sum%2==0) cout<<even;
- else cout<<odd;
- }
94. Contest
- #include<bits/stdc++.h>
- using namespace std;
- int score(int p, int t){
- return max(3*p/10, p - p*t/250);
- }
- int main(){
- int a,b,c,d;
- cin>>a>>b>>c>>d;
- int Misha = score(a,c), Vasya=score(b,d);
- if(Misha==Vasya) cout<<"Tie";
- else if(Misha<Vasya) cout<<"Vasya";
- else cout<<"Misha";
- return 0;
- }
OR
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int a,b,c,d;
- cin>>a>>b>>c>>d;
- a = max(3*a/10, a - a*c/250);
- b = max(3*b/10, b - b*d/250);
- if(a == b) cout<<"Tie";
- else if(a<b) cout<<"Vasya";
- else cout<<"Misha";
- return 0;
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll n; cin>>n;
- ll a[n];
- map<ll, ll> m;
- for(int i=0; i<n; i++) {
- cin>>a[i];
- m[a[i]]++;
- }
- sort(a,a+n);
- if(m.size()==1) cout<<n*(n-1)<<endl;
- else cout<<2*m[a[0]]*m[a[n-1]]<<endl;
- }
- }
92. Quick Sort
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- ll t; cin>>t;
- while(t--){
- int n,k; cin>>n>>k;
- vector<int> a(n);
- for(int i=0; i<n; i++) cin>>a[i];
- int num=1, total=0;
- for(int j=0; j<n; j++)
- if(a[j]==num) num++;
- else total++;
- int ans = (total+k-1)/k;
- cout<<ans<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- ll t; cin>>t;
- while(t--){
- int n, pos=0; string s; cin>>n>>s;
- for(int i=1; i<n; i++)
- if(s[i]=='?')
- if(s[i-1]=='R') s[i]='B';
- else if(s[i-1]=='B') s[i]='R';
- for(int i=n-2; i>=0; i--)
- if(s[i]=='?')
- if(s[i+1]=='R') s[i]='B';
- else if(s[i+1]=='B') s[i]='R';
- if(s[0]=='?')
- for(int i=0; i<n; i++)
- if(i%2) s[i]='B';
- else s[i]='R';
- cout<<s<<endl;
- }
- }
90. Rudolf and the Ugly String
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; string s;
- cin>>n>>s;
- int cnt=0;
- for(int i=0; i<n; i++){
- if(s.substr(i,3) == "map" || s.substr(i,3)=="pie") cnt++;
- if(s.substr(i,5) == "mapie") cnt--;
- }
- cout<<cnt<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- void solve(){
- int n; cin>>n;
- ll a[n], pos=-1, sum=0, zero=0;
- for(int i=0; i<n; i++) cin>>a[i];
- for(int i=0; i<n-1; i++) if(a[i]!=0) {pos=i; break;}
- if(pos==-1) { cout<<0<<endl; return; }
- for(int i=pos; i<n-1; i++){
- sum += a[i];
- if(a[i]==0) zero++;
- }
- cout<<sum+zero<<endl;
- }
- int main(){
- int t; cin>>t;
- while(t--){
- solve();
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- string a, b;
- cin>>a>>b;
- int ans = a.size() + b.size();
- while(a.size() && b.size() && a.back() == b.back()){
- ans = ans - 2;
- a.pop_back();
- b.pop_back();
- }
- cout<<ans;
- return 0;
- }
87. AB Balance
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- string s; cin>>s;
- s[s.size()-1] = s[0];
- cout<<s<<endl;
- }
- }
86. GCD Problem
- #include<bits/stdc++.h>
- using namespace std;
- void solve(){
- int n; cin>>n;
- if(n%2==0) cout<<"2 "<<(n-3)<<" 1\n";
- else{
- int mid = (n-1)/2;
- if(mid%2==0) cout<<mid-1<<" "<<mid+1<<" "<<1<<'\n';
- else cout<<mid-2<<" "<<mid+2<<" "<<1<<endl;
- }
- }
- int main(){
- int t; cin>>t;
- while(t--) solve();
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- if(n%2==1) {cout<<"7"; n-=3;}
- for(int i=0; i<n/2; i++) cout<<"1";
- cout<<endl;
- }
- }
84. Duff and Meat
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int a, p, p_min = INT_MAX, sum=0;
- for(int i=0; i<n; i++){
- cin>>a>>p;
- p_min = min(p, p_min);
- sum += a * p_min;
- }
- cout<<sum;
- }
83. Three Indices
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- vector<int> a(n);
- for(auto &x : a) cin>>x;
- int flag=0;
- for(int i=1;i<n-1;i++){
- if(a[i-1] < a[i] and a[i] > a[i+1]){
- flag = 1;
- cout<<"YES\n"<<i<<" "<<i+1<<" "<<i+2<<endl;
- break;
- }
- }
- if(flag==0) cout<<"NO\n";
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll n, i=1; cin>>n;
- while(n%i==0) i++;
- cout<<i-1<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- int n,a; cin>>n;
- ll sum=0;
- for(int i=0; i<n; i++){
- cin>>a;
- sum+=a;
- }
- ll r = sum % n;
- cout<<r*(n-r)<<endl;
- }
- }
80. Good Arrays
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- vector<ll> a(n);
- ll sum = 0;
- for(int i=0; i<n; i++) cin>>a[i], sum+=a[i];
- for(int i=0; i<n; i++) {
- if(a[i]==1) sum -= 2;
- else sum -= 1;
- }
- if(sum<0 || n==1) cout<<"NO\n";
- else cout<<"YES\n";
- }
- }
79. Three Threadlets
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll a, b, c; cin>>a>>b>>c;
- //to get min value in a
- if(b<a) swap(a,b);
- if(c<a) swap(a,c);
- //3 cases
- if(a==b && b==c) cout<<"YES\n";
- else if(b%a==0 && c%a==0 && (b/a-1)+ (c/a-1)<=3)
- cout<<"YES\n";
- else cout<<"NO\n";
- }
- }
78. Cubes Sorting
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- int a[n];
- bool flag = 0;
- for(int i=0; i<n; i++) cin>>a[i];
- for(int i=1; i<n; i++){
- if(a[i]>=a[i-1]) {
- flag=1; break;
- }
- }
- cout<<(flag?"YES":"NO")<<endl;
- }
- }
77. MKnez's ConstructiveForces Task
- #include<bits/stdc++.h>
- using namespace std;
- void solve(){
- int n; cin>>n;
- if(n==3) {cout<<"NO"<<endl; return;}
- cout<<"YES"<<endl;
- if(n%2==0){
- for(int i=0; i<n; i+=2) cout<<"1 -1 ";
- cout<<endl;
- } else {
- int k = n/2;
- for(int i=0; i<n-1; i+=2)
- cout<<k-1<<' '<<-k<<' ';
- cout<<k-1<<endl;
- }
- }
- int main(){
- int t; cin>>t;
- while(t--){
- solve();
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll x,y; cin>>x>>y;
- if(x-y == 1) cout<<"NO"<<endl;
- else cout<<"YES"<<endl;
- }
- }
75. Alarm Clock
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll a, b, c, d; cin>>a>>b>>c>>d;
- ll sleep = -1;
- if(b>=a) sleep = b;
- else {
- if(d>=c) sleep=-1;
- else sleep = b + c*((a-b+c-d-1)/(c-d));
- }
- cout<<sleep<<endl;
- }
- }
74. Chips Moving
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int odd=0;
- for(int i=0; i<n; i++){
- int x; cin>>x;
- odd += x & 1;
- }
- cout<<min(odd, n-odd)<<endl;
- }
73. Maximums
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int x=0;
- for(int i=0; i<n; i++){
- int b; cin>>b;
- int a = b + x;
- x = max(x,a);
- cout<<a<<' ';
- }
- cout<<endl;
- }
72. Chemistry
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n, k; cin>>n>>k;
- string s; cin>>s;
- map<char,int> m;
- for(int i=0; i<n; i++) m[s[i]]++;
- int count=0;
- for(int i='a'; i<='z'; i++)
- if(m[i]%2) count++;
- if(count>k+1) cout<<"NO"<<endl;
- else cout<<"YES"<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- map<char, int> freq;
- string s = "";
- for(int i=0; i<n; i++){
- int a; cin>>a;
- for(char c='a'; c<='z'; c++)
- if(freq[c]==a){
- s.push_back(c);
- freq[c]++;
- break;
- }
- }
- cout<<s<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- vector<int> a(n);
- for(auto &i: a) cin>>i;
- int ans=0, flag=1;
- for(int i=n-2; i>=0; i--){
- while(a[i]>=a[i+1] && a[i]>0){
- a[i]/=2;
- ans++;
- }
- if(a[i]==a[i+1])
- {cout<<-1<<endl; flag=0; break;}
- }
- if(flag) cout<<ans<<endl;
- }
- }
69. Long Comparison
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- double a, b, c, d;
- cin>>a>>b>>c>>d;
- double x = log10(a) + b, y = log10(c) + d;
- if(abs(x - y) <= 1e-7) cout<<"="<<endl;
- else if(x>y) cout<<">"<<endl;
- else cout<<"<"<<endl;
- }
- }
68. Odd Grasshopper
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main(){
- int t; cin>>t;
- while(t--){
- ll x, n; cin>>x>>n;
- ll ans = x;
- if(x%2==0){
- if(n%4==1) ans-=n;
- else if(n%4==2) ans++;
- else if(n%4==3) ans += n+1;
- }
- else{
- if(n%4==1) ans+=n;
- else if(n%4==2) ans--;
- else if(n%4==3) ans -= n+1;
- }
- cout<<ans<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- long long int n, x, y, _max=0, _min=0;
- cin>>n>>x;
- for(int i=0; i<n; i++){
- cin>>y;
- _min += y;
- _max += (y + x - 1)/x;
- }
- cout<<(_min + x - 1)/x<<" "<<_max<<endl;
- }
- }
66. Wizard of Orz
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- string s = "989";
- if(n==1) cout<<"9"<<endl;
- if(n==2) cout<<"98"<<endl;
- if(n>=3){
- cout<<s;
- for(int i=0; i<n-3; i++)
- cout<<i%10;
- cout<<endl;
- }
- }
- }
65. Tricky Sum
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- long long int n, s, pow2=1;
- cin>>n;
- s = n*(n+1)/2;
- while(pow2<=n)
- s -= pow2*2, pow2 *= 2;
- cout<<s<<endl;
- }
- }
64. Permutation Sort
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t;
- cin >> t;
- while(t--){
- int n;
- cin >> n;
- vector<int> a(n);
- for(int &i : a)
- cin >> i;
- int ans = 2;
- if (is_sorted(a.begin(), a.end()))
- ans = 0;
- else if (a[0] == 1 || a[n - 1] == n)
- ans = 1;
- else if (a[0] == n && a[n - 1] == 1)
- ans = 3;
- cout << ans << endl;
- }
- return 0;
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- string s; cin>>s;
- int n = s.size();
- int ans=INT_MAX;
- for(int i=0; i<n; i++){
- for(int j=i+1; j<n; j++){
- int no = s[i]-'0';
- no = no*10 + s[j]-'0';
- if(no%25==0)
- ans = min(ans, j-i-1+n-j-1);
- }
- }
- cout<<ans<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- cout<<n<<" "<<0<<" "<<0<<endl;
- }
61. Sending Messages
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- long long int n, f, a, b, temp=0;
- cin>>n>>f>>a>>b;
- for(int i=0; i<n; i++){
- long long int val;
- cin>>val;
- f -= min((val-temp)*a,b); //per second cost = a
- temp = val; //restart cost = b
- }
- cout<<(f>0?"YES":"NO")<<endl;
- }
- }
60. Unnatural Language Processing
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t,n;
- cin>>t;
- while(t--){
- string s; cin>>n>>s;
- cout<<s[0];
- for(int i=1; i<n; i++){
- if((s[i+1]=='a'|| s[i+1]=='e'))
- cout<<".";
- cout<<s[i];
- }
- cout<<endl;
- }
- }
59. Devu, the Singer and Churu, the Joker
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, d, sum = 0;
- cin>>n>>d;
- int a[n];
- for(int i=0; i<n; i++) cin>>a[i], sum+=a[i];
- if(sum + (n-1)*10 > d) cout<<-1<<endl;
- else cout<< (d-sum)/5<<endl;
- }
58. Shifting Stacks
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- vector<int> a(n);
- for(auto &i:a) cin>>i;
- long long sum = 0, req = 0, flag=0;
- for(int i=0; i<n; i++){
- req += i;
- sum += a[i];
- if(sum<req) flag=1;
- }
- cout<<(flag==0?"YES\n":"NO\n");
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- string s; cin>>s;
- int ans = 1, cnt = 1;
- for(int i=1; i<n; i++){
- if(s[i]!=s[i-1]) cnt=1;
- else cnt++;
- ans = max(ans, cnt);
- }
- cout<<ans+1<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n,k; cin>>n>>k;
- int ans[n], b[n];
- pair<int, int> a[n];
- for(int i=1; i<=n; i++){
- cin>>a[i].first;
- a[i].second = i;
- }
- for(int i=1; i<=n; i++)
- cin>>b[i];
- sort(a+1, a+n+1);
- sort(b+1, b+n+1);
- for(int i=1; i<=n; i++)
- ans[a[i].second] = b[i];
- for(int i=1; i<=n;i++)
- cout<<ans[i]<<" ";
- cout<<endl;
- }
- }
55. Promo
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n,q; cin>>n>>q;
- long long int a[n],b[n];
- for(int i=1; i<=n; i++) cin>>a[i];
- sort(a+1,a+n+1,greater<int>());
- for(int i=1; i<=n; i++)
- b[i]=b[i-1] + a[i];
- while(q--){
- int x, y; cin>>x>>y;
- y = x-y;
- cout<<b[x]-b[y]<<endl;
- }
- }
54. Yet Another Tetris Problem
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- vector<int> a(n);
- for(int i=0; i<n; i++) cin>>a[i];
- bool possible=true;
- for(int i=0; i<n; i++)
- if(a[i]%2!=a[0]%2)
- possible=false;
- cout<<(possible?"YES":"NO")<<endl;
- }
- }
- #include<bits/stdc++.h>
- #define ll long long
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- ll n, k, x;
- cin>>n>>k>>x;
- ll total = n*(n+1)/2;
- ll min_sum = k*(k+1)/2;
- ll max_sum = total - (n-k)*(n-k+1)/2;
- if(x>= min_sum and x<=max_sum)
- cout<<"YES"<<endl;
- else cout<<"NO"<<endl;
- }
- }
52. Decoding
- #include<bits/stdc++.h>
- using namespace std;
- int main() {
- int n; cin >> n;
- string s; cin >> s;
- if (n & 1) {
- for (int i = n - 1; i >= 0; i--) {
- if (i % 2 == 1) cout << s[i];
- }
- for (int i = 0; i < n; i++) {
- if (i % 2 == 0) cout << s[i];
- }
- } else {
- for (int i = n - 1; i >= 0; i--) {
- if (i % 2 == 0) cout << s[i];
- }
- for (int i = 0; i < n; i++) {
- if (i % 2 == 1) cout << s[i];
- }
- }
- return 0;
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int a[n], mx=INT_MIN, mn=INT_MAX;
- for(int i=0; i<n; i++){
- cin>>a[i];
- mx = max(mx,a[i]);
- mn = min(mn, a[i]);
- }
- int cnt_max=0, cnt_min=0;
- for(int i=0; i<n; i++) {
- if(a[i]==mx) cnt_max++;
- if(a[i]==mn) cnt_min++;
- }
- if(mx==mn) cout<<0;
- else cout<<(n - cnt_max - cnt_min);
- return 0;
- }
50. Exciting Bets
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int t; cin>>t;
- while(t--){
- ll a,b;
- cin>>a>>b;
- if(a==b) cout<<0<<" "<<0<<'\n';
- else{
- ll x = abs(a-b);
- ll y = a%x;
- y = min(y, x-y);
- cout<<x<<" "<<y<<'\n';
- }
- }
- }
49. Orac and Factors
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- long long int n,k;
- cin>>n>>k;
- if(n%2==0) cout<<n+2*k<<endl;
- else {
- int d;
- for(int i=n; i>2; i--)
- if(n%i==0) d = i;
- cout<<n+d+ 2*(k-1)<<endl;
- }
- }
- }
48. The Corridor or There and Back Again
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- int k=INT_MAX, room, timer;
- vector<pair<int, int>> v;
- for(int i=0; i<n; i++){
- cin>>room>>timer;
- v.push_back({room, timer});
- }
- for(int i=0; i<v.size(); i++)
- k = min(k, v[i].first + (v[i].second-1)/2);
- cout<<k<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- long long int z=0, neg=0, c=0;
- while(n--){
- long long int a; cin>>a;
- if(a==0) z++;
- else if(a>0) c+= a-1;
- else c+= -a-1, neg++;
- }
- if(z) c += z;
- else if(neg%2==1) c+=2;
- cout<<c<<endl;
- }
46. Odd Queries
- #include<bits/stdc++.h>
- using namespace std;
- long long n, q, t, a[200005], pref[200005];
- int main(){
- cin>>t;
- while(t--){
- cin>>n>>q;
- for(int i=1;i<=n; i++){
- cin>>a[i];
- pref[i] = pref[i-1]+a[i];
- }
- for(int i=0;i<q; i++){
- long long l,r,k; cin>>l>>r>>k;
- long long sum = pref[l-1] + (r-l+1)*k + pref[n]-pref[r];
- if(sum%2==1) cout<<"YES"<<endl;
- else cout<<"NO"<<endl;
- }
- }
- }
45. Dominant Piranha
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n, mx=0; cin>>n;
- vector<int> a(n);
- for(auto &p:a) {cin>>p; mx=max(mx,p);}
- int idx=-1;
- for(int i=0; i<n; i++){
- if(a[i]!=mx) continue;
- if(i>0 && a[i-1]!=mx || i<n-1 && a[i+1]!=mx) idx=i+1;
- }
- cout<<idx<<endl;
- }
- return 0;
- }
44. Lunch Rush
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, k;
- cin>>n>>k;
- int f, t, m = INT_MIN;
- while(n--){
- cin>>f>>t;
- m = max(m, (t>k)?(f+k-t):f);
- }
- cout<<m;
- }
43. DIV + MOD
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int l,r,x;
- cin>>l>>r>>x;
- int ans = r/x + r%x;
- int m = r/x*x - 1;
- if(m>=l) ans = max(ans, m/x + m%x);
- cout<<ans<<endl;
- }
- }
42. Almost Prime
- #include<bits/stdc++.h>
- using namespace std;
- int a[3001];
- int main(){
- int n, cnt=0; cin>>n;
- for(int i=2; i<=n; i++){
- if(a[i]==0) {
- for(int j=i; j<=n; j+=i) a[j]++;
- }
- if(a[i]==2) cnt++;
- }
- cout<<cnt<<endl;
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int a,k; cin>>a>>k;
- if(a<k) cout<<k-a<<endl;
- else cout<<(a+k)%2<<endl;
- }
- }
40. PizzaForces
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- long long n; cin>>n;
- cout<<max(6LL,n+1)/2*5<<endl;
- }
- }
39. Bad Boy
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n, m, i, j;
- cin>>n>>m>>i>>j;
- cout<<1<<" "<<1<<" "<<n<<" "<<m<<"\n";
- }
- }
38. Mocha and Math
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- int a[n];
- for(int i=0; i<n; i++) cin>>a[i];
- int j = a[0];
- for(int i=1; i<n; i++)
- j &= a[i];
- cout<<j<<endl;
- }
- }
37. Lights Out
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int a[5][5];
- memset(a,0,sizeof(a));
- for(int i=1; i<=3; i++)
- for(int j=1; j<=3; j++)
- cin>>a[i][j];
- for(int i=1; i<=3; i++, cout<<endl)
- for(int j=1; j<=3; j++)
- cout<<(( a[i][j]
- + a[i-1][j]
- + a[i+1][j]
- + a[i][j-1]
- + a[i][j+1] ) %2==0);
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n,h, m; cin>>n>>h>>m;
- int t1 = 60*h+m; //cout<<t1<<endl;
- int ans = 24*60;
- for(int i=0; i<n; i++){
- cin>>h>>m;
- int t2=60*h+m-t1;
- if(t2<0) t2+=24*60;
- ans = min(ans, t2);
- }
- cout<<ans/60<<" "<<ans%60<<endl;
- }
- }
35. Nastya and Rice
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n, a, b, c, d;
- cin>>n>>a>>b>>c>>d;
- int l=n*(a-b), r=n*(a+b);
- if(r<c-d || c+d<l) cout<<"No\n";
- else cout<<"Yes\n";
- }
- }
34. Juicer
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, b, d, a, sum=0, cnt=0;
- cin>>n>>b>>d;
- for(int i=0; i<n; i++){
- cin>>a;
- if(a<=b) sum+=a;
- if(sum>d) sum=0, cnt++;
- }
- cout<<cnt;
- }
33. Balanced Round
- #include<bits/stdc++.h>
- using namespace std;
- void solve(){
- int n, k; cin>>n>>k;
- vector<int> a(n);
- for(auto &i:a ) cin>>i;
- sort(a.begin(), a.end());
- int cnt=1, ans=1;
- for(int i=1; i<n; i++){
- if(a[i]-a[i-1]>k) cnt=1;
- else cnt++;
- ans = max(cnt, ans);
- }
- cout<<n-ans<<endl;
- }
- int main(){
- int t; cin>>t;
- while(t--) solve();
- }
32. Food Buying
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- cout<<n+(n-1)/9<<endl;
- }
- }
31. Array Reodering
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n, ans=0; cin>>n;
- vector<int> a(n);
- for(int &x: a) cin>>x;
- //sorting based on even/odd
- sort(a.begin(), a.end(), [](int x, int y){return x%2 < y%2;});
- //cnt no. of good index pairs
- for(int i=0; i<n; i++){
- for(int j=i+1; j<n; j++){
- ans += __gcd(a[i],a[j]*2)>1;
- }
- }
- cout<<ans<<endl;
- }
- }
30. Stripes
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- bool red=false;
- for(int i=1; i<=8; i++){
- string s; cin>>s;
- if(s=="RRRRRRRR") red=true;
- }
- if(red) cout<<"R"<<endl;
- else cout<<"B"<<endl;
- }
- }
29. Lineland Mail
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int a[n];
- for(int i=0; i<n; i++) cin>>a[i];
- cout<<abs(a[0]-a[1])<<" "<<abs(a[0]-a[n-1])<<endl;
- for(int i=1; i<n-1; i++)
- cout<<min(abs(a[i]-a[i-1]),abs(a[i]-a[i+1]))<<" "<<max(abs(a[i]-a[0]),abs(a[i]-a[n-1]))<<endl;
- cout<<abs(a[n-1]-a[n-2])<<" "<<abs(a[n-1]-a[0])<<endl;
- return 0;
- }
28. Unique Number
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n;
- string s;
- for(int i=9; i>0; i--) {
- if(n>=i) s=to_string(i)+s, n-=i;
- }
- cout << (n==0 ? s : "-1") << "\n";
- }
- }
27. Filling Diamonds
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n; cin>>n; cout<<n<<endl;
- }
- }
26. 01 Game
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- string s; cin>>s;
- int a[2] = {};
- for(char c : s) a[c-'0']++;
- cout<<(min(a[0],a[1])%2?"DA":"NET")<<endl;
- }
- }
25. Party
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, a[2000], temp, ans=0, cnt;
- cin>>n;
- for(int i=1; i<=n; i++){
- cin>>a[i];
- }
- for(int i=1; i<=n; i++){
- temp = a[i], cnt=1;
- while(temp!=-1) cnt++, temp = a[temp];
- ans = max(ans, cnt);
- }
- cout<<ans;
- }
24. Magic Numbers
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- string s; cin>>s;
- for(int i=0; i<s.length();){
- if(s[i]!='1' && s[i]!='4') {cout<<"NO"; return 0;}
- else if(s[i]=='1' && s[i+1]=='4' && s[i+2]=='4') i+=3;
- else if(s[i]=='1' && s[i+1]=='4') i+=2;
- else if(s[i]=='1') i++;
- else { cout<<"NO"; return 0;}
- }
- cout<<"YES";
- }
23. Kana and Dragon Quest game
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int x,n,m; cin>>x>>n>>m;
- while(n--){
- int temp = (x/2)+10;
- if(x<=temp) break;
- x = temp;
- }
- if(x<=m*10) cout<<"YES"<<endl;
- else cout<<"NO"<<endl;
- }
- }
22. Make AP
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int a, b, c; cin>>a>>b>>c;
- //ye actual hai
- int A = b - (c-b);
- int B = a + (c-a)/2;
- int C = b + (b-a);
- //a mein dikkat
- if(A!=0 && A>=a && A%a==0) cout<<"YES\n";
- //b mein dikkat
- else if(B && B>=b && B%b==0 && (c-a)%2==0) cout<<"YES\n";
- //c mein dikkat
- else if(C && C>=c && C%c==0) cout<<"YES\n";
- //kuch nhi ho skta
- else cout<<"NO\n";
- }
- }
21. Two-gram
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, mc=0; //max count mc
- string s, ss, twogram; //substring ss
- map<string, int> smp; //string map smp
- cin>>n>>s;
- for(int i=0; i<n-1; i++){
- ss = s[i], ss+=s[i+1], smp[ss]++; //AB:1
- if(smp[ss]>mc) mc=smp[ss], twogram=ss;
- }
- cout<<twogram;
- }
20. Business trip
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int k, cnt=0, a[12]; //12 months
- cin>>k;
- for(int i=0; i<12; i++) cin>>a[i];
- sort(a, a+12);
- for(int i=11; i>=0; i--){
- if(k<=0) { cout<<cnt; return 0;}
- k-=a[i];
- cnt++;
- }
- cout<<(k<=0?cnt:-1); return 0;
- }
19. Case of the Zeros and Ones
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, cnt=0;
- char c; cin>>n;
- while(n--){
- cin>>c;
- if(c=='1') cnt++;
- else cnt--;
- }
- cout<<abs(cnt);
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- string s;
- int n, a, b;
- cin>>n>>a>>b;
- for(int i=0; i<b; i++) s+='a'+i;
- for(int i=0; i<a-b; i++) s+=s[i];
- for(int i=0; i<n-a; i++) s+=s[i];
- cout<<s<<endl;
- }
- }
17. Keyboard
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- char c,i; cin>>c;
- string s = "qwertyuiopasdfghjkl;zxcvbnm,./";
- while(cin>>i){
- cout<<s[s.find(i)-(c=='R')+(c=='L')];
- }
- return 0;
- }
16. Vasya and Socks
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, m, days=0;
- cin>>n>>m;
- for(int i=1; i<=n; i++){
- if(i%m == 0) n++;
- days++;
- }
- cout<<days;
- }
15. Multiply by 2, divide by 6
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main(){
- ll n, cnt, t;
- cin>>t;
- while(t--){
- cin>>n;
- cnt = 0;
- while(n%6==0) n/=6, cnt+=1;
- while(n%3==0) n/=3, cnt+=2;
- if(n!=1) cnt=-1;
- cout<<cnt<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- void solve(){
- long long int n,x,y; cin>>n;
- for(x=0; x<=1000; x++){
- y = n - (2020*x);
- if(y<0) break;
- else if(y%2021==0)
- {
- cout<<"YES"<<endl;
- return;
- }
- else continue;
- }
- cout<<"NO"<<endl;
- }
- int main(){
- int t; cin>>t;
- while(t--){
- solve();
- }
- }
13. Candies
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t; cin>>t;
- while(t--){
- int n, x; cin>>n;
- for(int k=2; k<=30; k++)
- {
- int dr = pow(2,k)-1;
- if(n%dr) continue;
- x = n/dr;
- break;
- }
- cout<<x<<endl;
- }
- }
12. Sale
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, m; cin>>n>>m;
- int a[n], ans=0;
- for(int i=0; i<n; i++) cin>>a[i];
- sort(a,a+n);
- for(int i=0; i<m; i++){
- if(a[i]>=0) break;
- ans += a[i];
- }
- cout<<abs(ans);
- }
11. Odd Divisor
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main(){
- int t; cin>>t;
- while(t--){
- ll n; cin>>n;
- while(n%2==0) n/=2;
- cout<<(n>1 ? "YES" : "NO")<<endl;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- if(n<0){
- int no_ones = n/10;
- int no_tens = n/100 * 10 + n%10;
- cout<<max(no_ones, no_tens);
- } else {
- cout<<n;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n,m;
- cin>>n>>m;
- if(min(n,m)%2) cout<<"Akshat";
- else cout<<"Malvika";
- }
8. Puzzles
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n,m; cin>>n>>m;
- int a[m];
- for(int i=0; i<m; i++) cin>>a[i];
- sort(a,a+m);
- int MIN = a[n-1]-a[0];
- for(int i=1; i+n-1<m; i++)
- MIN = min(MIN, a[i+n-1]-a[i]);
- cout<<MIN;
- }
7. Dubstep
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- string s; cin>>s;
- int flag = 0;
- for(int i=0; i<s.size(); i++){
- if(s[i]=='W' && s[i+1]=='U' && s[i+2]=='B') {
- if(flag==1) cout<<" ";
- i += 2;
- }
- else cout<<s[i], flag=1;
- }
- }
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int a[n], cnt=1, max=1;
- for(int i=0; i<n; i++){
- cin>>a[i];
- }
- for(int i=1; i<n; i++){
- if(a[i]>=a[i-1]) {
- cnt++;
- if(max<cnt) max=cnt;
- }
- else cnt=1;
- }
- cout<<max;
- }
5. Gravity Flip
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int c,r;
- vector<int> a;
- cin>>c;
- for(int i=0; i<c; i++){
- cin>>r;
- a.push_back(r);
- }
- sort(a.begin(), a.end());
- for(int i=0; i<c; i++){
- cout<<a[i]<<" ";
- }
- }
4. HQ9+
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- string s; cin>>s;
- for(int i=0; i<s.size(); i++){
- if(s[i]=='Q' || s[i]=='H' || s[i]=='9'){
- cout<<"YES";
- return 0;
- }
- }
- cout<<"NO";
- }
3. Even Odds
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- long long n,k;
- cin>>n>>k;
- n = (n+1)/2;
- if(k<=n) cout<<2*k-1;
- else cout<<(k-n)*2;
- }
2. Twins
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin>>n;
- int a[n], sum=0, cnt=0, curr=0;
- for(int i=0; i<n; i++) cin>>a[i], sum+=a[i];
- sort(a,a+n);
- sum/= 2;
- for(int i=n-1; i>=0; i--){
- curr+= a[i];
- cnt++;
- if(curr>sum) break;
- }
- cout<<cnt;
- }
1. Football
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- string a; cin>>a;
- int zero=0, one=0;
- for(int i=0; i<size(a); i++){
- cin>>a[i];
- if(a[i] == '0') {zero++; one=0;}
- else {one++; zero=0;}
- if(one==7 || zero==7) {break;}
- }
- if(one==7 || zero==7) {cout<<"YES";}
- else {cout<<"NO";}
- }