Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

public static int answer(int[] l) { Arrays.sort(l); int ans=0; // Your code goes here. // q0->%3=0 Queue q0 = new LinkedList(); // q1->%3=1 Queue q1 = new LinkedList(); // q2->%3=2 Queue q2 = new LinkedList(); int totalSum=0; for(int x:l) { totalSum+=x; if(x%3==0) {q0.add(x);} if(x%3==1) {q1.add(x);} if(x%3==2) {q2.add(x);} } if(totalSum %3 ==1) { if(!q1.isEmpty()) {q1.remove();} else { if(!q2.isEmpty()) {q2.remove();} else {ans=0;} } } if(totalSum %3 ==2) { if(!q2.isEmpty()) {q2.remove();} else { if(!q1.isEmpty()) {q1.remove();} else {ans=0;} if(!q1.isEmpty()) {q1.remove();} else {ans=0;} } } Queue q_temp = new LinkedList(); int oo=0; while(!q0.isEmpty()) { // System.out.println("q0: "+q0.toString()); oo=q0.remove(); q_temp.add(oo); // System.out.println("q_temp: "+q_temp.toString()); } while(!q1.isEmpty()) { // System.out.println("q1: "+q1.toString()); oo=q1.remove(); q_temp.add(oo); // System.out.println(q_temp.toString()); } while(!q2.isEmpty()) { // System.out.println("q2: "+q2.toString()); oo=q2.remove(); q_temp.add(oo); // System.out.println(q_temp.toString()); } int temp[] = new int[q_temp.size()]; // int counter=0; for(int x:q_temp) {temp[counter]=x;counter++;} Arrays.sort(temp); String out=""; for(int i=temp.length-1;i>=0;i--) { out+=String.valueOf(temp[i]); } // System.out.println(out); ans=Integer.parseInt(out); return ans; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago

Question

Write short notes on departmentation.

Answered: 1 week ago