Skip to content
Related Articles

Related Articles

Generate an N-length array having equal count and sum of elements of both parities
  • Last Updated : 22 Apr, 2021

Given an integer N (3 ≤ N ≤ 105), the task is to generate an array of N distinct positive elements such that the count and sum of elements of both parties i.e even and odd, are the same. If it is not possible to construct such an array, print -1.

Examples:

Input: N = 8 
Output: 2, 4, 6, 8, 1, 3, 5, 11

Input:
Output: -1 
Explanation: For the count of odd and even array elements to be equal, 3 even and odd elements must be present in the array. But, sum of 3 od elements will always be odd. Therefore, it is not possible to generate such an array.

 

Approach: Follow the steps below to solve the problem:



  1. If N is odd or (N / 2) is odd, then print -1.
  2. Otherwise: 
    • Print N/2 even values starting from 2 and store the count in some variable, say SumEven.
    • Print N / 2 – 1 odd values starting from 1 and store the sum in another variable, say SumOdd.
    • At last print SumEven – SumOdd which is also odd.

Below is the implementation of the above approach:

C++




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Fun dtion to print the
// required sequence
void Print(int N)
{
    if ((N / 2) % 2 == 1
|| (N % 2 == 1)) {
        cout << -1 << endl;
        return;
    }
 
// Stores count of even
// and odd elements
    int CurEven = 2, CurOdd = 1;
 
// Stores sum of even
// and odd elements
    int SumOdd = 0, SumEven = 0;
 
// Print N / 2 even elements
    for (int i = 0; i < (N / 2); i++) {
 
        cout << CurEven << " ";
        SumEven += CurEven;
        CurEven += 2;
    }
 
// Print N / 2 - 1 odd elements
    for (int i = 0; i < N / 2 - 1; i++) {
        cout << CurOdd << " ";
        SumOdd += CurOdd;
        CurOdd += 2;
    }
 
    CurOdd = SumEven - SumOdd;
 
    // Print final odd element
    cout << CurOdd << '\n';
}
 
// Driver Code
int main()
{
    int N = 12;
    Print(N);
    return 0;
}

Java




// Java Program to implement
// the above approach
import java.io.*;
class GFG
{
 
  // Fun dtion to print the
  // required sequence
  static void Print(int N)
  {
    if ((N / 2) % 2 == 1 || (N % 2 == 1))
    {
      System.out.print(-1);
      return;
    }
 
    // Stores count of even
    // and odd elements
    int CurEven = 2, CurOdd = 1;
 
    // Stores sum of even
    // and odd elements
    int SumOdd = 0, SumEven = 0;
 
    // Print N / 2 even elements
    for (int i = 0; i < (N / 2); i++)
    {
      System.out.print(CurEven + " ");
      SumEven += CurEven;
      CurEven += 2;
    }
 
    // Print N / 2 - 1 odd elements
    for (int i = 0; i < N / 2 - 1; i++)
    {
      System.out.print(CurOdd + " ");
      SumOdd += CurOdd;
      CurOdd += 2;
    }
    CurOdd = SumEven - SumOdd;
 
    // Print final odd element
    System.out.println(CurOdd);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int N = 12;
    Print(N);
  }
}
 
// This code is contributed by Dharanendra L V.

Python3




# Python Program to implement
# the above approach
 
# Fun dtion to print the
# required sequence
def Print(N):
    if ((N / 2) % 2 or (N % 2)):
        print(-1)
        return
 
# Stores count of even
# and odd elements
    CurEven = 2
    CurOdd = 1
 
# Stores sum of even
# and odd elements
    SumOdd = 0
    SumEven = 0
 
# Print N / 2 even elements
    for i in range(N // 2):
 
        print(CurEven,end=" ")
        SumEven += CurEven
        CurEven += 2
 
# Print N / 2 - 1 odd elements
    for i in range( (N//2) - 1):
        print(CurOdd, end=" ")
        SumOdd += CurOdd
        CurOdd += 2
 
    CurOdd = SumEven - SumOdd
 
    # Print final odd element
    print(CurOdd)
 
# Driver Code
N = 12
Print(N)
 
# This code is contributed by rohitsingh07052

C#




// C# Program to implement
// the above approach
using System;
class GFG
{
 
  // Fun dtion to print the
  // required sequence
  static void Print(int N)
  {
    if ((N / 2) % 2 == 1 || (N % 2 == 1))
    {
      Console.WriteLine(-1);
      return;
    }
 
    // Stores count of even
    // and odd elements
    int CurEven = 2, CurOdd = 1;
 
    // Stores sum of even
    // and odd elements
    int SumOdd = 0, SumEven = 0;
 
    // Print N / 2 even elements
    for (int i = 0; i < (N / 2); i++)
    {
      Console.Write(CurEven + " ");
      SumEven += CurEven;
      CurEven += 2;
    }
 
    // Print N / 2 - 1 odd elements
    for (int i = 0; i < N / 2 - 1; i++)
    {
      Console.Write(CurOdd + " ");
      SumOdd += CurOdd;
      CurOdd += 2;
    }
    CurOdd = SumEven - SumOdd;
 
    // Print final odd element
    Console.WriteLine(CurOdd);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 12;
    Print(N);
  }
}
 
// This code is contributed by chitranayal.

Javascript




<script>
 
// Javascript Program to implement
// the above approach
 
// Fun dtion to print the
// required sequence
function Print(N)
{
    if ((N / 2) % 2 == 1 || (N % 2 == 1))
    {
         document.write(-1);
        return;
    }
     
    // Stores count of even
    // and odd elements
    var CurEven = 2, CurOdd = 1;
     
    // Stores sum of even
    // and odd elements
    var SumOdd = 0, SumEven = 0;
     
    // Print N / 2 even elements
    for(var i = 0; i < (N / 2); i++)
    {
        document.write(CurEven + " ");
        SumEven += CurEven;
        CurEven += 2;
    }
     
    // Print N / 2 - 1 odd elements
    for(var i = 0; i < N / 2 - 1; i++)
    {
        document.write(CurOdd + " ");
        SumOdd += CurOdd;
        CurOdd += 2;
    }
    CurOdd = SumEven - SumOdd;
     
    // Print final odd element
    document.write(CurOdd);
}
 
// Driver Code
var N = 12;
 
Print(N);
 
// This code is contributed by Ankita saini
 
</script>
Output: 
2 4 6 8 10 12 1 3 5 7 9 17

 

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :