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++
// 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);
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Subarray with no pair sum divisible by K Given an array of N non-negative integers, task is to find the maximum size of a subarray such that the pairwise sum of the elements of this subarray is not divisible by a given integer, K. Also, print this subarray as well. If there are two or more subarrays that follow the above stated condition,
13 min read
Longest Subarray With Sum Divisible By K Given an arr[] containing n integers and a positive integer k, he problem is to find the longest subarray's length with the sum of the elements divisible by k.Examples:Input: arr[] = [2, 7, 6, 1, 4, 5], k = 3Output: 4Explanation: The subarray [7, 6, 1, 4] has sum = 18, which is divisible by 3.Input:
10 min read
Longest subarray with sum not divisible by X Given an array arr[] and an integer X, the task is to print the longest subarray such that the sum of its elements isn't divisible by X. If no such subarray exists, print "-1". Note: If more than one subarray exists with the given property, print any one of them.Examples: Input: arr[] = {1, 2, 3} X
15+ min read
CSES Solutions - Subarray Divisibility Given an array arr[] of N integers, your task is to count the number of subarrays where the sum of values is divisible by N. Examples: Input: N = 5, arr[] = {3, 1, 2, 7, 4}Output: 1Explanation: There is only 1 subarray with sum divisible by 5, subarray {1, 2, 7}, sum = 10 and 10 is divisible by 5. I
6 min read
CSES Solutions - Subarray Sums I Given an array arr[] of N positive integers, your task is to count the number of subarrays having sum X. Examples: Input: N = 5, X = 7, arr[] = {2, 4, 1, 2, 7}Output: 3Explanation: There are 3 subarrays with sum = 7. Subarray {2, 4, 1}, sum = 2 + 4 + 1 = 7.Subarray {4, 1, 2}, sum = 4 + 1 + 2 = 7.Sub
8 min read