Print all the combinations of N elements by changing sign such that their sum is divisible by M
Last Updated :
29 Aug, 2022
Given an array of N integers and an integer M. You can change the sign(positive or negative) of any element in the array. The task is to print all possible combinations of the array elements that can be obtained by changing the sign of the elements such that their sum is divisible by M.
Note: You have to take all of the array elements in each combination and in the same order as the elements present in the array. However, you can change the sign of elements.
Examples:
Input: a[] = {5, 6, 7}, M = 3
Output:
-5-6-7
+5-6+7
-5+6-7
+5+6+7
Input: a[] = {3, 5, 6, 8}, M = 5
Output:
-3-5+6-8
-3+5+6-8
+3-5-6+8
+3+5-6+8
Approach: The concept of power set is used here to solve this problem. Using power-set generate all possible combinations of signs that can be applied to the array of elements. If the sum obtained is divisible by M, then print the combination. Below are the steps:
- Iterate for all possible combinations of ‘+’ and ‘-‘ using power set.
- Iterate on the array elements and if the j-th bit from left is set, then assume the array element to be positive and if the bit is not set, then assume the array element to be negative. Refer here to check if bit any index is set or not.
- If the sum is divisible by M, then the again traverse the array elements and print them along with sign( ‘+’ or ‘-‘ ).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printCombinations( int a[], int n, int m)
{
for ( int i = 0; i < (1 << n); i++) {
int sum = 0;
int num = 1 << (n - 1);
for ( int j = 0; j < n; j++) {
if (i & num)
sum += a[j];
else
sum += (-1 * a[j]);
num = num >> 1;
}
if (sum % m == 0) {
num = 1 << (n - 1);
for ( int j = 0; j < n; j++) {
if ((i & num))
cout << "+" << a[j] << " " ;
else
cout << "-" << a[j] << " " ;
num = num >> 1;
}
cout << endl;
}
}
}
int main()
{
int a[] = { 3, 5, 6, 8 };
int n = sizeof (a) / sizeof (a[0]);
int m = 5;
printCombinations(a, n, m);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printCombinations( int a[],
int n, int m)
{
for ( int i = 0 ;
i < ( 1 << n); i++)
{
int sum = 0 ;
int num = 1 << (n - 1 );
for ( int j = 0 ; j < n; j++)
{
if ((i & num) > 0 )
sum += a[j];
else
sum += (- 1 * a[j]);
num = num >> 1 ;
}
if (sum % m == 0 )
{
num = 1 << (n - 1 );
for ( int j = 0 ; j < n; j++)
{
if ((i & num) > 0 )
System.out.print( "+" +
a[j] + " " );
else
System.out.print( "-" +
a[j] + " " );
num = num >> 1 ;
}
System.out.println();
}
}
}
public static void main(String args[])
{
int a[] = { 3 , 5 , 6 , 8 };
int n = a.length;
int m = 5 ;
printCombinations(a, n, m);
}
}
|
Python3
def printCombinations(a, n, m):
for i in range ( 0 , ( 1 << n)):
sum = 0
num = 1 << (n - 1 )
for j in range ( 0 , n):
if ((i & num) > 0 ):
sum + = a[j]
else :
sum + = ( - 1 * a[j])
num = num >> 1
if ( sum % m = = 0 ):
num = 1 << (n - 1 )
for j in range ( 0 , n):
if ((i & num) > 0 ):
print ( "+" , a[j], end = " " ,
sep = "")
else :
print ( "-" , a[j], end = " " ,
sep = "")
num = num >> 1
print ("")
a = [ 3 , 5 , 6 , 8 ]
n = len (a)
m = 5
printCombinations(a, n, m)
|
C#
using System;
class GFG
{
static void printCombinations( int []a,
int n, int m)
{
for ( int i = 0;
i < (1 << n); i++)
{
int sum = 0;
int num = 1 << (n - 1);
for ( int j = 0; j < n; j++)
{
if ((i & num) > 0)
sum += a[j];
else
sum += (-1 * a[j]);
num = num >> 1;
}
if (sum % m == 0)
{
num = 1 << (n - 1);
for ( int j = 0; j < n; j++)
{
if ((i & num) > 0)
Console.Write( "+" +
a[j] + " " );
else
Console.Write( "-" +
a[j] + " " );
num = num >> 1;
}
Console.Write( "\n" );
}
}
}
public static void Main()
{
int []a = { 3, 5, 6, 8 };
int n = a.Length;
int m = 5;
printCombinations(a, n, m);
}
}
|
PHP
<?php
function printCombinations( $a , $n , $m )
{
for ( $i = 0; $i < (1 << $n ); $i ++)
{
$sum = 0;
$num = 1 << ( $n - 1);
for ( $j = 0; $j < $n ; $j ++)
{
if ( $i & $num )
$sum += $a [ $j ];
else
$sum += (-1 * $a [ $j ]);
$num = $num >> 1;
}
if ( $sum % $m == 0)
{
$num = 1 << ( $n - 1);
for ( $j = 0; $j < $n ; $j ++)
{
if (( $i & $num ))
echo "+" , $a [ $j ] , " " ;
else
echo "-" , $a [ $j ] , " " ;
$num = $num >> 1;
}
echo "\n" ;
}
}
}
$a = array ( 3, 5, 6, 8 );
$n = sizeof( $a );
$m = 5;
printCombinations( $a , $n , $m );
?>
|
Javascript
<script>
function printCombinations(a, n, m)
{
for (let i = 0; i < (1 << n); i++)
{
let sum = 0;
let num = 1 << (n - 1);
for (let j = 0; j < n; j++)
{
if ((i & num) > 0)
sum += a[j];
else
sum += (-1 * a[j]);
num = num >> 1;
}
if (sum % m == 0)
{
num = 1 << (n - 1);
for (let j = 0; j < n; j++)
{
if ((i & num) > 0)
document.write( "+" + a[j] + " " );
else
document.write( "-" + a[j] + " " );
num = num >> 1;
}
document.write( "</br>" );
}
}
}
let a = [ 3, 5, 6, 8 ];
let n = a.length;
let m = 5;
printCombinations(a, n, m);
</script>
|
Output
-3 -5 +6 -8
-3 +5 +6 -8
+3 -5 -6 +8
+3 +5 -6 +8
Time Complexity: O(2N * N), where N is the number of elements.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...