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.

Approach :
We will take a greedy approach to find the solution:
- if size of negative side is 0 then we can only print 0 in positive side.
- if the size of positive side is 0 and the size of negative side is greater than 0 then balanced array is never possible .
- if size of negative side is 0 then we can only print 0 in positive side.
- 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.
- if size of positive side(b) is greater than negative side(a)
- we will store all the elements from 1 to b in positive side and sum it.
- we will store all the elements from -1 to -(a-1) in negative side and sum it.
- lastly we store (-sum) as the last element of negative side because it is the difference between positive and negative side of array.
- if size of negative side(a) is greater than positive side(b)
- we will store all the elements from -1 to -a in negative side and sum it.
- we will store all the elements from 1 to (b-1) in positive side and sum it.
- lastly we store (-sum) as the last element of positive side because it is the difference between positive and negative side of array.
- 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;
}
Time Complexity : O(A+B)