S02E10 - Editorial

PROBLEM LINK:

Contest Division 3

Setter: Kanhaiya Mohan
Tester: Nishant Shah
Editorialist: Shivam Thakur

DIFFICULTY

Cakewalk

PREREQUISITES

Basic-implementation skills

PROBLEM

Given two arrays A and B of length N, along with 2 integers X and K. You need to find if the number of indices for which abs(A[i] - B[i]) <= K exceeds X or not.

EXPLANATION

This problem uses a simple implementation of comparing values.
You can use a for loop to count the number of indices for which abs(A[i] - B[i]) <= K. Then compare the count with X. If the count is greater than or equal to X, print “Yes”, else print “No”.

TIME COMPLEXITY

O(N) where N is the size of arrays

SOLUTIONS

Setter's Solution
#include <bits/stdc++.h>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);


    int t;
    cin >> t;
    while(t--){
        int n, x, k;
        cin >> n >> x >> k;
        int a[n], b[n];
        for(int i = 0; i < n; i++){
            cin >> a[i];
        }
        for(int i = 0; i < n; i++){
            cin >> b[i];
        }
        int cnt = 0;
        for(int i = 0; i < n; i++){
            if(abs(a[i] - b[i]) <= k){
                cnt++;
            }
        }
        if(cnt >= x){
            cout << "YES\n";
        }else{
            cout << "NO\n";
        }
    }


    return 0;
}

I also did the same thing, but my code was not giving the output. But later on I found that I was taking two array’s input in just one array & that’s where I was getting wrong outputs. I hope beginners can learn something from this mistake. Hope this helped any of you :slight_smile:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define PI 3.1415926535897932384626

void solve(){
int n,x,k,m;
cin>>n>>x>>k;
int a[n],b[n],i,count=0;

for(i=0; i<n; i++)
{
    cin>>a[i];
    cin>>b[i];
}

for(i=0; i<n; i++)
{
    m=abs(a[i]-b[i]);
    if(m<=k)
        count++;
}
// cout<<count<<endl;
if(count>=x) cout<<"YES\n";
else cout<<"NO\n";

}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
while(t--){
    solve();
}
return 0;

}