-3

The question was: A contest closes in n days hh hours, mm minutes and ss seconds. Given two values of n, how many palindromes of the format nhhmmss would we find in the indicated interval?

Example 1

Input

1 2

Output

472

Explanation

We need to check the numbers 1000000 through 2235959 including only numbers where the last 6 digits correspond to times. We find 472 such numbers: 1000001, 1001001, 1002001, 1003001, 1004001, ..., 2231322, 2232322, 2233322, 2234322, 2235322

Example 2

Input

0 2

Output

708

Explanation

There are 708 palindromes: 0000000, 0001000, 0002000, 0003000, 0004000, ..., 2231322, 2232322, 2233322, 2234322, 2235322

What I tried is:

#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define int long long

int ctr = 0;

int isPal(int n) {
    int reverse = 0;

    for(int i = n; i > 0; i /= 10)
        reverse = reverse*10 + i%10;

    return n == reverse; 
}

void inRange(int low, int high) {
     for (int i = low; i <= high; i++) {
          if (isPal(i)) {
             string tmp_str = to_string(i);
             string hh = tmp_str.substr(1, 2);
             string mm = tmp_str.substr(3, 2);
             string ss = tmp_str.substr(5, 2);
             int hh1, mm1, ss1;

             hh1 = stoi(hh);
             mm1 = stoi(mm);
             ss1 = stoi(ss);

             if (hh1 <= 23 && mm1 <=59 && ss1 <=59)
                 ctr++;
          }
     }
}

int main() {
    ios::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0);

    int n1, n2, min, max;

    cin >> n1 >> n2;

    min = n1*1000000;
    max = (n2*1000000)+235959;

    inRange(min,max);

    if (n1 == 0)
       cout << (ctr+99);
    else
        cout << ctr;

    return 0;
}

But it is throwing an error as:

    terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 3) > this->size() (whic
h is 1)
exited, aborted

Any help would be appreciated!

TheArchitect
  • 1,160
  • 4
  • 15
  • 26
zeroSpook
  • 54
  • 10
  • 2
    And when you used your debugger to run your program, what did you see? This is precisely what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Aug 15 '20 at 16:32
  • 1
    You may want to read these two links: 1. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) 2. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Aug 15 '20 at 16:34
  • 2
    `std::to_string(0)` is "0", not "0000000" (there are no leading zeros). – Blastfurnace Aug 15 '20 at 16:34
  • @Blastfurnace....Though my program logic is wrong, but this is the answer I was looking for. Many Thanks ! :) – zeroSpook Aug 15 '20 at 16:36
  • Sam & Andreas Thanks, I'll look into it – zeroSpook Aug 15 '20 at 16:37
  • How is the answer for first test case 472? I am getting 288 – Deepak Tatyaji Ahire Aug 15 '20 at 17:38
  • @DeepakTatyajiAhire 472 is the expected answer, since my code is faulty, it is generating 288(which doesn't match with the test case) – zeroSpook Aug 15 '20 at 18:45
  • @zeroSpook Can you mathematically prove 472 as the answer? – Deepak Tatyaji Ahire Aug 15 '20 at 18:49
  • @DeepakTatyajiAhire IDK, my programming logic says 288 must be the answer, but this was the question given in TCS Codevita and according to them 472 must be the answer – zeroSpook Aug 15 '20 at 18:51
  • I did calculate the answer mathematically. I still get 288 – Deepak Tatyaji Ahire Aug 15 '20 at 18:52
  • Also, I corrected your code and checked the answer, still 288 – Deepak Tatyaji Ahire Aug 15 '20 at 18:53
  • 1
    @zeroSpook - I tried to implement this solution in JAVA. Even I am getting answer as 288 (for input 1 2) and 431 (for input 0 2) – Aakash Goplani Aug 16 '20 at 10:54
  • @DeepakTatyajiAhire - could you please help me with the steps to calculate this mathematically? – Aakash Goplani Aug 16 '20 at 18:09
  • 1
    Sure @Kinley Christian. Lets take an example for input = 1 2. It will be a seven digit number starting with 1 or 2. If it starts with 1, it will end with 1 and same for 2. Now the middle digits, that at the 4th place can be anything from [0, 5] -> 6 values. Now we are left with 4 blank spaces -> 1 (_) (_) ([0-5]) (_)(_) 1. Assigning hours is easy in this case. So the second and third blank can take values from [00, 23] -> 24 values. We will have same procedure for numbers starting with 2. Therefore, Final Answer = 6 * 24 * 2 = 288. – Deepak Tatyaji Ahire Aug 16 '20 at 18:25

1 Answers1

1

The std::to_string function does not include leading zeros in the result. Your current code assumes the string is 7 digits but that might not be true and is likely why std::string::substr is throwing an exception for an invalid position.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70