Check if any permutation of array contains sum of every adjacent pair not divisible by 3
Given an array arr[] consisting of N integers, the task is to check if any permutation of the array elements exists where the sum of every pair of adjacent elements is not divisible by 3. If it is possible, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {1, 2, 3, 3}
Output: Yes
Explanation:
Since there exist at least 1 combination {3, 2, 3, 1} where sum of every adjacent pairs is not divisible by 3.
Input: arr[] = {3, 6, 1, 9}
Output: No
Naive Approach: The simplest approach is to generate all permutations of the given array and check if there exists an arrangement in which the sum of no two adjacent elements is divisible by 3. If it is found to be true, then print “Yes”. Otherwise, print “No”.
Time Complexity: O(N!)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe that the only possible remainders for all the array elements i.e., {0, 1, 2}. To segregate these three numbers in such a way that the sum of two adjacent elements is not divisible by 3. Follow the steps below:
- Count all the numbers into three parts having remainder 0, 1, and 2. Let the count be a, b, and c respectively.
- Now arrange the numbers having remainder as 0 with the numbers having remainder as 1 or 2 such that their sum will not be divisible by 3. Below are the conditions where this condition can be true:
- If a ? 1 and a ? b + c + 1
- If a and b both are equals to 0 and c > 0
- If a and c both are equals to 0 and b > 0
- If there is no way to arrange all the numbers in the above way then there is no permutation such that their sum of adjacent elements is not divisible by 3. Therefore, print “No”.
- If the condition in Step 2 is found to be true, then print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void factorsOf3( int arr[], int N)
{
int a = 0, b = 0, c = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 3 == 0)
a++;
else if (arr[i] % 3 == 1)
b++;
else if (arr[i] % 3 == 2)
c++;
}
if (a >= 1 && a <= b + c + 1)
cout << "Yes" << endl;
else if (a == 0 && b == 0 && c > 0)
cout << "Yes" << endl;
else if (a == 0 && c == 0 && b > 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int arr[] = { 1, 2, 3, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
factorsOf3(arr, N);
return 0;
}
|
Java
class GFG{
static void factorsOf3( int arr[], int N)
{
int a = 0 , b = 0 , c = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] % 3 == 0 )
a++;
else if (arr[i] % 3 == 1 )
b++;
else if (arr[i] % 3 == 2 )
c++;
}
if (a >= 1 && a <= b + c + 1 )
System.out.print( "Yes" + "\n" );
else if (a == 0 && b == 0 && c > 0 )
System.out.print( "Yes" + "\n" );
else if (a == 0 && c == 0 && b > 0 )
System.out.print( "Yes" + "\n" );
else
System.out.print( "No" + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 3 };
int N = arr.length;
factorsOf3(arr, N);
}
}
|
Python3
def factorsOf3(arr, N):
a = 0
b = 0
c = 0
for i in range (N):
if (arr[i] % 3 = = 0 ):
a + = 1
elif (arr[i] % 3 = = 1 ):
b + = 1
elif (arr[i] % 3 = = 2 ):
c + = 1
if (a > = 1 and a < = b + c + 1 ):
print ( "Yes" )
elif (a = = 0 and b = = 0 and c > 0 ):
print ( "Yes" )
elif (a = = 0 and c = = 0 and b > 0 ):
print ( "Yes" )
else :
print ( "No" )
arr = [ 1 , 2 , 3 , 3 ]
N = len (arr)
factorsOf3(arr, N)
|
C#
using System;
class GFG{
static void factorsOf3( int []arr,
int N)
{
int a = 0, b = 0, c = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] % 3 == 0)
a++;
else if (arr[i] % 3 == 1)
b++;
else if (arr[i] % 3 == 2)
c++;
}
if (a >= 1 && a <= b + c + 1)
Console.Write( "Yes" + "\n" );
else if (a == 0 && b == 0 && c > 0)
Console.Write( "Yes" + "\n" );
else if (a == 0 && c == 0 && b > 0)
Console.Write( "Yes" + "\n" );
else
Console.Write( "No" + "\n" );
}
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 3};
int N = arr.Length;
factorsOf3(arr, N);
}
}
|
Javascript
<script>
function factorsOf3(arr, N)
{
let a = 0, b = 0, c = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] % 3 == 0)
a++;
else if (arr[i] % 3 == 1)
b++;
else if (arr[i] % 3 == 2)
c++;
}
if (a >= 1 && a <= b + c + 1)
document.write( "Yes" + "<br/>" );
else if (a == 0 && b == 0 && c > 0)
document.write( "Yes" + "<br/>" );
else if (a == 0 && c == 0 && b > 0)
document.write( "Yes" + "<br/>" );
else
document.write( "No" + "<br/>" );
}
let arr = [ 1, 2, 3, 3 ];
let N = arr.length;
factorsOf3(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...