Check if a Sequence is a concatenation of two permutations
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++
#include <bits/stdc++.h>
using namespace std;
bool checkPermutation( int arr[], int n)
{
long long sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
long long prefix[n + 1] = { 0 };
prefix[0] = arr[0];
for ( int i = 1; i < n; i++)
prefix[i] = prefix[i - 1] + arr[i];
for ( int i = 0; i < n - 1; i++) {
long long lsum = prefix[i];
long long rsum = sum - prefix[i];
long long l_len = i + 1,
r_len = n - i - 1;
if (((2 * lsum)
== (l_len * (l_len + 1)))
&& ((2 * rsum)
== (r_len * (r_len + 1))))
return true ;
}
return false ;
}
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
import java.util.*;
class GFG{
static boolean checkPermutation( int []arr, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
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];
for ( int i = 0 ; i < n - 1 ; i++) {
int lsum = prefix[i];
int rsum = sum - prefix[i];
int l_len = i + 1 ,
r_len = n - i - 1 ;
if ((( 2 * lsum)
== (l_len * (l_len + 1 )))
&& (( 2 * rsum)
== (r_len * (r_len + 1 ))))
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def checkPermutation(arr, n):
sum = 0 ;
for i in range (n):
sum + = arr[i];
prefix = [ 0 ] * (n + 1 );
prefix[ 0 ] = arr[ 0 ];
for i in range (n):
prefix[i] = prefix[i - 1 ] + arr[i];
for i in range (n - 1 ):
lsum = prefix[i];
rsum = sum - prefix[i];
l_len = i + 1
r_len = n - i - 1 ;
if ((( 2 * lsum) = = (l_len * (l_len + 1 ))) and
(( 2 * rsum) = = (r_len * (r_len + 1 )))):
return True ;
return False ;
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 3 , 4 , 1 , 2 ]
n = len (arr)
if (checkPermutation(arr, n)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool checkPermutation( int []arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
int []prefix = new int [n + 1];
prefix[0] = arr[0];
for ( int i = 1; i < n; i++)
prefix[i] = prefix[i - 1] + arr[i];
for ( int i = 0; i < n - 1; i++) {
int lsum = prefix[i];
int rsum = sum - prefix[i];
int l_len = i + 1,
r_len = n - i - 1;
if (((2 * lsum)
== (l_len * (l_len + 1)))
&& ((2 * rsum)
== (r_len * (r_len + 1))))
return true ;
}
return false ;
}
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" );
}
}
|
Javascript
<script>
function checkPermutation(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
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];
for (let i = 0; i < n - 1; i++) {
let lsum = prefix[i];
let rsum = sum - prefix[i];
let l_len = i + 1,
r_len = n - i - 1;
if (((2 * lsum)
== (l_len * (l_len + 1)))
&& ((2 * rsum)
== (r_len * (r_len + 1))))
return true ;
}
return false ;
}
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)
Last Updated :
24 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...