Codeforces Round 739 (Div. 3) 1560D. Make a Power of Two Solution


  

Problem Link : https://codeforces.com/problemset/problem/1560/D

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. using ll = long long ;
  5. int32_t main() {
  6. ios_base::sync_with_stdio(0);
  7. cin.tie(0);
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n ;
  12. cin >> n ;
  13. if ( n & ( n - 1) == 0){
  14. cout <<"0\n";
  15. continue ;
  16. }
  17. vector<ll>v ;
  18. for ( int i = 0 ; i <= 60 ; i++){
  19. v.push_back(1ll<<i);
  20. }
  21. string s = to_string(n) ;
  22. int s_size = s.size() ;
  23. ll ans = 1e9 ;
  24. for ( int i = 0 ; i < v.size() ; i++){
  25. ll s_index = 0 , t_index = 0 , extra = 0 ;
  26. string t ;
  27. t = to_string(v[i]);
  28. int t_size = t.size() ;
  29. while(s_index < s_size and t_index < t.size()){
  30. if(s[s_index] == t[t_index]){
  31. s_index++ , t_index++ ;
  32. }
  33. else {
  34. s_index++,extra++;
  35. }
  36. }
  37. if (s_index == s_size and t_index != t_size){
  38. extra += (t_size - t_index) ;
  39. }
  40. else if (s_index != s_size and t_index == t_size){
  41. extra += (s_size - s_index) ;
  42. }
  43. ans = min(extra,ans) ;
  44. }
  45. cout << ans <<'\n';
  46. }
  47. return 0 ;
  48. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.