给定一个包含正整数的数组arr ,任务是检查给定的数组arr是否为两个排列的串联。如果M个整数序列只包含一次从1到M的所有整数,则称为置换。
例子:
Input: arr[] = {1, 2, 5, 3, 4, 1, 1}
Output: No
Explanation:
Given array contains 1 thrice. The first 5 elements form a permutation of length 5, but the remaining 2 elements do not form a permutation.
Input: arr[] = {1, 2, 5, 3, 4, 1, 2}
Output: Yes
Explanation:
Given array arr[] = {1, 2, 5, 3, 4} + {1, 2}
The first 5 elements form a permutation of length 5 and the remaining 2 elements form a permutation of length 2.
方法:
- 遍历给定数组并计算所有元素的总和。
- 形成一个包含前缀和的前缀数组。
- 现在,对于范围[1,N)中的每个索引
- 使用以下条件检查从开始索引到当前索引的元素是否形成排列:
Sum of K elements = Sum of K natural numbers where K is the current index
- 然后检查其余元素是否形成排列。
- 如果是,则我们打印/返回“是”。
- 使用以下条件检查从开始索引到当前索引的元素是否形成排列:
下面是上述方法的实现:
C++
// C++ program to check if a given sequence
// is a concatenation of two permutations or not
#include
using namespace std;
// Function to Check if a given sequence
// is a 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;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 5, 3, 4, 1, 2 };
int n = sizeof(arr) / sizeof(int);
if (checkPermutation(arr, n))
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
Java
// Java program to check if a given sequence
// is a concatenation of two permutations or not
import java.util.*;
class GFG{
// Function to Check if a given sequence
// is a concatenation of two permutations or not
static boolean checkPermutation(int []arr, int n)
{
// Computing the sum of all the
// elements in the array
int 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];
Arrays.fill(prefix,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
int lsum = prefix[i];
// Sum of remaining n-i-1 elements
int rsum = sum - prefix[i];
// Lengths of the 2 permutations
int 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;
}
// Driver code
public static void main(String args[])
{
int []arr = { 1, 2, 5, 3, 4, 1, 2 };
int n = arr.length;
if (checkPermutation(arr, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Surendra_Gangwar
Python3
# Python program to check if a given sequence
# is a concatenation of two permutations or not
# Function to Check if a given sequence
# is a 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]*(n + 1);
prefix[0] = arr[0];
for i in range(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;
# Driver code
if __name__=='__main__':
arr = [ 1, 2, 5, 3, 4, 1, 2 ]
n = len(arr)
if (checkPermutation(arr, n)):
print("Yes");
else:
print("No");
# This code is contributed by Princi Singh
C#
// C# program to check if a given sequence
// is a concatenation of two permutations or not
using System;
class GFG{
// Function to Check if a given sequence
// is a concatenation of two permutations or not
static bool checkPermutation(int []arr, int n)
{
// Computing the sum of all the
// elements in the array
int 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
int lsum = prefix[i];
// Sum of remaining n-i-1 elements
int rsum = sum - prefix[i];
// Lengths of the 2 permutations
int 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;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 1, 2, 5, 3, 4, 1, 2 };
int n = arr.Length;
if (checkPermutation(arr, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Rajput-Ji
输出:
Yes