jayati

Minimum Operations to Make a Special Number

Sep 3rd, 2023
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minimumOperations(string num) {
  4.         if(num.length()==1)
  5.         {
  6.             return 1;
  7.         }
  8.         int cz=0;
  9.       for(int i=0;i<num.length();i++)
  10.       {
  11.           if(num[i]=='0')
  12.           {
  13.               cz++;
  14.           }
  15.       }
  16.   bool f=true;
  17.     for(int i=0;i<num.length()-1;i++)
  18.     {
  19.        
  20.              for(int j=i+1;j<num.length();j++)
  21.              {
  22.                  string pair = string(1,num[i]) + string(1,num[j]);
  23.                  if (pair == "00" || pair == "25" || pair == "50" || pair == "75") {
  24.                   f=false;
  25.             }
  26.              }
  27.        
  28.        
  29.     }
  30.         if(f)
  31.         {
  32.             return num.length()-cz;
  33.         }
  34.       int n = num.size();
  35.     int min_del = INT_MAX;
  36.  
  37.     min_del = min(min_del, n - 1);
  38.  
  39.     for (int i = 0; i < n - 1; i++) {
  40.         for (int j = i + 1; j < n; j++) {
  41.             string pair = string(1,num[i]) + string(1,num[j]);
  42.             int deletions;
  43.             if(i+1==j)
  44.             {
  45.                 deletions = n-j-1;
  46.             }
  47.             else
  48.             {
  49.                  deletions = n-i-2;
  50.             }
  51.            
  52.  
  53.             if (pair == "00" || pair == "25" || pair == "50" || pair == "75") {
  54.                 min_del = min(min_del, deletions);
  55.             }
  56.         }
  57.     }
  58.  
  59.     return min_del;
  60.     }
  61. };
  62. // 8040. Minimum Operations to Make a Special Number
  63. User Accepted: 6883
  64. User Tried: 9492
  65. Total Accepted: 7035
  66. Total Submissions: 22544
  67. Difficulty: Medium
  68. You are given a 0-indexed string num representing a non-negative integer.
  69.  
  70. In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0.
  71.  
  72. Return the minimum number of operations required to make num special.
  73.  
  74. An integer x is considered special if it is divisible by 25.
Advertisement
Add Comment
Please, Sign In to add comment