https://www.codechef.com/viewsolution/52082921
Can someone tell me what corner case I missed?
can anyone tell me what’s wrong with my approach or what test cases i am missing ?
#include <iostream>
#include<cmath>
using namespace std;
unsigned long long int rem(unsigned long long int n,int d){
unsigned long long int k=0;
unsigned long long int a=0;
int m;
int l=-1;
m=log10(n);
m+=1;
int h=0;
int flag=0;
a=n;
for(int i=0;i<m;i++){
h=n%10;
if(h==d){
l=i;
}
n=n/10;
}
n=a;
if( l==-1){
return 0;
}
else{
for(int i=0;i<m ;i++){
h=n%10;
if(h==d){
if(i<l){
for(int f=0;f<10;f++){
if(h+f>=10 && ((h+f)%10)!=d){
k+=f*pow(10,i);
n+=f;
flag=1;
break;
}
}
if(flag==0){
k+=1*pow(10,i);
n+=1;
}
else{
flag=0;
}
}
else if( i>=l){
k+=1*pow(10,i);
n+=1;
}
}
else{
if(i<l){
a=n/10;
a=a%10;
if(a+1!=d){
for(int f=1;f<10;f++){
if(h+f>=10 && ((h+f)%10)!=d){
k+=f*pow(10,i);
n+=f;
break;
}
}
}
}
}
n=n/10;
if(n==0){
break;
}
}
}
return k;
}
int main() {
unsigned long long int t;
cin>>t;
for(unsigned long long int i=0;i<t;i++){
unsigned long long int n;
int d;
cin>>n;
cin>>d;
d=rem(n,d);
cout<<d<<endl;
}
return 0;
}
1
801523893 9
1
536942010 5
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define mod 1000000007
void testCase(){
ll n, k;
cin>>n>>k;
if(n==k){
cout<<1<<endl;
return;
}
string num = to_string(n);
string sum = “”;
ll carry = 0;
char find = (k+‘0’);
int index = num.size()-1;
if(k==0){
while(index>=0){
if(num[index] == find){
int si = (num.size() - 1 - sum.size());
int ei = index;
while(ei<=si){
if(num[si] == find){
num[si] = (((num[si] - '0') + 1) + '0');
char newChar = '1';
sum = newChar + sum;
}else{
char newChar = '0';
sum = newChar + sum;
}
si--;
}
}
index--;
}
if(sum.size() == 0){
cout<<0<<endl;
return;
}
cout<<sum<<endl;
return;
}
while(index>=0){
if(carry>0){
ll newNum = (num[index] - '0') + carry;
carry = newNum/10;
num[index] = ((newNum%10) + '0');
}
if(num[index] == find){
int si = (num.size() - 1 - sum.size());
int ei = index;
while(ei<=si && (si<=(num.size()-1))){
if(ei == si){
if(carry==0 && (num[si]==find)){
char one = '1';
sum = one + sum;
ll newNum = ((num[si] - '0') + 1);
carry = newNum/10;
num[si] = ((newNum%10) + '0');
}else{
ll newNum = ((num[si] - '0') + carry);
carry = newNum/10;
num[si] = ((newNum%10) + '0');
}
si--;
continue;
}
ll qty = 10 - ((num[si] - '0') + carry);
if(qty==10){
qty = 0;
}
ll newNum = ((num[si] - '0') + qty + carry);
carry = newNum/10;
num[si] = ((newNum%10) + '0');
char newChar = qty + '0';
sum = newChar + sum;
si--;
}
}
index--;
}
if(sum.size() == 0){
cout<<0<<endl;
return;
}
int j = 0;
while((j<sum.size()) && (sum[j]=='0')){
j++;
}
if(j>0){
sum.erase(0, j);
}
cout<<sum<<endl;
}
int main(){
ll t = 1;
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>t;
while(t–){
testCase();
}
}
my code is not getting AC. Could anyone tell me which test case is failing my code?
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Thanks!!
I did it in a different way not the most efficient though.
https://www.codechef.com/viewsolution/52123069
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;
can anyone tell me what’s wrong with my approach or what test cases i am missing ?
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 ?
1
439978020 0
Thank You very much Bro.
Thanks man! I figured out the mistake and submitted the solution successfully. Tysm!