Count maximum concatenation of pairs from given array that are divisible by 3
Last Updated :
28 Jul, 2021
Given an array arr[] of size N, the task is to count the pairs whose concatenation of elements is divisible by 3 and each array element present in at most one pair.
Examples:
Input: arr[] = { 5, 3, 2, 8, 7 }
Output: 1
Explanation:
Possible pairs whose concatenation is divisible by 3 are { 27, 72, 78, 87 }, but the array element arr[4] will be present in at most one pair. Therefore, the required output is 1
Input: arr[] = { 10, 6, 3, 7, 2 }
Output: 2
Naive approach: The simplest approach to solve this problem is to traverse the array and generate all possible pairs of the given array. For each pair check if the concatenation of elements of the pair is divisible by 3 or not. If found to be true, then mark both the element as false, so that both the elements of the pair can’t be present in more than one pair.
Below is the implementation of the naive approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDivBy3InArray( int arr[], int N)
{
int ans = 0;
bool taken[N];
memset (taken, false , sizeof (taken));
for ( int i = 0; i < N; i++)
{
if (taken[i] == true )
{
continue ;
}
for ( int j = i + 1; j < N; j++)
{
if (taken[j] == true )
{
continue ;
}
if (stoi(to_string(arr[i]) +
to_string(arr[j])) % 3 == 0 ||
stoi(to_string(arr[j]) +
to_string(arr[i])) % 3 == 0)
{
ans += 1;
taken[i] = true ;
taken[j] = true ;
}
}
}
return ans;
}
int main()
{
int arr[] = { 5, 3, 2, 8, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countDivBy3InArray(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
public static int countDivBy3InArray( int [] arr)
{
int ans = 0 ;
boolean [] taken = new boolean [arr.length];
Arrays.fill(taken, false );
for ( int i = 0 ; i < arr.length; i++)
{
if (taken[i] == true )
{
continue ;
}
for ( int j = i + 1 ; j < arr.length; j++)
{
if (taken[j] == true )
{
continue ;
}
if (Integer.parseInt(
Integer.toString(arr[i]) +
Integer.toString(arr[j])) % 3 == 0 ||
Integer.parseInt(
Integer.toString(arr[j]) +
Integer.toString(arr[i])) % 3 == 0 )
{
ans += 1 ;
taken[i] = true ;
taken[j] = true ;
}
}
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 5 , 3 , 2 , 8 , 7 };
System.out.println(countDivBy3InArray(arr));
}
}
|
Python3
def countDivBy3InArray(arr):
ans = 0
taken = [ False ] * len (arr)
for i in range ( len (arr)):
if taken[i]:
continue
for j in range (i + 1 , len (arr)):
if taken[j]:
continue
if ( not int ( str (arr[i]) + str (arr[j])) % 3 or
not int ( str (arr[j]) + str (arr[i])) % 3 ):
ans + = 1
taken[i] = True
taken[j] = True
return ans
arr = [ 5 , 3 , 2 , 8 , 7 ]
print (countDivBy3InArray(arr))
|
C#
using System;
public class GFG
{
public static int countDivBy3InArray( int [] arr)
{
int ans = 0;
bool [] taken = new bool [arr.Length];
for ( int i = 0; i < arr.Length; i++)
{
if (taken[i] == true )
{
continue ;
}
for ( int j = i + 1; j < arr.Length; j++)
{
if (taken[j] == true )
{
continue ;
}
if (Int32.Parse(
(arr[i]).ToString() +
(arr[j]).ToString()) % 3 == 0 ||
Int32.Parse(
(arr[j]).ToString() +
(arr[i]).ToString()) % 3 == 0)
{
ans += 1;
taken[i] = true ;
taken[j] = true ;
}
}
}
return ans;
}
public static void Main(String[] args)
{
int [] arr = { 5, 3, 2, 8, 7 };
Console.WriteLine(countDivBy3InArray(arr));
}
}
|
Javascript
<script>
function countDivBy3InArray(arr)
{
let ans = 0;
let taken = new Array(arr.length);
taken.fill( false );
for (let i = 0; i < arr.length; i++)
{
if (taken[i] == true )
{
continue ;
}
for (let j = i + 1; j < arr.length; j++)
{
if (taken[j] == true )
{
continue ;
}
if (parseInt((arr[i]).toString() + (arr[j]).toString(), 10) % 3 == 0 ||
parseInt((arr[j]).toString() + (arr[i]).toString(), 10) % 3 == 0)
{
ans += 1;
taken[i] = true ;
taken[j] = true ;
}
}
}
return ans;
}
let arr = [ 5, 3, 2, 8, 7 ];
document.write(countDivBy3InArray(arr));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient approach: The above approach can be optimized by using the concept of checking a number is divisible by 3 or not. Follow the steps below to solve the problem:
- Initialize three variables, say rem0, rem1 and rem2, to store the count of array elements whose remainder is 0, 1 and 2 respectively when divided by 3.
- Traverse the array and check for the following conditions:
- If arr[i] %3 == 0, then update cnt0 += 1.
- If arr[i] %3 == 1, then update cnt1 += 1.
- If arr[i] %3 == 2, then update cnt2 += 1.
- Finally, print the count of pairs, i.e. (rem0 / 2 + min(rem1, rem2)).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDiv( int arr[], int n)
{
int rem0 = 0;
int rem1 = 0;
int rem2 = 0;
for ( int i = 0; i < n; i++)
{
int digitSum = 0;
digitSum += arr[i];
if (digitSum % 3 == 0)
{
rem0 += 1;
}
else if (digitSum % 3 == 1)
{
rem1 += 1;
}
else
{
rem2 += 1;
}
}
return (rem0 / 2 + min(rem1, rem2));
}
int main()
{
int arr[] = { 5, 3, 2, 8, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << (countDiv(arr, n));
}
|
Java
public class GFG
{
static int countDiv( int [] arr)
{
int rem0 = 0 ;
int rem1 = 0 ;
int rem2 = 0 ;
for ( int i : arr)
{
int digitSum = 0 ;
digitSum += i;
if (digitSum % 3 == 0 )
{
rem0 += 1 ;
}
else if (digitSum % 3 == 1 )
{
rem1 += 1 ;
}
else
{
rem2 += 1 ;
}
}
return (rem0 / 2 + Math.min(rem1, rem2));
}
public static void main(String[] args) {
int [] arr = { 5 , 3 , 2 , 8 , 7 };
System.out.println(countDiv(arr));
}
}
|
Python3
def countDiv(arr):
rem0 = 0
rem1 = 0
rem2 = 0
for i in arr:
digitSum = 0
for digit in str (i):
digitSum + = int (digit)
if digitSum % 3 = = 0 :
rem0 + = 1
elif digitSum % 3 = = 1 :
rem1 + = 1
else :
rem2 + = 1
return (rem0 / / 2 + min (rem1, rem2))
arr = [ 5 , 3 , 2 , 8 , 7 ]
print (countDiv(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int countDiv( int [] arr)
{
int rem0 = 0;
int rem1 = 0;
int rem2 = 0;
foreach ( int i in arr)
{
int digitSum = 0;
digitSum += i;
if (digitSum % 3 == 0)
{
rem0 += 1;
}
else if (digitSum % 3 == 1)
{
rem1 += 1;
}
else
{
rem2 += 1;
}
}
return (rem0 / 2 + Math.Min(rem1, rem2));
}
static void Main() {
int [] arr = {5, 3, 2, 8, 7};
Console.Write(countDiv(arr));
}
}
|
Javascript
<script>
function countDiv(arr)
{
let rem0 = 0;
let rem1 = 0;
let rem2 = 0;
for (let i = 0; i < arr.length; i++)
{
let digitSum = 0;
digitSum += arr[i];
if (digitSum % 3 == 0)
{
rem0 += 1;
}
else if (digitSum % 3 == 1)
{
rem1 += 1;
}
else
{
rem2 += 1;
}
}
return (parseInt(rem0 / 2, 10) +
Math.min(rem1, rem2));
}
let arr = [5, 3, 2, 8, 7];
document.write(countDiv(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O()
Share your thoughts in the comments
Please Login to comment...