Biggest possible sum of multiples

Firstly, as for me it's not clearly why for first number from the second array we should use only three first items from the first array, and for each other numbers from the second array we use anyone numbers from the first array.

However, we can represent this two arrays as a bipartite graph, where each number from the second array connect to (each?*) numbers from the first array. In this graph each edge (u,v) has a weight which equal to multiplication of the two numbers from arrays (u*v). And for given graph you should resolve max flow problem.

* if I understand the problem correctly, you should connect first number from the 2nd array with first 3 numbers from the 1st array, and each other number from 2nd array with each numbers from the 1st array.


Continue doing this until every number is used. Write the biggest possible sum of these multiples

This looks like a discrete and combinatorial optimization problem. This first algorithm which came to my mind for solving this is by using Branch and Bound algorithm.

It allows back tracking and you can implement it with recursion.


I implemented a Mixed-Integer Programming approach in Julia using JuMP. It works for your example and the general concept should work, but may need more testing!

It should be quite readable even if you are not familiar with julia. Keep in mind, that indexing starts with 1 (like in Matlab) instead of 0 (like in C, Java, ...)

The basic idea is the following (read the comments to understand more): define a binary variable X with dimension (N,N): if X[a,b] == 1 -> A[b] was chosen in step a.

The hardest part to understand is the bigM-Formulation which is just a different formulation of the logic explained in the comments. It's sole purpose is linearizing!

using JuMP

N = 4
A = [16 6 2 10]
B = [3 8 12 9]

# MODEL
m = Model()

# VARS
@defVar(m, x[1:N, 1:N], Bin)  # N steps, N vars/indices // binary

# CONSTRAINTS
# (1) exactly one var chosen in each step
for s = 1:N 
    @addConstraint(m, sum(x[s,:]) == 1)
end

# (2) each var is chosen exactly once
for v = 1:N 
    @addConstraint(m, sum(x[:,v]) == 1) 
end 

# (3) if var v chosen in step s -> are enough predecessors already taken away -> is in positions 1:3?
for s = 1:N 
    for v = 1:N 
        needed_pops_before = v - 3
        if needed_pops_before > 0
            # bigM formulation:
            # formula: (x[s,v] == 1) -> sum of predecessors taken away until now >= needed_pops
            #   use deMorgan-law -> !(x[s,v] == 1) -> sum >= needed_pops    
            #   formulate with bigM = N because needed_pops limited by N-3
            @addConstraint(m, N * (1-x[s,v])  + sum{x[ss,jj], ss=1:s-1, jj=1:v-1} >= needed_pops_before)
        end 
    end 
end

# OBJECTIVE
@setObjective(m, Max, sum{B[s] * A[v] * x[s,v], s=1:N, v=1:N})

# SOLVE
status = solve(m)

# OUTPUT
println("Objective is: ", getObjectiveValue(m))
println("Solution: ")
for s = 1:N 
    for v = 1:N 
        if getValue(x[s,v]) > 0.00001
            print(A[v], " ")
        end 
    end
end

Output:

Objective is: 336.0
Solution:
2 6 16 10

Comments

  1. Keith

    • 2019/12/29

    Find the largest number that can be made from some or all digits of array and is divisible by 3. The same element may appear multiple times in 

  2. Stewart

    • 2019/7/7

    Multiples of 8 are: 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 256

  3. Lombardo

    • 2018/7/22

    5, 10, 15, 20, 25, 30, 35, 40, 45, 50,

  4. Arjun

    • 2018/1/23

    Find the largest number that can be made from some or all digits of array and is divisible by 3. The same element may appear multiple times in the array, but each element in the array may only be used once. Examples: Input : arr [] = {5, 4, 3, 1, 1} Output : 4311 Input : Arr [] = {5, 5, 5, 7} Output : 555. Asked In : Google Interview.

  5. Luka

    • 2020/2/17

    100 multiples of 5 greater than 100 are: 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 

  6. Collin

    • 2015/6/4

    This is a list of multiple births, consisting of notable higher order (4+) multiple births and pregnancies. Twins and triplets are sufficiently common to have their own separate articles. With the use of reproductive technology such as fertility drugs and in vitro fertilization (IVF) such births have become increasingly common. This list

  7. Dominik

    • 2020/2/14

    The largest number, that can not be made of the sum of a multiple of coprimes p and q, you can make every number - add 5 to these, and obtain 6,7,8,9,10 - and so on. Let M be the set of numbers which can be expressed in the form 5a+​11b.

  8. Walter

    • 2016/12/17

    $\begingroup$ Interesting, the largest number that cannot be represented as non-negative combination of 5 and 11 is $39 = (5-1)(11-1)-1$ and the one for positive combination is $55 = 5\times11$. I wonder whether there is a pattern of this. $\endgroup$ – achille hui Aug 18 '13 at 9:17

  9. Cole

    • 2017/4/7

    multiplied can be as large as 2 digit or 100 digit.

  10. Colter

    • 2020/8/9

    You mean the answer to (8×1)+(8×2)+(8×3)+…….+(8×15). This is 960 as explained below The above can be written as 8×(1+2+3+….15). A continuous series like 1

  11. Barone

    • 2020/9/21

    step 1 Address the formula, input parameters & values. Input parameters & values: The number series 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, . . . . , 240. The 

  12. Beckett

    • 2020/7/19

    class Solution: def largestMultipleOfThree (self, digits: List[int]) -> str: ''' -can use a clever trick, if the sum of digits % 3 == 0, then the whole number is a

  13. Jesus

    • 2016/1/5

    What are the Multiples of 3? Any number that can be denoted in the form 3p where p is an integer is a multiple of 3. For example, 9, 12 

  14. Brahimi

    • 2021/1/1

    the answer would be 18, as 2*9 is the highest you can go while staying below 20. SO a way of doing it would be =. y/x == 20/2= 10; 10–1= 9; 9 *2=18; y = 20; x = 3; the answer would be 18; as 3 * 6 = 18; 20/3 = 6.67 approx. 7; 7–1 = 6; 6 * 3 = 18; y = 30; x = 10; the answer would be 20 as 10*2 = 20;

  15. Jude

    • 2020/6/5

    Given an integer array of digits , return the largest multiple of three that can be formed by concatenating some of the given digits in any order.

  16. Nova

    • 2016/3/6

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

  17. Sterling

    • 2015/6/8

    Find the largest multiple of 2, 3 and 5. An array of size n is given. The array contains digits from 0 to 9. Generate the largest number using the digits in the array 

  18. Avery

    • 2016/7/22

    Given an array of non-negative integers. Find the largest multiple of 3 that can be formed from array elements. For example, if the input array is {8, 1, 9}, the output should be “9 8 1”, and if the input array is {8, 1, 7, 6, 0}, output should be “8 7 6 0”.

  19. Kieran

    • 2020/12/25

    Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three. Example 1: Input: nums = [3​ 

  20. Gatlin

    • 2015/7/5

    Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string. Example 1: Input: digits = [8,1,9] Output: "981" Example 2:

  21. Jonas

    • 2021/5/3

    The smallest positive number that is a multiple of two or more numbers. Let's start with an Example Least Common Multiple of 3 and 5: List the Multiples of each 

  22. Kane

    • 2016/5/20

    Least Common Multiple is : 30 Calculate Least Common Multiple for : 5, 6 and 10. Factorize of the above numbers : 5 = 5 6 = 2 • 3 10 = 2 • 5 Build a prime factors table

  23. Cook

    • 2018/5/30

    Input array is {4,8,0}.Then,the largest number formed using these digits and divisible by 3 is 840. Properties of multiples of 3-. 1.Sum of the digits 

  24. Dexter

    • 2020/4/21

    Calculator Use. The multiples of numbers calculator will find 100 multiples of a positive integer. For example, the multiples of 3 are calculated 3x1, 3x2, 3x3, 3x4

  25. Fletcher

    • 2020/2/19

    Suppose we have an array of different digits; we have to find the largest multiple of 3 that can be generated by concatenating some of the given 

  26. Tony

    • 2019/1/16

    Find the largest multiple of 3. Given an array of non-negative integers. Find the largest multiple of 3 that can be formed from array elements. For example, if the input array is {8, 1, 9}, the output should be “9 8 1”, and if the input array is {8, 1, 7, 6, 0}, output should be “8 7 6 0”.

  27. Dalton

    • 2019/10/31

    I have found a way, say y is 20, and x is 2. the answer would be 18, as 2*9 is the Given an integer X, what is the algorithm to calculate the biggest odd number 

  28. Bruno

    • 2020/11/17

    Given an array of digits (contain elements from 0 to 9). Find the largest number that can be made from some or all digits of array and is divisible by 3. The same element may appear multiple times in the array, but each element in the array may only be used once.

  29. Jordan

    • 2017/5/12

    List Multiples of a Number Calculator - Online Tool - dCode

  30. Robin

    • 2015/5/19

    Find the largest number that can be made from some or all digits of array and is divisible by 3. The same element may appear multiple times in the array, but each element in the array may only be used once. Examples: Input : arr[] = {5, 4, 3, 1, 1} Output : 4311. Algorithm. Get array size and array input and calculate sum while getting input.

  31. Gavin

    • 2021/4/17

    What is the largest number for which it is impossible to purchase exactly that number of What if the McNuggets were available in boxes of 7, 11, and 17? If the number of McNuggetts cannot be purchased by only using a 6, 9, or 20 pack of 

  32. Uriel

    • 2016/7/30

    The tables once again illustrate the number of McNuggetts and possible combinations of 7, 11 and/or 17 McNuggetts in order to be able to purchase the given number of Mcnuggetts. From these tables, one can see that 37 is the largest number of McNuggetts one cannot purchase by only using 7, 11 and/or 17 packs of McNuggetts.

  33. Hernandez

    • 2019/6/7

    The first 3 multiples of 8 are: 8, 16, and 24.The first 3 multiples of 10 are: 10, 20, and 30. What are 

  34. Emir

    • 2015/11/1

    Calculator Use. The multiples of numbers calculator will find 100 multiples of a positive integer. For example, the multiples of 3 are calculated 3x1, 3x2, 3x3, 3x4

  35. Mazza

    • 2016/6/27

    If there is no answer return an empty string. So, if the input is like [7,2,8], then the output will be 87. To solve this, we will 

  36. Sonny

    • 2015/2/12

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

  37. Odin

    • 2021/1/9

    Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.

  38. Perez

    • 2019/10/29

    List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18, etc. The multiples of 5 are 5, 10, 15, 20, 25, etc. Find the first Common (same) value: The Least Common Multiple of 3 and 5 is 15. (15 is a multiple of both 3 and 5, and is the smallest number like that.)

  39. Mercier

    • 2015/9/17

    Given an integer array of digits , return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Example 2:

  40. Kashton

    • 2016/10/30

    The multiples of a whole number are found by taking the product of any counting number and that whole number. For example, to find the multiples of 3, multiply 3 by 1, 3 by 2, 3 by 3, and so on. To find the multiples of 5, multiply 5 by 1, 5 by 2, 5 by 3, and so on. The multiples are the products of these multiplications.

Comments are closed.

Recent Posts