Help me in solving NUM239 problem(Cant seem to understand what test case i am failing)

My issue

Cant seem to understand what test case I am failing . I would really appreciate if some one with pro pack can check it out for me … I have dry ran the code like more than 15 times but still cant still cant seem to get it

My code

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


int main(){
    

int t;
cin >> t;

for (int i = 0; i < t; i++) {

  int x, y;

  cin >> x >> y;
  int c = 0;
  int diff = abs(x - y);
  int mi = min(x, y);
  int ma = max(x, y);

  if (mi % 10 > 3 && mi % 10 <= 9) {

    c = c + 1;

  } else if (mi % 10 == 3) {

    c = c + 3;

  } else if (mi % 10 >= 0 && mi % 10 <= 2) {

    c = c + 3;
  }

  if (diff > 10) {

    c = c + (3 * ((ma / 10) - (mi / 10) - 1));
  }

  if (ma % 10 == 9) {

    c = c + 3;

  } else if (ma % 10 >= 3 && ma % 10 < 9) {

    c = c + 2;

  }

  else if (ma % 10 == 2) {

    c = c + 1;
  }

  cout << c << endl;
}
return 0;
}

Problem Link: NUM239 Problem - CodeChef

@justdoit01
The logic is quite simple u have to iterate from l to r and while iterating when u get a number whole %10 == 2 or 3 or 9 then increase your count.

Yes i thought about that and it already worked but i wanted to optimize it so … i broke a for eg: 46 87 into 3 parts ( 46 to 50 , 50 to 80 , and 80 to 87 )

for 1st part (Here it is 46):

  There  can be 3 cases :  end digit is {0 to 2}  ,  end digit is {3 to 8}   ,  end  digit is {9}
                                   |                                 |                     |
                                   \/                                \/                    \/
                          it will have exactly 3     it will has exactly 2       it will have exactly1
                          beautiful numbers          beautiful numbers            beautiful numbers 
                         {for is 42(or 40 or 41)      similar to example        
                          to 50 will have 3 num       of 42 to 50 
                         namely 42 43 and 49             

part 2 : In a set of 10 number there are exactly 3 beautiful numbers .
for Eg: 50 to 60 will have 3 beautiful numbers , 60 to 70 will have 3 and so on…

       So , if we can calculate the number of sets , can then multiply them "3" .

      For eg of 46 to 87 ,we Have 50 to 80 , i.e "3" sets (50 to 60 , 60 to 70 and 70 to 80) , So we can multiply it by ' 3 ' to get total number of beautiful numbers between 50 to 80 .


            Formula that I created for number of sets :  [  (2_num / 10)   -    (1_num/10)   -  1  ]

           Note: This formula wont work for [(2_num - 1_num) / 10 ] as we are depending on "/10" to
                      convert the number to its right most 10th place integer .... i.e 46 will be converted 
                      40 rather than 50 same goes for number like 49 too.

part 3 : It is more or less same as part 1 … but with a few conditional adjustments

SO if we calculate the total number of beautiful number between 46 to 87 through my method ,
We get :

           1       +         9        +          2       ===  12 Beautiful Number

           |                    |                     |
           \/                   \/                    \/
         46 to 50         50 to 80               80 to 87

@justdoit01
ok got it
for 13 - 20 its should be 2 your code is giving 3

1 Like

yeh i did see it later … thank you for taking ur time to find it out for me :slight_smile:

1 Like