Help me in solving LPC problem

My issue

can anyone explain how to find this kind of loop condition by observation the testcase.i got this solution from the submission section.if anybody know this please explain me.And why we use this condition x>5.thank you.

My code

#include<bits/stdc++.h>
using namespace std;
int main ()
{

int t;
cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        string s1,s2;
        cin>>s1>>s2;
        int cnt=0;
        int ans=INT_MAX;
        for(int i=0;i<n-m+1;i++){
                int x1,x2,x;
            for(int j=0;j<m;j++){
                x1=s1[i+j];
                x2=s2[j];
                x=abs(x1-x2);
                if(x>5){
                    x=10-x;
                }
                cnt+=x;

            }
           ans= min(ans,cnt);
           cnt=0;
        }

        cout<<ans<<endl;

    }








    return 0;
}


Problem Link: LPC Problem - CodeChef

@nionroy
just practice more and more bro . and also try to observe things in the rough and try to dry run the code so that u can understand each and every part of it.

1 Like