I was wondering what test case would this fail on? I passed 4 out of 5? The goal
ID: 3750816 • Letter: I
Question
I was wondering what test case would this fail on? I passed 4 out of 5? The goal of the function is to read an array on integers and find the max combination of numbers that is divisible by 3 and return it.
"You have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once."
Inputs: (int list) l = [3, 1, 4, 1] Output: (int) 4311
Inputs: (int list) l = [3, 1, 4, 1, 5, 9] Output: (int) 94311
My code is below
Explanation / Answer
Answer:
Consider the test case:
int[] l = {1,2,3,4,5,6,7,8,9,0};
You're code will fail with error:
int this line of code:
Because the limit for int is 2,147,483,647 you can't convert "9876543210" into an int because it exceeds the limit.
So to correct this you can return the result as the string itself instead of converting it into an int.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.