Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Ways to make sum of odd and even indexed elements equal by removing an array element
Next article icon

Construct an Array of size N in which sum of odd elements is equal to sum of even elements

Last Updated : 17 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer N which is always even, the task is to create an array of size N which contains N/2 even numbers and N/2 odd numbers. All the elements of array should be distinct and the sum of even numbers is equal to the sum of odd numbers. If no such array exists then print -1.
Examples: 
 

Input: N = 8 
Output: 2 4 6 8 1 3 5 11 
Explanation: 
The array has 8 distinct elements which have equal sum of odd and even numbers, i.e., (2 + 4 + 6 + 8 = 1 + 3 + 5 + 11).
Input: N = 10 
Output: -1 
Explanation: 
It is not possible to construct array of size 10. 
 

 

Approach: To solve the problem mentioned above the very first observation is that it is not possible to create an array that has size N which is a multiple of 2 but not multiple of 4. Because, if that happens then the sum of one half which contains odd numbers will always be odd and the sum of another half which contains even numbers will always be even, hence the sum of both halves can’t be the same.
Therefore, the array which satisfies the problem statement should always have a size N which is a multiple of 4. The approach is to first construct N/2 even numbers starting from 2, which is the first half of the array. Then create another part of the array starting from 1 and finally calculate the last odd element such that it makes both the halves equal. In order to do so the last element of odd numbers should be (N/2) – 1 + N.
Below is the implementation of the above approach: 
 

CPP




// C++ program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to construct the required array
void arrayConstruct(int N)
{
 
    // To construct first half,
    // distinct even numbers
    for (int i = 2; i <= N; i = i + 2)
        cout << i << " ";
 
    // To construct second half,
    // distinct odd numbers
    for (int i = 1; i < N - 1; i = i + 2)
        cout << i << " ";
 
    // Calculate the last number of second half
    // so as to make both the halves equal
    cout << N - 1 + (N / 2) << endl;
}
 
// Function to construct the required array
void createArray(int N)
{
 
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
 
        // function call to construct array
        arrayConstruct(N);
 
    else
        cout << -1 << endl;
}
 
// Driver code
int main()
{
 
    int N = 8;
 
    createArray(N);
 
    return 0;
}
 
 

Java




// Java program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
class GFG{
  
// Function to construct the required array
static void arrayConstruct(int N)
{
  
    // To confirst half,
    // distinct even numbers
    for (int i = 2; i <= N; i = i + 2)
        System.out.print(i+ " ");
  
    // To consecond half,
    // distinct odd numbers
    for (int i = 1; i < N - 1; i = i + 2)
        System.out.print(i+ " ");
  
    // Calculate the last number of second half
    // so as to make both the halves equal
    System.out.print(N - 1 + (N / 2) +"\n");
}
  
// Function to construct the required array
static void createArray(int N)
{
  
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
  
        // function call to conarray
        arrayConstruct(N);
  
    else
        System.out.print(-1 +"\n");
}
  
// Driver code
public static void main(String[] args)
{
    int N = 8;
  
    createArray(N);
}
}
 
// This code is contributed by Princi Singh
 
 

Python3




# python3 program to Create an array
# of size N consisting of distinct
# elements where sum of odd elements
# is equal to sum of even elements
 
