Check if a Sequence is a concatenation of two permutations
Last Updated :
24 Nov, 2021
Given an array arr containing positive integers, the task is to check if the given array arr is a concatenation of two permutations or not. A sequence of M integers is called a permutation if it contains all integers from 1 to M exactly once.
Examples:
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.
Approach:
- Traverse through the given array and calculate the sum of all the elements.
- Form a prefix array containing the prefix sum.
- Now, for each index in range [1, N)
- Check if the elements, from start to current index, form a permutation, using the below condition:
Sum of K elements = Sum of K natural numbers
where K is the current index
- Then check the remaining elements forms a permutation.
- If yes, then we print/return Yes.
Below is the implementation of the above approach:
C++
// C++ program to check if a given sequence
// is a concatenation of two permutations or not
#include <bits/stdc++.h>
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
JavaScript
<script>
// JavaScript 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
function checkPermutation(arr, n)
{
// Computing the sum of all the
// elements in the array
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
// Computing the prefix sum
// for all the elements in the array
let prefix = Array.from({length: n+1}, (_, i) => 0);
prefix[0] = arr[0];
for (let i = 1; i < n; i++)
prefix[i] = prefix[i - 1] + arr[i];
// Iterating through the i
// from lengths 1 to n-1
for (let i = 0; i < n - 1; i++) {
// Sum of first i+1 elements
let lsum = prefix[i];
// Sum of remaining n-i-1 elements
let rsum = sum - prefix[i];
// Lengths of the 2 permutations
let 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
let arr = [ 1, 2, 5, 3, 4, 1, 2 ];
let n = arr.length;
if (checkPermutation(arr, n))
document.write("Yes");
else
document.write("No");
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Count Permutations in a Sequence Given an array A consisting of N positive integers, find the total number of subsequences of the given array such that the chosen subsequence represents a permutation. Note: Sequence A is a subsequence of B if A can be obtained from B by deleting some(possibly, zero) elements without changing its or
5 min read
Check if a string is concatenation of another given string Given two strings str1 and str2 of length N and M respectively, the task is to check if the string str1 can be formed by concatenating the string str2 repetitively or not. Examples: Input: str1 = âabcabcabcâ, str2 = âabcâOutput: YesExplanation: Concatenating the string str2 thrice generates the stri
7 min read
Check if a binary string contains all permutations of length k Given a binary string and k, to check whether it's contains all permutations of length k or not. Examples: Input : Binary string 11001 k : 2 Output : Yes 11001 contains all possibilities of binary sequences with k = 2, 00, 01, 10, 11 Input : Binary string: 1001 k : 2 Output: No 1001 does not contain
13 min read
Check if two strings are permutation of each other Write a function to check whether two given strings are Permutation of each other or not. A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, "abcd" and "dabc" are Permutation of each other. We strongly recommend that
13 min read
Check if two arrays are permutations of each other Given two unsorted arrays of the same size, write a function that returns true if two arrays are permutations of each other, otherwise false. Examples: Input: arr1[] = {2, 1, 3, 5, 4, 3, 2} arr2[] = {3, 2, 2, 4, 5, 3, 1}Output: YesInput: arr1[] = {2, 1, 3, 5,} arr2[] = {3, 2, 2, 4}Output: NoWe stron
15+ min read