GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers
Problem :- Nearest Power
Submit Your Solution :- Click Here
Solution :-
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long t, n, m, p, ans, a, b;
cin>>t;
while(t--){
cin>>n>>m;
p=log(n)/log(m);
a=pow(m,floor(p));
b=a*m;
if(b-n<=n-a)
ans=b;
else
ans=a;
cout<<ans<<endl;
}
return 0;
}
Output:-
0 Comments: