# Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)

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.

Examples:

```Input : arr[] = {5, 4, 3, 1, 1}
Output : 4311

Input : Arr[] = {5, 5, 5, 7}
Output : 555
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We have discussed a queue based solution. Both solutions (discussed in previous and this posts) are based on the fact that a number is divisible by 3 if and only if sum of digits of the number is divisible by 3.
For example, let us consider 555, it is divisible by 3 because sum of digits is 5 + 5 + 5 = 15, which is divisible by 3. If a sum of digits is not divisible by 3 then the remainder should be either 1 or 2.
If we get remainder either ‘1’ or ‘2’, we have to remove maximum two digits to make a number that is divisible by 3:

1. If remainder is ‘1’ : We have to remove single digit that have remainder ‘1’ or we have to remove two digit that have remainder ‘2’ ( 2 + 2 => 4 % 3 => ‘1’)
2. If remainder is ‘2’ : .We have to remove single digit that have remainder ‘2’ or we have to remove two digit that have remainder ‘1’ ( 1 + 1 => 2 % 3 => 2 ).

Examples :

```Input : arr[] = 5, 5, 5, 7
Sum of digits = 5 + 5 + 7 = 22
Remainder = 22 % 3 = 1
We remove smallest single digit that
has remainder '1'. We remove 7 % 3 = 1
So largest number divisible by 3 is : 555

Let's take an another example :
Input : arr[]  = 4 , 4 , 1 , 1 , 1 , 3
Sum of digits  = 4 + 4 + 1 + 1 + 1 + 3 = 14
Reminder = 14 % 3 = 2
We have to remove the smallest digit that
has remainder ' 2 ' or two digits that have
remainder '1'. Here there is no digit with
reminder '2', so we have to remove two smallest
digits that have remainder '1'. The digits are :
1, 1. So largest number divisible by 3 is 4 4 3 1
```

Below is C++ implementation of above idea.

 `// C++ program to find the largest number ` `// that can be mode from elements of the ` `// array and is divisible by 3 ` `#include ` `using` `namespace` `std; ` ` `  `// Number of digits ` `#define MAX_SIZE 10 ` ` `  `// function to sort array of digits using ` `// counts ` `void` `sortArrayUsingCounts(``int` `arr[], ``int` `n) ` `{ ` `    ``// Store count of all elements ` `    ``int` `count[MAX_SIZE] = {0}; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``count[arr[i]]++; ` ` `  `    ``// Store ` `    ``int` `index = 0; ` `    ``for` `(``int` `i = 0; i < MAX_SIZE; i++) ` `        ``while` `(count[i] > 0) ` `            ``arr[index++] = i, count[i]--; ` `} ` ` `  `// Remove elements from arr[] at indexes ind1 and ind2 ` `bool` `removeAndPrintResult(``int` `arr[], ``int` `n, ``int` `ind1, ` `                                        ``int` `ind2 = -1) ` `{ ` `    ``for` `(``int` `i = n-1; i >=0; i--) ` `        ``if` `(i != ind1 && i != ind2) ` `            ``cout << arr[i] ; ` `} ` ` `  `// Returns largest multiple of 3 that can be formed ` `// using arr[] elements. ` `bool` `largest3Multiple(``int` `arr[], ``int` `n) ` `{ ` `    ``// Sum of all array element ` `    ``int` `sum = accumulate(arr, arr+n, 0); ` ` `  `    ``// Sum is divisible by 3 , no need to ` `    ``// delete an element ` `    ``if` `(sum%3 == 0) ` `        ``return` `true` `; ` ` `  `    ``// Sort array element in increasing order ` `    ``sortArrayUsingCounts(arr, n); ` ` `  `    ``// Find reminder ` `    ``int` `remainder = sum % 3; ` ` `  `    ``// If remainder is '1', we have to delete either ` `    ``// one element of remainder '1' or two elements ` `    ``// of remainder '2' ` `    ``if` `(remainder == 1) ` `    ``{ ` `        ``int` `rem_2; ` `        ``rem_2 = -1, rem_2 = -1; ` ` `  `        ``// Traverse array elements ` `        ``for` `(``int` `i = 0 ; i < n ; i++) ` `        ``{ ` `            ``// Store first element of remainder '1' ` `            ``if` `(arr[i]%3 == 1) ` `            ``{ ` `                ``removeAndPrintResult(arr, n, i); ` `                ``return` `true``; ` `            ``} ` ` `  `            ``if` `(arr[i]%3 == 2) ` `            ``{ ` `                ``// If this is first occurrence of remainder 2 ` `                ``if` `(rem_2 == -1) ` `                    ``rem_2 = i; ` ` `  `                ``// If second occurrence ` `                ``else` `if` `(rem_2 == -1) ` `                    ``rem_2 = i; ` `            ``} ` `        ``} ` ` `  `        ``if` `(rem_2 != -1 && rem_2 != -1) ` `        ``{ ` `            ``removeAndPrintResult(arr, n, rem_2, rem_2); ` `            ``return` `true``; ` `        ``} ` `    ``} ` ` `  `    ``// If remainder is '2', we have to delete either ` `    ``// one element of remainder '2' or two elements ` `    ``// of remainder '1' ` `    ``else` `if` `(remainder == 2) ` `    ``{ ` `        ``int` `rem_1; ` `        ``rem_1 = -1, rem_1 = -1; ` ` `  `        ``// traverse array elements ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``// store first element of remainder '2' ` `            ``if` `(arr[i]%3 == 2) ` `            ``{ ` `                ``removeAndPrintResult(arr, n, i); ` `                ``return` `true``; ` `            ``} ` ` `  `            ``if` `(arr[i]%3 == 1) ` `            ``{ ` `                ``// If this is first occurrence of remainder 1 ` `                ``if` `(rem_1 == -1) ` `                    ``rem_1 = i; ` ` `  `                ``// If second occurrence ` `                ``else` `if` `(rem_1 == -1) ` `                    ``rem_1 = i; ` `            ``} ` `        ``} ` ` `  `        ``if` `(rem_1 != -1 && rem_1 != -1) ` `        ``{ ` `            ``removeAndPrintResult(arr, n, rem_1, rem_1); ` `            ``return` `true``; ` `        ``} ` `    ``} ` ` `  `    ``cout << ``"Not possible"``; ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = {4 , 4 , 1 , 1 , 1 , 3} ; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` `    ``largest3Multiple(arr, n); ` `    ``return` `0; ` `} `

Output:

``` 555
```