Permutations of an array having sum of Bitwise AND of adjacent elements at least K
Last Updated :
08 Jun, 2021
Given an array arr[] consisting of N integers and a positive integer K, the task is to find all permutations of the array arr[] such that the sum of Bitwise AND of adjacent elements in each permutation is greater than or equal to K. If no such permutation exists, print “-1”.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 8
Output:
2, 3, 1, 5, 4
4, 5, 1, 3, 2
Explanation:
For the permutation {2, 3, 1, 5, 4}: (2 & 3) + (3 & 1) + (1 & 5) + (5 & 4) = 8, which is at least K( = 8).
For the permutation {4, 5, 1, 3, 2}: (4 & 5) + (5 & 1) + (1 & 3) + (3 & 2) = 8, which is at least K( = 8).
Input: arr[] = {1, 2, 3}, K = 4
Output: -1
Approach: The idea is to generate all possible permutations of arr[] and check for each permutation, if the required condition is satisfied or not.
Follow the steps below to solve the problem:
- Initialize a boolean variable, say flag as false, to check if any required permutation exists or not.
- Generate all possible permutations of the array arr[] and perform the following steps:
- Calculate the sum of Bitwise AND of all adjacent pairs of array elements in the current permutation and store t in a variable, say sum.
- Traverse the current permutation over the range [0, N – 2] and add Bitwise AND of arr[i] and arr[i + 1] to the sum.
- If the value of sum is at least K, then set the flag to true and print the current permutation.
- After completing the above steps, If the value of flag is false, the print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printPermutation( int arr[], int n,
int k)
{
bool flag = false ;
sort(arr, arr + n);
do {
int sum = 0;
for ( int i = 0; i < n - 1; i++) {
sum += arr[i] & arr[i + 1];
}
if (sum >= k) {
flag = true ;
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout << "\n" ;
}
} while (next_permutation(arr, arr + n));
if (flag == false ) {
cout << "-1" ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int K = 8;
int N = sizeof (arr) / sizeof (arr[0]);
printPermutation(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printPermutation( int arr[], int n,
int k)
{
boolean flag = false ;
Arrays.sort(arr);
do
{
int sum = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
sum += arr[i] & arr[i + 1 ];
}
if (sum >= k)
{
flag = true ;
for ( int i = 0 ; i < n; i++)
{
System.out.print(arr[i]+ " " );
}
System.out.print( "\n" );
}
} while (next_permutation(arr));
if (flag == false )
{
System.out.print( "-1" );
}
}
static boolean next_permutation( int [] p) {
for ( int a = p.length - 2 ; a >= 0 ; --a)
if (p[a] < p[a + 1 ])
for ( int b = p.length - 1 ;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int K = 8 ;
int N = arr.length;
printPermutation(arr, N, K);
}
}
|
C#
using System;
class GFG{
static void printPermutation( int []arr, int n,
int k)
{
bool flag = false ;
Array.Sort(arr);
do
{
int sum = 0;
for ( int i = 0; i < n - 1; i++)
{
sum += arr[i] & arr[i + 1];
}
if (sum >= k)
{
flag = true ;
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i] + " " );
}
Console.Write( "\n" );
}
} while (next_permutation(arr));
if (flag == false )
{
Console.Write( "-1" );
}
}
static bool next_permutation( int [] p) {
for ( int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for ( int b = p.Length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int K = 8;
int N = arr.Length;
printPermutation(arr, N, K);
}
}
|
Python3
def next_permutation(a):
for i in reversed ( range ( len (a) - 1 )):
if a[i] < a[i + 1 ]:
break
else :
return False
j = next (j for j in
reversed ( range (i + 1 , len (a)))
if a[i] < a[j])
a[i], a[j] = a[j], a[i]
a[i + 1 :] = reversed (a[i + 1 :])
return True
def printPermutation(arr, n, k):
flag = False
arr.sort()
while True :
sum = 0
for i in range (n - 1 ):
sum + = arr[i] & arr[i + 1 ]
if ( sum > = k):
flag = True
for i in range (n):
print (arr[i], end = " " )
print ()
if (next_permutation(arr) = = False ):
break
if (flag = = False ):
print ( "-1" )
arr = [ 1 , 2 , 3 , 4 , 5 ]
K = 8
N = len (arr)
printPermutation(arr, N, K)
|
Javascript
<script>
function printPermutation(arr, n, k)
{
let flag = false ;
arr.sort();
do
{
let sum = 0;
for (let i = 0; i < n - 1; i++)
{
sum += arr[i] & arr[i + 1];
}
if (sum >= k)
{
flag = true ;
for (let i = 0; i < n; i++)
{
document.write(arr[i]+ " " );
}
document.write( "<br/>" );
}
} while (next_permutation(arr));
if (flag == false )
{
System.out.print( "-1" );
}
}
function next_permutation(p) {
for (let a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (let b = p.length - 1;; --b)
if (p[b] > p[a]) {
let t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
let arr = [ 1, 2, 3, 4, 5 ];
let K = 8;
let N = arr.length;
printPermutation(arr, N, K);
</script>
|
Output:
2 3 1 5 4
4 5 1 3 2
Time Complexity: O(N*(N!))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...