DIGITREM - Editorial

1 Like

The main logic snippet of my solution(without using strings)

    ll n,d;
    cin>>n>>d;
    ll tenPower = 1;
    ll sum = 0;
    ll original = n;
    while(n>0){
    	if(n%10 == d){
    		sum = (n+1)*tenPower - original;
    		n++;
    		if(d == 0){
    			sum += (tenPower - 1)/9; // Add the 111.... if d is 0
    		}
    	}
    	tenPower *= 10;
    	n /= 10;
    }
    cout<<sum<<endl;
1 Like

can anyone tell me what’s wrong with my approach or what test cases i am missing ?

https://www.codechef.com/viewsolution/52285493

Out-of-bounds access on this test input:

[simon@simon-laptop][11:00:17]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling satyamk36-DIGITREM.cpp
Executing command:
  g++ -std=c++17 satyamk36-DIGITREM.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
satyamk36-DIGITREM.cpp: In function ‘int main()’:
satyamk36-DIGITREM.cpp:12:24: warning: conversion to ‘int’ from ‘std::__cxx11::basic_string<char>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
      int len=str.length();
              ~~~~~~~~~~^~
satyamk36-DIGITREM.cpp:34:27: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
              int num=stoll(str);
                      ~~~~~^~~~~
satyamk36-DIGITREM.cpp:46:36: warning: conversion to ‘__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}’ from ‘long long int’ may alter its value [-Wconversion]
                  cout<<pow(10,len)-n<<endl;
                                    ^
satyamk36-DIGITREM.cpp:56:63: warning: conversion to ‘__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}’ from ‘int’ may alter its value [-Wconversion]
                          str[index-count-1]=str[index-count-1]+1;
satyamk36-DIGITREM.cpp:60:39: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
                          int num=stoll(str);
                                  ~~~~~^~~~~
satyamk36-DIGITREM.cpp:64:44: warning: conversion to ‘__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}’ from ‘long long int’ may alter its value [-Wconversion]
                          cout<<pow(10,len)-n<<endl;
                                            ^
satyamk36-DIGITREM.cpp:68:47: warning: conversion to ‘__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}’ from ‘int’ may alter its value [-Wconversion]
                      str[index-1]=str[index-1]+1;
satyamk36-DIGITREM.cpp:72:35: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
                      int num=stoll(str);
                              ~~~~~^~~~~
satyamk36-DIGITREM.cpp:85:35: warning: conversion to ‘__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}’ from ‘int’ may alter its value [-Wconversion]
              str[index]=str[index]+1;
satyamk36-DIGITREM.cpp:90:27: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
              int num=stoll(str);
                      ~~~~~^~~~~
satyamk36-DIGITREM.cpp:45:14: warning: ‘index’ may be used uninitialized in this function [-Wmaybe-uninitialized]
              if(index==0){
              ^~
Successful
[simon@simon-laptop][11:00:26]
[~/devel/hackerrank/otherpeoples]>echo "1                 
895896049 9
" | ./a.out
/usr/include/c++/7/bits/basic_string.h:1057: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)

Can anybody tell me what is wrong with my code??
Link to my code
https://www.codechef.com/viewsolution/52393784

Hey, Can i please know which test case(s) am i failing ?

https://www.codechef.com/viewsolution/52388435

1
439978020 0

Thank You very much Bro.

1 Like

Thanks man! I figured out the mistake and submitted the solution successfully. Tysm!

1 Like

Hey, Can I please know why this logic failed ?
https://www.codechef.com/viewsolution/52201204

It gives the wrong answer for this test input:

Ohh, thanks

1 Like

Please help me find an edge case for my code.
Link to Code: CodeChef: Practical coding for everyone

Same as everyone else :stuck_out_tongue:

Can anybody tell me what am I missing?
here

Consider the test input:

1
114915568 0

https://www.codechef.com/viewsolution/52583799
Please tell me which corner case I am missing