Non-Divisible Subarray sum permutation
Last Updated :
22 Aug, 2023
Given a positive integer X, the task is to find a permutation of length X such that all subarray sum of length greater than one is not divisible by subarray length. If no permutation of length X exists, then print “Permutation doesn’t exist”.
Examples:
Input: X = 4
Output: {2, 1, 4, 3}
Explanation: All subarray sum of length greater than 1 isn’t divisible by subarray length.
Input: X = 3
Output: Permutation doesn’t exist
Explanation: subarray sum of length 3 is divisible by subarray length(3)
Approach: To solve the problem follow the below idea:
If X is odd, then there will exist a subarray of length X and their sum is divisible by X. So, permutation will not exist except if X is 1. If X is even, Permutation always exists, we will iterate all even numbers from 2 to X and insert i and i-1 in the permutation. Now the permutation will be like this, -2, 1, 4, 3….X, (X-1), it will not automatically contain a subarray of length greater than 1 such that the subarray sum is divisible by subarray length.
Illustration:
Consider X = 4,
- X is even, then we will start iterating from all even numbers from 2 to X.
- At 2, we will print 2 and 2-1 in the array.
- At 4, we will print 4 and 4-1 in the array.
- Now, the subarray of length 2 – [1, 2], [2, 3], [3, 4], subarray sum is not divisible by 2 the subarray of length 3 – [1, 2, 3], [2, 3, 4], subarray sum is not divisible by 3. the subarray of length 4 -[1, 4], subarray sum is not divisible by 4.
- Now, no subarray sum is not divisible by subarray length.
- So finally, return.
Below are the steps to implement the approach:
- If X is odd, if X is 1, then print 1. else print “permutation doesn’t exist” and return.
- If X is even, iterate from all even numbers from 2 to X.
- while iterating i, print i and i-1 at every i.
- Finally, return.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Buildpermutation( int n)
{
if (n % 2 != 0) {
if (n == 1) {
cout << "1" << endl;
}
else {
cout << "Permutation doesn't exist" << endl;
}
}
else {
for ( int i = 2; i <= n; i += 2) {
cout << i << " " ;
cout << i - 1 << " " ;
}
cout << endl;
}
}
int main()
{
int n = 4;
Buildpermutation(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void buildPermutation( int n)
{
if (n % 2 != 0 ) {
if (n == 1 ) {
System.out.println( "1" );
}
else {
System.out.println(
"Permutation doesn't exist" );
}
}
else {
for ( int i = 2 ; i <= n; i += 2 ) {
System.out.print(i + " " );
System.out.print(i - 1 + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 4 ;
buildPermutation(n);
}
}
|
Python
def build_permutation(n):
if n % 2 ! = 0 :
if n = = 1 :
print ( "1" )
else :
print ( "Permutation doesn't exist" )
else :
for i in range ( 2 , n + 1 , 2 ):
print (i, end = " " )
print (i - 1 , end = " " )
print ()
n = 4
build_permutation(n)
|
C#
using System;
class GFG
{
static void buildPermutation( int n)
{
if (n % 2 != 0)
{
if (n == 1)
{
Console.WriteLine( "1" );
}
else
{
Console.WriteLine( "Permutation doesn't exist" );
}
}
else
{
for ( int i = 2; i <= n; i += 2)
{
Console.Write(i + " " );
Console.Write(i - 1 + " " );
}
Console.WriteLine();
}
}
static void Main( string [] args)
{
int n = 4;
buildPermutation(n);
}
}
|
Javascript
function buildPermutation(n) {
if (n % 2 !== 0) {
if (n === 1) {
console.log( "1" );
} else {
console.log( "Permutation doesn't exist" );
}
} else {
let result = "" ;
for (let i = 2; i <= n; i += 2) {
result += i + " " ;
result += i - 1 + " " ;
}
console.log(result);
}
}
const n = 4;
buildPermutation(n);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...