Comments

Hey man appreciate it. thanks for helping me.

oh yes. thanks. i tried to make both conditions using mod but the k>n condition was giving error. But why? can you please explain ?

i tried but it was showing error.

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

int main(){
  long long n,k;
  cin>>n>>k;
  long long N=n;
  long long mn=1000000000000000000;
     while(n>=0){
        mn=min(n,mn);
        if(n<k){
            n=k-n;
        }
        else{
            n=n-k;
        }
        if(n==mn){
        mn=min(n,mn);
            break;
        }
     }
     cout<<mn;

   return 0;
       }

i was getting tle. Can anyone help me ?