Skip to content

Instantly share code, notes, and snippets.

@danivijay
Created June 1, 2017 12:41
Show Gist options
  • Save danivijay/b8fe59e496434e27205666e4a558e8be to your computer and use it in GitHub Desktop.
Save danivijay/b8fe59e496434e27205666e4a558e8be to your computer and use it in GitHub Desktop.
package com.google.challenges;
public class Answer {
public static int answer(int[] l) {
// Your code goes here.
}
}
@danivijay
Copy link
Author

Please Pass the Coded Messages

You need to pass a message to the bunny prisoners, but to avoid detection, the code you agreed to use is... obscure, to say the least. The bunnies are given food on standard-issue prison plates that are stamped with the numbers 0-9 for easier sorting, and you need to combine sets of plates to create the numbers in the code. The signal that a number is part of the code is that it is divisible by 3. You can do smaller numbers like 15 and 45 easily, but bigger numbers like 144 and 414 are a little trickier. Write a program to help yourself quickly create large numbers for use in the code, given a limited number of plates to work with.

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.

Languages

To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java

Test cases

Inputs:
(int list) l = [3, 1, 4, 1]
Output:
(int) 4311

Inputs:
(int list) l = [3, 1, 4, 1, 5, 9]
Output:
(int) 94311

@danivijay
Copy link
Author

@danivijay
Copy link
Author

danivijay commented Jun 1, 2017

To find all permutations of an array :

public class Permute{
    static void permute(java.util.List<Integer> arr, int k){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, k+1);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            System.out.println(java.util.Arrays.toString(arr.toArray()));
        }
    }
    public static void main(String[] args){
        Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment