My code is showing error for some of the inputs but I am not able to figure out the issue, please help. (Out of 9 test cases only 3 are passed)
Finding CB Numbers doubt1
Can you explain the logic of your code?
-
why are executing this only for user==0;
if(user==0){
//cout<<temp<<endl;
for(int h = 0;h<temp.length();h++){
repeat(sub,temp[h]);
} -
Why are checking for the digits that exist in a substring that is not a CB number?
else{
def = true;
for(int h=0;h<10;h++){
if(sub[h]==0){
for(int g=0;g<temp.length();g++){
if(int(temp[g]-‘0’)==h){
//cout<<temp<<" ";
def = false;
}
}
}
} -
What is the significance of def?
if(def){
//cout<<temp<<" ";
for(int h = 0;h<temp.length();h++){
repeat(sub,temp[h]);
}
user++;
- For the first time(user = 0) we don’t have to check for sub-strings condition that’s why.
- I am checking for the on line number 58 then checking whether the digits of the CB number have repeated before in another CB number.
3.def is used for checking the whether the digit is occurred before or not if yes then def is false otherwise true.
I think that I got the error that the way I am checking whether the sub-string is repeated or not condition is wrong. Can you please tell the approach to tackle this.
The following reasons are causing wrong input:
-
Your code is causing runtime error because the length of string could be 17 characters long.
But the stoi() has maximum limit of 2147483647 for 32-bit integer. -
You are doing mistake in checking for already used characters,
Example:
5
23572
the output for this 5
there are two 2, so we can count both as CB number.
But in your case, you are checking for digit.
If you have used 2 earlier than you are not ignoring any other occurrence of 2.
What you have to do is to keep track of position.
If a character at ith position is used in one CB number then that character at that position can’t be used again in other CB number. But it’s any other occurrence at any other position can be used.
Hope, you have understood.
First you solve above mentioned errors, then i’ll tell you about other modifications that you need to incorporate to pass all test cases.
This is the modified code sir. One test is showing wrong answer only. I am not able to figure out why?
Your program is failing for this testcase:
11
44166181111
Expected Output:
4
Your output is:
7
It is counting for numbers:
11 11 61 11 41 181 661
If you notice, the digit at one position is used in multiple numbers which is not allowed as per question.