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.
- Check if the elements, from start to current index, form a permutation, using the below condition:
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 |
Yes
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Print the two possible permutations from a given sequence
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Check if two arrays are permutations of each other
- Check if two Linked Lists are permutations of each other
- Check if given string can be formed by two other strings or their permutations
- Check if two arrays are permutations of each other using Mathematical Operation
- Check if an array can be formed by merging 2 non-empty permutations
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Check if any valid sequence is divisible by M
- Check if the given Prufer sequence is valid or not
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- k-th missing element in increasing sequence which is not present in a given sequence
- Minimum concatenation required to get strictly LIS for the given array
- Find the number obtained after concatenation of binary representation of M and N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

