原文:
给定一个包含 n 正整数的数组 arr ,任务是检查给定的数组是否可以分解成两个排列,如果可能的话打印排列。如果从 1 到 m 的所有整数恰好只包含一次,则一个 m 整数序列被称为置换。 举例:
输入: arr[] = { 1,2,5,3,4,1,2 },n = 7 输出: {1 2 5 3 4},{1 2} 输入: arr[] = {2,1,1,3},n = 4 输出:不可能
进场:
- 首先,我们需要检查数组是否是两个排列的拼接。在文章中有解释。
- 如果是,找到数组中最大的元素,说 x 。
- 如果索引【0,x-1】和【x,n-1】处的元素形成两个有效排列,则打印它们。
- 否则,将索引【0,n-1–x】和【n–x,n–1】处的元素打印为两个有效排列。
以下是上述方法的实现:
c
// c program to print two
// permutations from a given sequence
#include
using namespace std;
// function to check if the sequence is
// concatenation of two permutations or not
bool checkpermutation(int arr[], int n)
{
// computing the sum of all the
// elements in the array
long long sum = 0;
for (int i = 0; i < n; i )
sum = arr[i];
// computing the prefix sum
// for all the elements in the array
long long prefix[n 1] = { 0 };
prefix[0] = arr[0];
for (int i = 1; i < n; i )
prefix[i] = prefix[i - 1] arr[i];
// iterating through the i
// from lengths 1 to n-1
for (int i = 0; i < n - 1; i ) {
// sum of first i 1 elements
long long lsum = prefix[i];
// sum of remaining n-i-1 elements
long long rsum = sum - prefix[i];
// lengths of the 2 permutations
long long l_len = i 1,
r_len = n - i - 1;
// checking if the sums
// satisfy the formula or not
if (((2 * lsum)
== (l_len * (l_len 1)))
&& ((2 * rsum)
== (r_len * (r_len 1))))
return true;
}
return false;
}
// function to print the
// two permutations
void printpermutations(int arr[], int n,
int l1, int l2)
{
// print the first permutation
for (int i = 0; i < l1; i ) {
cout << arr[i] << " ";
}
cout << endl;
// print the second permutation
for (int i = l1; i < n; i ) {
cout << arr[i] << " ";
}
}
// function to find the two permutations
// from the given sequence
void findpermutations(int arr[], int n)
{
// if the sequence is not a
// concatenation of two permutations
if (!checkpermutation(arr, n)) {
cout << "not possible";
return;
}
int l1 = 0, l2 = 0;
// find the largest element in the
// array and set the lengths of the
// permutations accordingly
l1 = *max_element(arr, arr n);
l2 = n - l1;
set s1, s2;
for (int i = 0; i < l1; i )
s1.insert(arr[i]);
for (int i = l1; i < n; i )
s2.insert(arr[i]);
if (s1.size() == l1 && s2.size() == l2)
printpermutations(arr, n, l1, l2);
else {
swap(l1, l2);
printpermutations(arr, n, l1, l2);
}
}
// driver code
int main()
{
int arr[] = { 2, 1, 3, 4, 5,
6, 7, 8, 9, 1,
10, 2 };
int n = sizeof(arr) / sizeof(int);
findpermutations(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print two
// permutations from a given sequence
import java.util.*;
class gfg{
// function to check if the sequence is
// concatenation of two permutations or not
static boolean checkpermutation(int arr[], int n)
{
// computing the sum of all the
// elements in the array
long sum = 0;
for (int i = 0; i < n; i )
sum = arr[i];
// computing the prefix sum
// for all the elements in the array
int []prefix = new int[n 1];
prefix[0] = arr[0];
for (int i = 1; i < n; i )
prefix[i] = prefix[i - 1] arr[i];
// iterating through the i
// from lengths 1 to n-1
for (int i = 0; i < n - 1; i ) {
// sum of first i 1 elements
long lsum = prefix[i];
// sum of remaining n-i-1 elements
long rsum = sum - prefix[i];
// lengths of the 2 permutations
long l_len = i 1,
r_len = n - i - 1;
// checking if the sums
// satisfy the formula or not
if (((2 * lsum)
== (l_len * (l_len 1)))
&& ((2 * rsum)
== (r_len * (r_len 1))))
return true;
}
return false;
}
// function to print the
// two permutations
static void printpermutations(int arr[], int n,
int l1, int l2)
{
// print the first permutation
for (int i = 0; i < l1; i ) {
system.out.print(arr[i] " ");
}
system.out.println();
// print the second permutation
for (int i = l1; i < n; i ) {
system.out.print(arr[i] " ");
}
}
// function to find the two permutations
// from the given sequence
static void findpermutations(int arr[], int n)
{
// if the sequence is not a
// concatenation of two permutations
if (!checkpermutation(arr, n)) {
system.out.print("not possible");
return;
}
int l1 = 0, l2 = 0;
// find the largest element in the
// array and set the lengths of the
// permutations accordingly
l1 = arrays.stream(arr).max().getasint();
l2 = n - l1;
hashset s1 = new hashset(),
s2 = new hashset();
for (int i = 0; i < l1; i )
s1.add(arr[i]);
for (int i = l1; i < n; i )
s2.add(arr[i]);
if (s1.size() == l1 && s2.size() == l2)
printpermutations(arr, n, l1, l2);
else {
l1 = l1 l2;
l2 = l1-l2;
l1 = l1-l2;
printpermutations(arr, n, l1, l2);
}
}
// driver code
public static void main(string[] args)
{
int arr[] = { 2, 1, 3, 4, 5,
6, 7, 8, 9, 1,
10, 2 };
int n = arr.length;
findpermutations(arr, n);
}
}
// this code is contributed by 29ajaykumar
python 3
# python3 program to print two
# permutations from a given sequence
# function to check if the sequence is
# concatenation of two permutations or not
def checkpermutation(arr, n):
# computing the sum of all the
# elements in the array
sum = 0
for i in range(n):
sum = arr[i]
# computing the prefix sum
# for all the elements in the array
prefix = [0 for i in range(n 1)]
prefix[0] = arr[0]
for i in range(1,n):
prefix[i] = prefix[i - 1] arr[i]
# iterating through the i
# from lengths 1 to n-1
for i in range(n - 1):
# sum of first i 1 elements
lsum = prefix[i]
# sum of remaining n-i-1 elements
rsum = sum - prefix[i]
# lengths of the 2 permutations
l_len = i 1
r_len = n - i - 1
# checking if the sums
# satisfy the formula or not
if (((2 * lsum) == (l_len * (l_len 1))) and
((2 * rsum) == (r_len * (r_len 1)))):
return true
return false
# function to print the
# two permutations
def printpermutations(arr,n,l1,l2):
# print the first permutation
for i in range(l1):
print(arr[i],end = " ")
print("\n",end = "");
# print the second permutation
for i in range(l1, n, 1):
print(arr[i], end = " ")
# function to find the two permutations
# from the given sequence
def findpermutations(arr,n):
# if the sequence is not a
# concatenation of two permutations
if (checkpermutation(arr, n) == false):
print("not possible")
return
l1 = 0
l2 = 0
# find the largest element in the
# array and set the lengths of the
# permutations accordingly
l1 = max(arr)
l2 = n - l1
s1 = set()
s2 = set()
for i in range(l1):
s1.add(arr[i])
for i in range(l1,n):
s2.add(arr[i])
if (len(s1) == l1 and len(s2) == l2):
printpermutations(arr, n, l1, l2)
else:
temp = l1
l1 = l2
l2 = temp
printpermutations(arr, n, l1, l2)
# driver code
if __name__ == '__main__':
arr = [2, 1, 3, 4, 5,6, 7, 8, 9, 1,10, 2]
n = len(arr)
findpermutations(arr, n)
# this code is contributed by surendra_gangwar
c
// c# program to print two
// permutations from a given sequence
using system;
using system.linq;
using system.collections.generic;
class gfg{
// function to check if the sequence is
// concatenation of two permutations or not
static bool checkpermutation(int []arr, int n)
{
// computing the sum of all the
// elements in the array
long sum = 0;
for (int i = 0; i < n; i )
sum = arr[i];
// computing the prefix sum
// for all the elements in the array
int []prefix = new int[n 1];
prefix[0] = arr[0];
for (int i = 1; i < n; i )
prefix[i] = prefix[i - 1] arr[i];
// iterating through the i
// from lengths 1 to n-1
for (int i = 0; i < n - 1; i ) {
// sum of first i 1 elements
long lsum = prefix[i];
// sum of remaining n-i-1 elements
long rsum = sum - prefix[i];
// lengths of the 2 permutations
long l_len = i 1,
r_len = n - i - 1;
// checking if the sums
// satisfy the formula or not
if (((2 * lsum)
== (l_len * (l_len 1)))
&& ((2 * rsum)
== (r_len * (r_len 1))))
return true;
}
return false;
}
// function to print the
// two permutations
static void printpermutations(int []arr, int n,
int l1, int l2)
{
// print the first permutation
for (int i = 0; i < l1; i ) {
console.write(arr[i] " ");
}
console.writeline();
// print the second permutation
for (int i = l1; i < n; i ) {
console.write(arr[i] " ");
}
}
// function to find the two permutations
// from the given sequence
static void findpermutations(int []arr, int n)
{
// if the sequence is not a
// concatenation of two permutations
if (!checkpermutation(arr, n)) {
console.write("not possible");
return;
}
int l1 = 0, l2 = 0;
// find the largest element in the
// array and set the lengths of the
// permutations accordingly
l1 = arr.max();
l2 = n - l1;
hashset s1 = new hashset(),
s2 = new hashset();
for (int i = 0; i < l1; i )
s1.add(arr[i]);
for (int i = l1; i < n; i )
s2.add(arr[i]);
if (s1.count == l1 && s2.count == l2)
printpermutations(arr, n, l1, l2);
else {
l1 = l1 l2;
l2 = l1-l2;
l1 = l1-l2;
printpermutations(arr, n, l1, l2);
}
}
// driver code
public static void main(string[] args)
{
int []arr = { 2, 1, 3, 4, 5,
6, 7, 8, 9, 1,
10, 2 };
int n = arr.length;
findpermutations(arr, n);
}
}
// this code contributed by rajput-ji
output:
2 1
3 4 5 6 7 8 9 1 10 2
时间复杂度:0(n)
辅助空间:o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处