# Function to construct the required array
def arrayConstruct(N):
 
    # To construct first half,
    # distinct even numbers
    for i in range(2, N + 1, 2):
        print(i,end=" ")
 
    # To construct second half,
    # distinct odd numbers
    for i in range(1, N - 1, 2):
        print(i, end=" ")
 
    # Calculate the last number of second half
    # so as to make both the halves equal
    print(N - 1 + (N // 2))
 
# Function to construct the required array
def createArray(N):
 
    # check if size is multiple of 4
    # then array exist
    if (N % 4 == 0):
 
        # function call to construct array
        arrayConstruct(N)
 
    else:
        cout << -1 << endl
 
# Driver code
if __name__ == '__main__':
 
    N = 8
 
    createArray(N)
 
# This code is contributed by mohit kumar 29
 
 

C#




// C# program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
using System;
 
class GFG{
   
// Function to construct the required array
static void arrayConstruct(int N)
{
   
    // To confirst half,
    // distinct even numbers
    for (int i = 2; i <= N; i = i + 2)
        Console.Write(i + " ");
   
    // To consecond half,
    // distinct odd numbers
    for (int i = 1; i < N - 1; i = i + 2)
        Console.Write(i + " ");
   
    // Calculate the last number of second half
    // so as to make both the halves equal
    Console.Write(N - 1 + (N / 2) +"\n");
}
   
// Function to construct the required array
static void createArray(int N)
{
   
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
   
        // function call to conarray
        arrayConstruct(N);
   
    else
        Console.Write(-1 +"\n");
}
   
// Driver code
public static void Main(String[] args)
{
    int N = 8;
   
    createArray(N);
}
}
 
// This code is contributed by Rajput-Ji
 
 

Javascript




<script>
// JavaScript program to Create an array
// of size N consisting of distinct
// elements where sum of odd elements
// is equal to sum of even elements
 
// Function to construct the required array
function arrayConstruct(N)
{
 
    // To construct first half,
    // distinct even numbers
    for (let i = 2; i <= N; i = i + 2)
        document.write(i + " ");
 
    // To construct second half,
    // distinct odd numbers
    for (let i = 1; i < N - 1; i = i + 2)
        document.write(i + " ");
 
    // Calculate the last number of second half
    // so as to make both the halves equal
    document.write(N - 1 + (N / 2) + "<br>");
}
 
// Function to construct the required array
function createArray(N)
{
 
    // check if size is multiple of 4
    // then array exist
    if (N % 4 == 0)
 
        // function call to construct array
        arrayConstruct(N);
    else
        document.write(-1 + "<br>");
}
 
// Driver code
    let N = 8;
    createArray(N);
 
// This code is contributed by Surbhi Tyagi.
</script>
 
 
Output: 
2 4 6 8 1 3 5 11

 

Time Complexity: O(N)

Auxiliary Space: O(1)



Next Article
Ways to make sum of odd and even indexed elements equal by removing an array element

D

divyeshrabadiya07
Improve
Article Tags :
  • Algorithms
  • Arrays
  • Competitive Programming
  • DSA
  • Mathematical
  • Pattern Searching
Practice Tags :
  • Algorithms
  • Arrays
  • Mathematical
  • Pattern Searching

Similar Reads

  • Construct array with sum of product of same indexed elements in the given array equal to zero
    Given an array, arr[] of size N (always even), the task is to construct a new array consisting of N non-zero integers such that the sum of the product of the same indexed elements of arr[] and the new array is equal to 0. If multiple solutions exist, print any one of them. Examples: Input: arr[] = {
    6 min read
  • Print indices of array elements whose removal makes the sum of odd and even-indexed elements equal
    Given an array arr[] of size N, the task is to find indices of array elements whose removal makes the sum of odd and even indexed elements equal. If no such element exists, then print -1. Examples: Input: arr[] = {4, 1, 6, 2} Output: 1 Explanation: Removing arr[1] modifies arr[] to {4, 6, 2} Sum of
    13 min read
  • Ways to make sum of odd and even indexed elements equal by removing an array element
    Given an array, arr[] of size n, the task is to find the count of array indices such that removing an element from these indices makes the sum of even-indexed and odd-indexed array elements equal. Examples: Input: arr[] = [ 2, 1, 6, 4 ] Output: 1 Explanation: Removing arr[1] from the array modifies
    10 min read
  • Check if the array has an element which is equal to sum of all the remaining elements
    Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
    10 min read
  • Construct sum-array with sum of elements in given range
    You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {
    7 min read
  • Number of permutations such that sum of elements at odd index and even index are equal
    Given N numbers, find the number of permutations in which the sum of elements at odd index and sum of elements at even index are equal. Examples: Input: 1 2 3 Output: 2 The permutations are: 1 3 2 sum at odd index = 1+2 = 3, sum at even index = 3 2 3 1 sum at odd index = 2+1 = 3, sum at even index =
    8 min read
  • Count subarrays having sum of elements at even and odd positions equal
    Given an array arr[] of integers, the task is to find the total count of subarrays such that the sum of elements at even position and sum of elements at the odd positions are equal. Examples: Input: arr[] = {1, 2, 3, 4, 1}Output: 1Explanation: {3, 4, 1} is the only subarray in which sum of elements
    7 min read
  • Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
    Given an array arr[] of length N, the task is to find the count of array indices such that removing an element from these indices makes the Bitwise xor of odd-indexed elements and even-indexed (1-based indexing) elements are equal. Examples: Input: arr[] = {1, 0, 1, 0, 1}, N = 5Output: 3Explanation:
    10 min read
  • Maximum partitions possible of given Array with cost at most K and equal count of odd and even elements
    Given two integers N, K, and an array, arr[] of size N, containing an equal number of even and odd elements, and also given that the cost of partitioning the array by making a cut between index i and i+1 is equal to the abs(arr[i]-arr[i+1]), the task is to find the maximum partitions of the array, s
    9 min read
  • Find the Array Permutation having sum of elements at odd indices greater than sum of elements at even indices
    Given an array arr[] consisting of N integers, the task is to find the permutation of array elements such that the sum of odd indices elements is greater than or equal to the sum of even indices elements. Examples: Input: arr[] = {1, 2, 3, 4}Output: 1 4 2 3 Explanation:Consider the permutation of th
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences