Please read the new rule regarding the restriction on the use of AI tools. ×

newbie_se_bhi_noob's blog

By newbie_se_bhi_noob, history, 3 years ago, In English

Problem My solution is here and my approach was first I was finding the sum of digits, then smallest digit we have to add will be (9-sum)%9 and that correct Tim William did it too!.

Now I'm finding the position where I want to add that single character. For position, logic is as follows If the wanted digit is less than all digits present in the string then put it before the smallest number. for eg., if the string is 5 I will put 4 before 5 and will be 45.

Now for wanted digit larger than digits present in the string, I will put it next to the smallest present in the string for eg 12121 then I will put 2 at last and it will be 121212. Then where is logic incorrect can someone give me a test case.

I tested for test cases as follows: 6
5
33
12121
55
19
90
and result were as follows :
Case 1: 45
Case 2: 333
Case 3: 121212
Case 4: 558
Case 5: 189
Case 6: 90

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You have the following line of code to calculate the variable want: lli want = (9 - sum) % 9;

However, if value of sum is greater than 9, (9 — sum) becomes negative and in programming languages (-a)%b is treated as -(a%b). You can print the value of want after calculating it for a case like 99998.

This is something you must fix. However, I am not sure if there are any additional mistakes since I haven't checked it fully.