Problem: Buggy Calculator
Problem Statement
Add two number A and B without carrying forward any value.
Solution
The above sum can also be written as
let A consist of M digits and B consist of N digits
let S[i] denote ith digit of required sum of A and B, A[i] be ith digit of A and B[i] be ith digit of B. (starting from unit, ten’s…).
For every digit i < Max(A,B)
S[i] = (A[i]+B[i])%10
Simply calculate this and output.
Addition (on vijju’s recommendation)
Avoid printing leading zeroes
Suppose you have a string (or char array) C storing digits if your answer, unit place at leftmost position.
Int f = 0;
String output = “”;
for(i = C.length-1; i >= 0;i–){
If(C[i] != ‘0’)f = 1; // for digit int array, replace ‘0’ with 0.
If f==1 out.append(C[i]);
}
if(f==0)print(0);
else print out;
If you feel you have a better idea for above, feel free to share in comments.
My Solution [Here][1]
Problem: Maximum Number
Problem Statement
Given a number N, output Maximum number possible which satisfy constraint that number is multiple of 6. If no such number possible print -1.
Solution
First thing to know:
- Every multiple of 2 has 0,2,4,6 and 8 at unit’s place. (I know You know this )
- Sum of digits of Every multiple of 3 is also divisible by 3.
For checking divisibility by 6, it is sufficient to check divisibility by both 2 and 3.
Then, Following cases arise:
I. Digit at unit’s place is odd
In this case, if digit at ten’s place is also odd, then number cannot be made a multiple of 6. Thus print -1.
If ten’s place digit is even. delete unit’s place digit from given number. Now, if the resultant number is divisible by 3, print that number. Else print -1.
II. Digit at unit’s place is Even
In this case, multiple choices are there.
Let sum be sum of digits of given number. We can delete any digit d (except unit place digit if ten’s place digit is odd, so that the number remains a multiple of 2) for which sum%3 == d%3. This works because after deleting that digit, sum is a multiple of 3.
Now, to maximize this number, we need to find the digit at largest place satisfying above condition. This works because:
Take example number 4510222
sum of digits = 4+5+1+2+2+2 = 16
sum%3 = 1
Now, we can delete digits 1 and 4 (because 1%3=3, 4%3 = 1)
If we delete 1, we get number 450222
If we delete 4, we get number 510222
We delete the largest digit (leftmost) for which the next digit is greater than deleted digit. This is necessary to avoid following case
Number 7510222
Digits 7 and 1 can be deleted according to above solution, giving numbers 510222 and 750222 respectively. Here, deleting largest (leftmost) index give smaller result because 7 > 5. This worked in above case because 4<5.
Correct Solution
Find leftmost digit satisfying constraints
- sum%3==digit%3
- digit < Immediately next digit
In case no digit is found such that digit < Immediately Smaller digit, find Rightmost digit for which digit > Immediately right digit. This works because of following saying.
There’s a well known saying (since now only :D) If you can’t maximize the addition of something, try to minimize it’s reduction. It’s the Same thing.
Just delete that digit and print the answer.
In case no digit satisfy sum%3==digit%3, print -1. (Example number 222)
Here’s my
[2]
Enjoy Coding. :D
Feel free to ask anything... as always :)
[1]: https://www.codechef.com/viewsolution/15981433
[2]: https://www.codechef.com/viewsolution/15988110