Open In App

Non-Divisible Subarray sum permutation

Last Updated : 22 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check can we sort the
// permutation in the increasing order
// bu doing these operation
void Buildpermutation(int n)
{
 
    // If n is odd
    if (n % 2 != 0) {
 
        // If n is 1, we can make
        // per = {1}
        if (n == 1) {
            cout << "1" << endl;
        }
 
        // If n is odd and greater
        // than 1
        else {
 
            // We can not make permutation
            // because subarray sum of
            // length n is divisible by
            // subarray length
            cout << "Permutation doesn't exist" << endl;
        }
    }
 
    // If n is even, permutation always
    // exist of length n
    else {
 
        // Iterating all even numbers
        // from 2 to n
        for (int i = 2; i <= n; i += 2) {
            cout << i << " ";
            cout << i - 1 << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int n = 4;
 
    // Function call
    Buildpermutation(n);
 
    return 0;
}


Java




// java code for the above approach:
import java.util.*;
 
class GFG {
    // Function to check if we can sort the
    // permutation in increasing order
    // by doing these operations
    static void buildPermutation(int n)
    {
        // If n is odd
        if (n % 2 != 0) {
            // If n is 1, we can make
            // per = {1}
            if (n == 1) {
                System.out.println("1");
            }
            else {
                // If n is odd and greater
                // than 1
                // We cannot make permutation
                // because subarray sum of
                // length n is divisible by
                // subarray length
                System.out.println(
                    "Permutation doesn't exist");
            }
        }
        else {
            // If n is even, permutation always
            // exists of length n
            for (int i = 2; i <= n; i += 2) {
                System.out.print(i + " ");
                System.out.print(i - 1 + " ");
            }
            System.out.println();
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
 
        // Function call
        buildPermutation(n);
    }
}
 
// This code is contributed by rambabuguphka


Python




def build_permutation(n):
    # If n is odd
    if n % 2 != 0:
        # If n is 1, we can make per = [1]
        if n == 1:
            print("1")
        # If n is odd and greater than 1
        else:
            # We cannot make a permutation
            # because subarray sum of length n
            # is divisible by subarray length
            print("Permutation doesn't exist")
    # If n is even, a permutation always exists of length n
    else:
        # Iterating all even numbers from 2 to n
        for i in range(2, n+1, 2):
            print(i, end=" ")
            print(i-1, end=" ")
        print()
# Nikunj Sonigara
# Driver code
n = 4
# Function call
build_permutation(n)


C#




using System;
 
class GFG
{
    static void buildPermutation(int n)
    {
        // If n is odd
        if (n % 2 != 0)
        {
            // If n is 1, we can make per = {1}
            if (n == 1)
            {
                Console.WriteLine("1");
            }
            // If n is odd and greater than 1
            else
            {
                // We cannot make the permutation
                // because subarray sum of length n
                // is divisible by subarray length
                Console.WriteLine("Permutation doesn't exist");
            }
        }
        else
        {
            for (int i = 2; i <= n; i += 2)
            {
                // Printing even number and
              // then its predecessor
                Console.Write(i + " ");
                Console.Write(i - 1 + " ");
            }
            Console.WriteLine();
        }
    }
    static void Main(string[] args)
    {
        int n = 4;
        // Function call
        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);
  }
}
// Nikunj Sonigara
// Driver code
  const n = 4;
  buildPermutation(n);


Output

2 1 4 3 




Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads