给定一个由N个整数和一个正整数K组成的数组arr [] ,任务是找到数组arr []的所有排列,以使每个排列中相邻元素的按位与之和大于或等于K。如果不存在这样的排列,请打印“ -1” 。
例子:
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
方法:想法是生成arr []的所有可能排列,并检查每个排列,是否满足所需条件。
请按照以下步骤解决问题:
- 初始化一个布尔变量,将其标记为false ,以检查是否存在任何必需的排列。
- 生成数组arr []的所有可能排列,并执行以下步骤:
- 计算当前排列中所有相邻对数组元素对的按位与之和,并将t存储在变量中,例如sum 。
- 遍历过的范围中的当前排列[0,N – 2],添加按位与改编的[i]和常用3 [I + 1]的总和。
- 如果sum的值至少为K ,则将标志设置为true并打印当前排列。
- 完成上述步骤后,如果flag的值为false ,则打印“ -1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print all permutations of
// arr[] such that the sum of Bitwise AND
// of all adjacent element is at least K
void printPermutation(int arr[], int n,
int k)
{
// To check if any permutation
// exists or not
bool flag = false;
// Sort the given array
sort(arr, arr + n);
// Find all possible permutations
do {
// Stores the sum of bitwise AND
// of adjcent elements of the
// current permuatation
int sum = 0;
// Traverse the current
// permutation of arr[]
for (int i = 0; i < n - 1; i++) {
// Update the sum
sum += arr[i] & arr[i + 1];
}
// If the sum is at least K, then
// print the current permutation
if (sum >= k) {
// Set the flag variable
flag = true;
// Print the current permutation
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}
} while (next_permutation(arr, arr + n));
// If flag is unset, then print -1
if (flag == false) {
cout << "-1";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int K = 8;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
printPermutation(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print all permutations of
// arr[] such that the sum of Bitwise AND
// of all adjacent element is at least K
static void printPermutation(int arr[], int n,
int k)
{
// To check if any permutation
// exists or not
boolean flag = false;
// Sort the given array
Arrays.sort(arr);
// Find all possible permutations
do
{
// Stores the sum of bitwise AND
// of adjcent elements of the
// current permuatation
int sum = 0;
// Traverse the current
// permutation of arr[]
for (int i = 0; i < n - 1; i++)
{
// Update the sum
sum += arr[i] & arr[i + 1];
}
// If the sum is at least K, then
// print the current permutation
if (sum >= k)
{
// Set the flag variable
flag = true;
// Print the current permutation
for (int i = 0; i < n; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.print("\n");
}
} while (next_permutation(arr));
// If flag is unset, then print -1
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;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int K = 8;
int N = arr.length;
// Function Call
printPermutation(arr, N, K);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
class GFG{
// Function to print all permutations of
// []arr such that the sum of Bitwise AND
// of all adjacent element is at least K
static void printPermutation(int []arr, int n,
int k)
{
// To check if any permutation
// exists or not
bool flag = false;
// Sort the given array
Array.Sort(arr);
// Find all possible permutations
do
{
// Stores the sum of bitwise AND
// of adjcent elements of the
// current permuatation
int sum = 0;
// Traverse the current
// permutation of []arr
for (int i = 0; i < n - 1; i++)
{
// Update the sum
sum += arr[i] & arr[i + 1];
}
// If the sum is at least K, then
// print the current permutation
if (sum >= k)
{
// Set the flag variable
flag = true;
// Print the current permutation
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
Console.Write("\n");
}
} while (next_permutation(arr));
// If flag is unset, then print -1
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;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int K = 8;
int N = arr.Length;
// Function Call
printPermutation(arr, N, K);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
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
# Function to print all permutations of
# arr[] such that the sum of Bitwise AND
# of all adjacent element is at least K
def printPermutation(arr, n, k):
# To check if any permutation
# exists or not
flag = False
# Sort the given array
arr.sort()
# Find all possible permutations
while True:
# Stores the sum of bitwise AND
# of adjcent elements of the
# current permuatation
sum = 0
# Traverse the current
# permutation of arr[]
for i in range(n - 1):
# Update the sum
sum += arr[i] & arr[i + 1]
# If the sum is at least K, then
# print the current permutation
if (sum >= k):
# Set the flag variable
flag = True
# Print the current permutation
for i in range(n):
print(arr[i], end = " ")
print()
if (next_permutation(arr) == False):
break
# If flag is unset, then print -1
if (flag == False):
print("-1")
# Driver Code
arr = [1, 2, 3, 4, 5]
K = 8
N = len(arr)
# Function Call
printPermutation(arr, N, K)
# This code is contributed by Dharanendra L V
Javascript
输出:
2 3 1 5 4
4 5 1 3 2
时间复杂度: O(N *(N!))
辅助空间: O(1)