0

how to reduce the time in this code

#include <bits/stdc++.h> using namespace std; bool isPowerOfTwo(int n) { if (n == 0) return 0; if(n==1) return 1; while (n != 1) { if (n%2 != 0) return 0; n = n/2; } return 1; } int highestPowerof2(int n) { int p = (int)log2(n); return (int)pow(2, p); } int difference(int v, int u){ if(v>u){return v-u;} else{return u-v;} } int main(){ unsigned long long int t,s,m,d; cin>>t; for(int r = 0;r<t;r++){ cin>>s>>m; d = difference(s,m); int i; if(s==m){cout<<0<<endl;} else { if(isPowerOfTwo(d)){cout<<1<<endl;} else { while(d!=1){ d=d-highestPowerof2(d); i++; } cout<<i+1<<endl; } } } return 0; } https://code.sololearn.com/c8hVr414rqA6/?ref=app

25th Jan 2021, 11:41 AM
Nazeefa Labiba
Nazeefa Labiba - avatar
1 Resposta
+ 1
Learn how to code properly, and learn algorithms
25th Jan 2021, 12:36 PM
Angelo
Angelo - avatar