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”.

Method 1 (Brute Force)
The simple & straight forward approach is to generate all the combinations of the elements and keep track of the largest number formed which is divisible by 3.

Time Complexity: O(n x 2^n). There will be 2^n combinations of array elements. To compare each combination with the largest number so far may take O(n) time.
Auxiliary Space: O(n) // to avoid integer overflow, the largest number is assumed to be stored in the form of array.

[ad type=”banner”]

Method 2 (Tricky)
This problem can be solved efficiently with the help of O(n) extra space. This method is based on the following facts about numbers which are multiple of 3.

its 7, by 3, we get the same remainder 1.

  1. A number is multiple of 3 if and only if the sum of digits of number is multiple of 3. For example, let us consider 8760, it is a multiple of 3 because sum of digits is 8 + 7+ 6+ 0 = 21, which is a multiple of 3.
  2.  If a number is multiple of 3, then all permutations of it are also multiple of 3. For example, since 6078 is a multiple of 3, the numbers 8760, 7608, 7068, ….. are also multiples of 3.
  3.  We get the same remainder when we divide the number and sum of digits of the number. For example, if divide number 151 and sum of it digWhat is the idea behind above facts?
    The value of 10%3 and 100%3 is 1. The same is true for all the higher powers of 10, because 3 divides 9, 99, 999, … etc.
    Let us consider a 3 digit number n to prove above facts. Let the first, second and third digits of n be ‘a’, ‘b’ and ‘c’ respectively. n can be written as

    n = 100.a + 10.b + c

    Since (10^x)%3 is 1 for any x, the above expression gives the same remainder as following expression

     1.a + 1.b + c

So the remainder obtained by sum of digits and ‘n’ is same.

[ad type=”banner”]

Following is a solution based on the above observation.

1. Sort the array in non-decreasing order.

2. Take three queues. One for storing elements which on dividing by 3 gives remainder as 0.The second queue stores digits which on dividing by 3 gives remainder as 1. The third queue stores digits which on dividing by 3 gives remainder as 2. Call them as queue0, queue1 and queue2

3. Find the sum of all the digits.

4. Three cases arise:
4.1 The sum of digits is divisible by 3. Dequeue all the digits from the three queues. Sort them in non-increasing order. Output the array.

4.2 The sum of digits produces remainder 1 when divided by 3.
Remove one item from queue1. If queue1 is empty, remove two items from queue2. If queue2 contains less than two items, the number is not possible.

4.3 The sum of digits produces remainder 2 when divided by 3.
Remove one item from queue2. If queue2 is empty, remove two items from queue1. If queue1 contains less than two items, the number is not possible.

Finally empty all the queues into an auxiliary array. Sort the auxiliary array in non-increasing order. Output the auxiliary array.

Based on the above, below is C implementation:

The below code works only if the input arrays has numbers from 0 to 9. It can be easily extended for any positive integer array. We just have to modify the part where we sort the array in decreasing order, at the end of code.

[ad type=”banner”]

C Programming

