Generate an Array such that the sum of any Subarray is not divisible by its Length Last Updated : 10 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N. Then the task is to output an array let's say A[] of length N such that: The sum S, of any subarray, let's say A[L, R] where (L != R) must not leave the remainder as 0 when dividing by the length of A[L, R]. Formally, Sum(A[L, R])%(length of A[L, R]) != 0. A[] must contain all unique elements. Note: Subarrays must be chosen at least of length equal to 2. Examples: Input: N = 3 Output: A[] = {7, 2, 5} Explanation: There can be three possible subarrays of length at least equal to 2: A[1, 2]: The length and sum of subarray is 2 and (7+2) = 9 respectively. We can see that (9%2) != 0 A[2, 3]: The length and sum of subarray is 2 and (2+5) = 7 respectively. We can see that (7%2) != 0A[1, 3]: The length and sum of subarray is 3 and (7+2+5) = 14 respectively. We can see that (14%3) != 0There is no subarray such that its sum is perfectly divisible by its length. A[] also contains N distinct elements. Therefore, output array A[] is valid. Input: N = 2 Output: A[] = {2, 1} Explanation: It can be verified that both conditions are followed by the output array A[]. Approach: Implement the idea below to solve the problem The problem is observation based and can be solved using those observations. Let us see some observations: When N is an even number, we can output a permutation.When n is an odd number, we can replace N with N + 1 and again we get another array consisting of N distinct integers. Examples: N = 7, A[] = {2, 1, 4, 3, 6, 5, 8} N = 8, A[] = {2, 1, 4, 3, 6, 5, 8, 7}General formula: N is Odd: A[] = {2, 1, 3, 4, ... N-1, N-2, N+1}N is Even: A[] = {2, 1, 3, 4, ... N, N-1}Steps were taken to solve the problem: If (N is Odd)Output {2, 1, 3, 4, ... N-1, N-2, N+1} using loop.If (N is Even)Output {2, 1, 3, 4, ... N, N-1} using loop.Code to implement the approach: C++ #include <iostream> void GFG(int N) { // Check if N is odd or even if (N % 2 == 1) { // Print elements for the odd N for (int i = 1; i < N; i += 2) std::cout << i + 1 << " " << i << " "; std::cout << N + 1 << std::endl; } else { // Print elements for even N for (int i = 1; i <= N; i += 2) std::cout << i + 1 << " " << i << " "; } } int main() { // Input int N = 8; // Function call GFG(N); return 0; } Java // Java code to implement the approach // Driver Class public class Main { // Driver Function public static void main(String[] args) { // Input int N = 8; // Function call Print_array(N); } // Method to print A[] public static void Print_array(int N) { if (N % 2 == 1) { for (int i = 1; i < N; i += 2) System.out.print(i + 1 + " " + i + " "); System.out.println(N + 1); } else { for (int i = 1; i <= N; i += 2) System.out.print(i + 1 + " " + i + " "); } } } Python3 # Python code to implement the approach # Method to print A[] def Print_array(N): if N % 2 == 1: for i in range(1, N, 2): print(i + 1, i, end=" ") print(N + 1) else: for i in range(1, N+1, 2): print(i + 1, i, end=" ") # Driver function def main(): # Input N = 8 # Function call Print_array(N) if __name__ == '__main__': main() # This code is contributed by ragul21 C# using System; class GFG { static void Main(string[] args) { // Input int N = 8; // Function call PrintArray(N); } // Method to print elements based on N static void PrintArray(int N) { // Check if N is odd or even if (N % 2 == 1) { // Print elements for the odd N for (int i = 1; i < N; i += 2) Console.Write(i + 1 + " " + i + " "); Console.WriteLine(N + 1); } else { // Print elements for even N for (int i = 1; i <= N; i += 2) Console.Write(i + 1 + " " + i + " "); } } } JavaScript function GFG(N) { // Check if N is odd or even if (N % 2 === 1) { // Print elements for the odd N for (let i = 1; i < N; i += 2) console.log(i + 1, i); console.log(N + 1); } else { // Print elements for even N for (let i = 1; i <= N; i += 2) console.log(i + 1, i); } } // Input let N = 8; // Function call GFG(N); Output2 1 4 3 6 5 8 7 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Generate a unique Array of length N with sum of all subarrays divisible by N P pradeep6036ymca Follow Improve Article Tags : Geeks Premier League DSA Arrays Arrays Geeks Premier League 2023 +1 More Practice Tags : ArraysArrays Similar Reads Generate a unique Array of length N with sum of all subarrays divisible by N Given an integer N, the task is to make an array of unique elements of length N such that all subarrays sum modulo N equals to zero. Examples: Input: N = 6 Output: 6 12 18 24 30 36 Explanation: Since all elements are a multiple of 6. Hence all subarrays add up to a sum divisible by 6.Input: N = 4 Ou 3 min read Generate an N-length array having sum of each subarray divisible by K Given two positive integers N and K, the task is to generate an array consisting of N distinct integers such that the sum of elements of each subarray of the constructed array is divisible by K. Examples: Input: N = 3, K = 3Output: 3 6 9Explanation:The subarrays of the resultant array are {3}, {6}, 4 min read Find a subarray whose sum is divisible by size of the array Given an array arr[] of length N. The task is to check if there exists any subarray whose sum is a multiple of N. If there exists such subarray, then print the starting and ending index of that subarray else print -1. If there are multiple such subarrays, print any of them. Examples: Input: arr[] = 13 min read Max sum of Subarray of length N/2 where sum is divisible by N Given an array, arr[] of size N where N is even, the task is to find the maximum possible sum of the subarray of length N/2 where the sum is divisible by N. Examples: Input: arr[] = {2, 3, 4, 5, 6, 7}, N = 6Output:18Explanation: Here, N = 6, The maximum possible sum of a sublist of length 3 (N/2) is 8 min read Count ways to split an array into subarrays such that sum of the i-th subarray is divisible by i Given an array arr[] consisting of N integers, the task is to find the number of ways to split the array into non-empty subarrays such that the sum of the ith subarray is divisible by i. Examples: Input: arr[] = {1, 2, 3, 4}Output: 3Explanation:Following are the number of ways to split the array int 8 min read Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N Given an array of N integers, the task is to find a non-empty subset such that the sum of elements of the subset is divisible by N. Output any such subset with its size and the indices(1-based indexing) of elements in the original array if it exists. Prerequisites: Pigeonhole PrincipleExamples: Inpu 8 min read Like