Construct array with sum of product of same indexed elements in the given array equal to zero
Last Updated :
28 Feb, 2022
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[] = {1, 2, 3, 4}
Output: -4 -3 2 1
Explanation:
Sum of product of same indexed array elements of arr[] with the new array = {1 * (-4) + 2 * (-3) + 3 * (2) + 4 * (1)} = 0.
Therefore, the required output is -4 -3 2 1.
Input: arr[] = {-1, 2, -3, 6, 4}
Output: 1 1 1 1 -1
Approach: The problem can be solved using the Greedy technique. The idea is based on the following observations:
arr[i] * (-arr[i + 1]) + arr[i + 1] * arr[i] = 0
Follow the steps below to solve the problem:
- Initialize an array, say newArr[] to store the new array elements such that ?(arr[i] * newArr[i]) = 0
- Traverse the given array using variable i and check if i is even or not. If found to be true then newArr[i] = arr[i + 1].
- Otherwise, newArr[i] = -arr[i – 1]
- Finally, print the values of newArr[].
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
void constructNewArraySumZero( int arr[], int N)
{
int newArr[N];
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
newArr[i] = arr[i + 1];
}
else {
newArr[i] = -arr[i - 1];
}
}
for ( int i = 0; i < N; i++) {
cout << newArr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, -5, -6, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
constructNewArraySumZero(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void constructNewArraySumZero( int arr[],
int N)
{
int newArr[] = new int [N];
for ( int i = 0 ; i < N; i++)
{
if (i % 2 == 0 )
{
newArr[i] = arr[i + 1 ];
}
else
{
newArr[i] = -arr[i - 1 ];
}
}
for ( int i = 0 ; i < N; i++)
{
System.out.print(newArr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , - 5 , - 6 , 8 };
int N = arr.length;
constructNewArraySumZero(arr, N);
}
}
|
Python3
def constructNewArraySumZero(arr, N):
newArr = [ 0 ] * N
for i in range (N):
if (i % 2 = = 0 ):
newArr[i] = arr[i + 1 ]
else :
newArr[i] = - arr[i - 1 ]
for i in range (N):
print (newArr[i] ,
end = " " )
arr = [ 1 , 2 , 3 , - 5 , - 6 , 8 ]
N = len (arr)
constructNewArraySumZero(arr, N)
|
C#
using System;
class GFG{
static void constructNewArraySumZero( int [] arr,
int N)
{
int [] newArr = new int [N];
for ( int i = 0; i < N; i++)
{
if (i % 2 == 0)
{
newArr[i] = arr[i + 1];
}
else
{
newArr[i] = -arr[i - 1];
}
}
for ( int i = 0; i < N; i++)
{
Console.Write(newArr[i] + " " );
}
}
public static void Main()
{
int [] arr = { 1, 2, 3, -5, -6, 8 };
int N = arr.Length;
constructNewArraySumZero(arr, N);
}
}
|
Javascript
<script>
function constructNewArraySumZero(arr, N)
{
let newArr = [];
for (let i = 0; i < N; i++)
{
if (i % 2 == 0)
{
newArr[i] = arr[i + 1];
}
else
{
newArr[i] = -arr[i - 1];
}
}
for (let i = 0; i < N; i++)
{
document.write(newArr[i] + " " );
}
}
let arr = [ 1, 2, 3, -5, -6, 8 ];
let N = arr.length;
constructNewArraySumZero(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...