[pastacode lang=”c” manual=”%2F*%20A%20program%20to%20find%20the%20largest%20multiple%20of%203%20from%20an%20array%20of%20elements%20*%2F%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20A%20queue%20node%0Atypedef%20struct%20Queue%0A%7B%0A%20%20%20%20int%20front%3B%0A%20%20%20%20int%20rear%3B%0A%20%20%20%20int%20capacity%3B%0A%20%20%20%20int*%20array%3B%0A%7D%20Queue%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20create%20a%20queue%20with%20given%20capacity%0AQueue*%20createQueue(%20int%20capacity%20)%0A%7B%0A%20%20%20%20Queue*%20queue%20%3D%20(Queue%20*)%20malloc%20(sizeof(Queue))%3B%0A%20%20%20%20queue-%3Ecapacity%20%3D%20capacity%3B%0A%20%20%20%20queue-%3Efront%20%3D%20queue-%3Erear%20%3D%20-1%3B%0A%20%20%20%20queue-%3Earray%20%3D%20(int%20*)%20malloc%20(queue-%3Ecapacity%20*%20sizeof(int))%3B%0A%20%20%20%20return%20queue%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20if%20queue%20is%20empty%0Aint%20isEmpty%20(Queue*%20queue)%0A%7B%0A%20%20%20%20return%20queue-%3Efront%20%3D%3D%20-1%3B%0A%7D%0A%20%0A%2F%2F%20A%20function%20to%20add%20an%20item%20to%20queue%0Avoid%20Enqueue%20(Queue*%20queue%2C%20int%20item)%0A%7B%0A%20%20%20%20queue-%3Earray%5B%20%2B%2Bqueue-%3Erear%20%5D%20%3D%20item%3B%0A%20%20%20%20if%20(%20isEmpty(queue)%20)%0A%20%20%20%20%20%20%20%20%2B%2Bqueue-%3Efront%3B%0A%7D%0A%20%0A%2F%2F%20A%20function%20to%20remove%20an%20item%20from%20queue%0Aint%20Dequeue%20(Queue*%20queue)%0A%7B%0A%20%20%20%20int%20item%20%3D%20queue-%3Earray%5B%20queue-%3Efront%20%5D%3B%0A%20%20%20%20if(%20queue-%3Efront%20%3D%3D%20queue-%3Erear%20)%0A%20%20%20%20%20%20%20%20queue-%3Efront%20%3D%20queue-%3Erear%20%3D%20-1%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20queue-%3Efront%2B%2B%3B%0A%20%0A%20%20%20%20return%20item%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20array%20contents%0Avoid%20printArr%20(int*%20arr%2C%20int%20size)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%3C%20size%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20printf%20(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%7D%0A%20%0A%2F*%20Following%20two%20functions%20are%20needed%20for%20library%20function%20qsort().%0A%20%20%20Refer%20following%20link%20for%20help%20of%20qsort()%0A%20%20%20http%3A%2F%2Fwww.cplusplus.com%2Freference%2Fclibrary%2Fcstdlib%2Fqsort%2F%20*%2F%0Aint%20compareAsc(%20const%20void*%20a%2C%20const%20void*%20b%20)%0A%7B%0A%20%20%20%20return%20*(int*)a%20%3E%20*(int*)b%3B%0A%7D%0Aint%20compareDesc(%20const%20void*%20a%2C%20const%20void*%20b%20)%0A%7B%0A%20%20%20%20return%20*(int*)a%20%3C%20*(int*)b%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20puts%20all%20elements%20of%203%20queues%20in%20the%20auxiliary%20array%0Avoid%20populateAux%20(int*%20aux%2C%20Queue*%20queue0%2C%20Queue*%20queue1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Queue*%20queue2%2C%20int*%20top%20)%0A%7B%0A%20%20%20%20%2F%2F%20Put%20all%20items%20of%20first%20queue%20in%20aux%5B%5D%0A%20%20%20%20while%20(%20!isEmpty(queue0)%20)%0A%20%20%20%20%20%20%20%20aux%5B%20(*top)%2B%2B%20%5D%20%3D%20Dequeue(%20queue0%20)%3B%0A%20%0A%20%20%20%20%2F%2F%20Put%20all%20items%20of%20second%20queue%20in%20aux%5B%5D%0A%20%20%20%20while%20(%20!isEmpty(queue1)%20)%0A%20%20%20%20%20%20%20%20aux%5B%20(*top)%2B%2B%20%5D%20%3D%20Dequeue(%20queue1%20)%3B%0A%20%0A%20%20%20%20%2F%2F%20Put%20all%20items%20of%20third%20queue%20in%20aux%5B%5D%0A%20%20%20%20while%20(%20!isEmpty(queue2)%20)%0A%20%20%20%20%20%20%20%20aux%5B%20(*top)%2B%2B%20%5D%20%3D%20Dequeue(%20queue2%20)%3B%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20that%20finds%20the%20largest%20possible%20multiple%20of%0A%2F%2F%203%20that%20can%20be%20formed%20by%20arr%5B%5D%20elements%0Aint%20findMaxMultupleOf3(%20int*%20arr%2C%20int%20size%20)%0A%7B%0A%20%20%20%20%2F%2F%20Step%201%3A%20sort%20the%20array%20in%20non-decreasing%20order%0A%20%20%20%20qsort(%20arr%2C%20size%2C%20sizeof(%20int%20)%2C%20compareAsc%20)%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%203%20queues%20to%20store%20numbers%20with%20remainder%200%2C%201%0A%20%20%20%20%2F%2F%20and%202%20respectively%0A%20%20%20%20Queue*%20queue0%20%3D%20createQueue(%20size%20)%3B%0A%20%20%20%20Queue*%20queue1%20%3D%20createQueue(%20size%20)%3B%0A%20%20%20%20Queue*%20queue2%20%3D%20createQueue(%20size%20)%3B%0A%20%0A%20%20%20%20%2F%2F%20Step%202%20and%203%20get%20the%20sum%20of%20numbers%20and%20place%20them%20in%0A%20%20%20%20%2F%2F%20corresponding%20queues%0A%20%20%20%20int%20i%2C%20sum%3B%0A%20%20%20%20for%20(%20i%20%3D%200%2C%20sum%20%3D%200%3B%20i%20%3C%20size%3B%20%2B%2Bi%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20sum%20%2B%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20if%20(%20(arr%5Bi%5D%20%25%203)%20%3D%3D%200%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20Enqueue(%20queue0%2C%20arr%5Bi%5D%20)%3B%0A%20%20%20%20%20%20%20%20else%20if%20(%20(arr%5Bi%5D%20%25%203)%20%3D%3D%201%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20Enqueue(%20queue1%2C%20arr%5Bi%5D%20)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20Enqueue(%20queue2%2C%20arr%5Bi%5D%20)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Step%204.2%3A%20The%20sum%20produces%20remainder%201%0A%20%20%20%20if%20(%20(sum%20%25%203)%20%3D%3D%201%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20either%20remove%20one%20item%20from%20queue1%0A%20%20%20%20%20%20%20%20if%20(%20!isEmpty(%20queue1%20)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20Dequeue(%20queue1%20)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20or%20remove%20two%20items%20from%20queue2%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20!isEmpty(%20queue2%20)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dequeue(%20queue2%20)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20!isEmpty(%20queue2%20)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dequeue(%20queue2%20)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Step%204.3%3A%20The%20sum%20produces%20remainder%202%0A%20%20%20%20else%20if%20((sum%20%25%203)%20%3D%3D%202)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20either%20remove%20one%20item%20from%20queue2%0A%20%20%20%20%20%20%20%20if%20(%20!isEmpty(%20queue2%20)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20Dequeue(%20queue2%20)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20or%20remove%20two%20items%20from%20queue1%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20!isEmpty(%20queue1%20)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dequeue(%20queue1%20)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20!isEmpty(%20queue1%20)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Dequeue(%20queue1%20)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20int%20aux%5Bsize%5D%2C%20top%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Empty%20all%20the%20queues%20into%20an%20auxiliary%20array.%0A%20%20%20%20populateAux%20(aux%2C%20queue0%2C%20queue1%2C%20queue2%2C%20%26top)%3B%0A%20%0A%20%20%20%20%2F%2F%20sort%20the%20array%20in%20non-increasing%20order%0A%20%20%20%20qsort%20(aux%2C%20top%2C%20sizeof(%20int%20)%2C%20compareDesc)%3B%0A%20%0A%20%20%20%20%2F%2F%20print%20the%20result%0A%20%20%20%20printArr%20(aux%2C%20top)%3B%0A%20%0A%20%20%20%20return%20top%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B8%2C%201%2C%207%2C%206%2C%200%7D%3B%0A%20%20%20%20int%20size%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20if%20(findMaxMultupleOf3(%20arr%2C%20size%20)%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20printf(%20%22Not%20Possible%22%20)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

The above method can be optimized in following ways.
1) We can use Heap Sort or Merge Sort to make the time complexity O(nLogn).

2) We can avoid extra space for queues. We know at most two items will be removed from the input array. So we can keep track of two items in two variables.

3) At the end, instead of sorting the array again in descending order, we can print the ascending sorted array in reverse order. While printing in reverse order, we can skip the two elements to be removed.

Time Complexity: O(nLogn), assuming a O(nLogn) algorithm is used for sorting.
[ad type=”banner”]

Categorized in: