Generate a balanced array of distinct integers

Updated

Question :

You are given two numbers A and B such that the number of negative numbers in array is A and number of positive number in array is B.

We have to make an array whose sum is balanced (sum of negative elements is equal to sum of positive numbers) and all elements are distinct.

Example :

Input : 4  5
Output : -1 -2 -3 -9 1 2 3 4 5

Explanation :
The sum of positive side is 15 which is equal to the sum of the negative side.

Two bars of equal length show the four negative numbers and the five positive numbers of the example, and each side has a total size of 15.

Approach :

We will take a greedy approach to find the solution:

  1. if size of negative side is 0 then we can only print 0 in positive side.
  2. if the size of positive side is 0 and the size of negative side is greater than 0 then balanced array is never possible .
  3. if size of negative side is 0 then we can only print 0 in positive side.
  4. if size of positive side and negative side are same then we can put same elements in the array with opposite signs in sorted manner.
  5. if size of positive side(b) is greater than negative side(a)
    1. we will store all the elements from 1 to b in positive side and sum it.
    2. we will store all the elements from -1 to -(a-1) in negative side  and sum it.
    3. lastly we store (-sum) as the last element of negative side because it is the difference between positive and negative side of array.
  6. if size of negative side(a) is greater than positive side(b)
    1. we will store all the elements from -1 to -a in negative side and sum it.
    2. we will store all the elements from 1 to (b-1) in positive side and sum it.
    3. lastly we store (-sum) as the last element of positive side because it is the difference between positive and negative side of array.
  7. lastly print the array storing all the negative and positive elements.

Code :

//C++ program to generate balanced array.
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
// function to display array.
void print(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}
//function to generate balanced array.
void balanced_array(int a, int b)
{
    int arr[a + b] = {0};
    int n = a + b;
    if (a == 0 && b == 0) //size of array will be zero if a and b is 0.
    {
        cout << "empty array" << endl;
        return;
    }
    if (a == 0) //if size of negative side is 0 then we can only print 0 in positive side.
    {
        print(arr, n);
        return;
    }
    if (b == 0) // if the size of positive side is 0 and the size of negative side is greater than 0 then balanced array is never possible .
    {
        cout << "balanced array not possible" << endl;
        return;
    }
    // declaring two vector to store negative and positive values of array respectively.
    vector<int> neg;
    vector<int> pos;
    if (a > b) // when size of negative side is greater than positive side.
    {
        int sum = 0;
        for (int i = 0; i < a; i++)
        {
            neg.push_back(-(i + 1));
            sum += i + 1;
        }
        for (int i = 0; i < b - 1; i++)
        {
            pos.push_back(i + 1);
            sum -= (i + 1);
        }
        pos.push_back(sum);
        // putting all values of vectors into the original array.
        int k = 0;
        for (int i = 0; i < neg.size(); i++)
        {
            arr[k] = neg[i];
            k++;
        }
        for (int i = 0; i < pos.size(); i++)
        {
            arr[k] = pos[i];
            k++;
        }
        print(arr, n);
    }

    if (b > a) // when size of positive side is greater than negative side.
    {
        int sum = 0;
        for (int i = 0; i < b; i++)
        {
            pos.push_back(i + 1);
            sum += (i + 1);
        }
        for (int i = 0; i < a - 1; i++)
        {
            neg.push_back(-(i + 1));
            sum -= (i + 1);
        }
        neg.push_back(-sum);
        // putting all values of vectors into the original array.
        int k = 0;
        for (int i = 0; i < neg.size(); i++)
        {
            arr[k] = neg[i];
            k++;
        }
        for (int i = 0; i < pos.size(); i++)
        {
            arr[k] = pos[i];
            k++;
        }
        print(arr, n);
    }
    if (a == b) // size of positive side and negative side are same then we can put same elements in the array with opposite signs.
    {
        for (int i = 0; i < a; i++)
        {
            arr[a + i] = i + 1;
            arr[i] = -(i + 1);
        }
        print(arr, n);
    }
}
//Driver code to test above function
int main()
{
    int a, b;
    a = 4, b = 5;
    balanced_array(a, b);
    return 0;
}

Output :

-1 -2 -3 -9 1 2 3 4 5

Time Complexity : O(A+B)

Comment
Mithlesh Upadhyay Published 2 Dec 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials