Find the largest multiple of 3 - GeeksforGeeks
Given an array of non-negative integers. Find the largest multiple of 3 that can be formed from array elements.
1) A number is multiple of 3 if and only if the sum of digits of number is multiple of 3.
2) If a number is multiple of 3, then all permutations of it are also multiple of 3.
http://www.geeksforgeeks.org/find-largest-multiple-3-array-digits-set-2-time-o1-space/
http://ideone.com/vNjreJ
Constant space.
http://ideone.com/txZ88G
http://ideone.com/ZoXl5w
No sort: use count array.
Divisibility Rules
http://www.mathsisfun.com/divisibility-rules.html
3 The sum of the digits is divisible by 3
4 The last 2 digits are divisible by 4
6 The number is divisible by both 2 and 3
8 The last three digits are divisible by 8
9 The sum of the digits is divisible by 9
(Note: you can apply this rule to that answer again if you want)
11
If you sum every second digit and then subtract all other digits and the answer is: 0, or divisible by 11
Read full article from Find the largest multiple of 3 - GeeksforGeeks
Given an array of non-negative integers. Find the largest multiple of 3 that can be formed from array elements.
1) A number is multiple of 3 if and only if the sum of digits of number is multiple of 3.
2) If a number is multiple of 3, then all permutations of it are also multiple 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 digits 7, by 3, we get the same remainder 1.
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
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.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.
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.
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.
5. Finally empty all the queues into an auxiliary array. Sort the auxiliary array in non-increasing order. Output the auxiliary array.
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.
int findMaxMultupleOf3( int* arr, int size ){ // Step 1: sort the array in non-decreasing order qsort( arr, size, sizeof( int ), compareAsc ); // Create 3 queues to store numbers with remainder 0, 1 // and 2 respectively Queue* queue0 = createQueue( size ); Queue* queue1 = createQueue( size ); Queue* queue2 = createQueue( size ); // Step 2 and 3 get the sum of numbers and place them in // corresponding queues int i, sum; for ( i = 0, sum = 0; i < size; ++i ) { sum += arr[i]; if ( (arr[i] % 3) == 0 ) Enqueue( queue0, arr[i] ); else if ( (arr[i] % 3) == 1 ) Enqueue( queue1, arr[i] ); else Enqueue( queue2, arr[i] ); } // Step 4.2: The sum produces remainder 1 if ( (sum % 3) == 1 ) { // either remove one item from queue1 if ( !isEmpty( queue1 ) ) Dequeue( queue1 ); // or remove two items from queue2 else { if ( !isEmpty( queue2 ) ) Dequeue( queue2 ); else return 0; if ( !isEmpty( queue2 ) ) Dequeue( queue2 ); else return 0; } } // Step 4.3: The sum produces remainder 2 else if ((sum % 3) == 2) { // either remove one item from queue2 if ( !isEmpty( queue2 ) ) Dequeue( queue2 ); // or remove two items from queue1 else { if ( !isEmpty( queue1 ) ) Dequeue( queue1 ); else return 0; if ( !isEmpty( queue1 ) ) Dequeue( queue1 ); else return 0; } } int aux[size], top = 0; // Empty all the queues into an auxiliary array. populateAux (aux, queue0, queue1, queue2, &top); // sort the array in non-increasing order qsort (aux, top, sizeof( int ), compareDesc); // print the result printArr (aux, top); return top;}
If we get remainder either ‘1’ or ‘2’, we have to remove maximum two digits to make a number that is divisible by 3:
- 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’)
- 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 ).
// function to sort array of digits using// countsvoid 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 ind2bool 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[2]; rem_2[0] = -1, rem_2[1] = -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[0] == -1) rem_2[0] = i; // If second occurrence else if (rem_2[1] == -1) rem_2[1] = i; } } if (rem_2[0] != -1 && rem_2[1] != -1) { removeAndPrintResult(arr, n, rem_2[0], rem_2[1]); 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[2]; rem_1[0] = -1, rem_1[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[0] == -1) rem_1[0] = i; // If second occurrence else if (rem_1[1] == -1) rem_1[1] = i; } } if (rem_1[0] != -1 && rem_1[1] != -1) { removeAndPrintResult(arr, n, rem_1[0], rem_1[1]); return true; } } cout << "Not possible"; return false;}http://ideone.com/vNjreJ
Constant space.
http://ideone.com/txZ88G
http://ideone.com/ZoXl5w
No sort: use count array.
- void largest_number(int a[],int len)
- {
- int rem[3] = {0};
- for(int i=0;i<10;i++)
- table[i] = 0;
- int sum = 0;
- for(int i=0;i<len;i++)
- {
- sum += a[i];
- table[a[i]]++;
- rem[a[i]%3]++;
- }
- if(sum%3 == 1)
- {
- if(rem[1] < 1 && rem[2]<2)
- { cout << "not possible \n";
- return;
- }
- if(rem[1] > 0)
- {
- int remove_element = 1;
- for(int i=0;i<10 && remove_element > 0;i++)
- {
- if(i%3 == 1 && table[i] > 0)
- { table[i]--;
- remove_element--;
- }
- }
- }
- else
- {
- int remove_element = 2;
- for(int i=0;i<10 && remove_element > 0;i++)
- {
- if(i%3 == 2 && table[i] > 0)
- { table[i]--;
- remove_element--;
- }
- }
- }
- }
- if(sum%3 == 2)
- {
- if(rem[2]<1 && rem[1]<2)
- { cout << "not possible \n";
- return;
- }
- if(rem[2] > 0)
- {
- int remove_element = 1;
- for(int i=0;i<10 && remove_element > 0;i++)
- {
- if(i%3 == 2 && table[i] > 0)
- { table[i]--;
- remove_element--;
- }
- }
- }
- else
- {
- int remove_element = 2;
- for(int i=0;i<10 && remove_element > 0;i++)
- {
- if(i%3 == 1 && table[i] > 0)
- { table[i]--;
- remove_element--;
- }
- }
- }
- }
- for(int i=9;i>=0;i--)
- while(table[i]--)
- cout << i << " ";
- cout << endl;
- }
Divisibility Rules
http://www.mathsisfun.com/divisibility-rules.html
3 The sum of the digits is divisible by 3
4 The last 2 digits are divisible by 4
6 The number is divisible by both 2 and 3
8 The last three digits are divisible by 8
9 The sum of the digits is divisible by 9
(Note: you can apply this rule to that answer again if you want)
11
If you sum every second digit and then subtract all other digits and the answer is: 0, or divisible by 11
Read full article from Find the largest multiple of 3 - GeeksforGeeks