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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to generate a new array with product
// of same indexed elements with arr[] equal to 0
void constructNewArraySumZero(int arr[], int N)
{
// Stores sum of same indexed array
// elements of arr and new array
int newArr[N];
// Traverse the array
for (int i = 0; i < N; i++) {
// If i is an even number
if (i % 2 == 0) {
// Insert arr[i + 1] into
// the new array newArr[]
newArr[i] = arr[i + 1];
}
else {
// Insert -arr[i - 1] into
// the new array newArr[]
newArr[i] = -arr[i - 1];
}
}
// Print new array elements
for (int i = 0; i < N; i++) {
cout << newArr[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, -5, -6, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
constructNewArraySumZero(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to generate a new array with
// product of same indexed elements with
// arr[] equal to 0
static void constructNewArraySumZero(int arr[],
int N)
{
// Stores sum of same indexed array
// elements of arr and new array
int newArr[] = new int[N];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If i is an even number
if (i % 2 == 0)
{
// Insert arr[i + 1] into
// the new array newArr[]
newArr[i] = arr[i + 1];
}
else
{
// Insert -arr[i - 1] into
// the new array newArr[]
newArr[i] = -arr[i - 1];
}
}
// Print new array elements
for(int i = 0; i < N; i++)
{
System.out.print(newArr[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, -5, -6, 8 };
int N = arr.length;
constructNewArraySumZero(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
# Function to generate a new array
# with product of same indexed elements
# with arr[] equal to 0
def constructNewArraySumZero(arr, N):
# Stores sum of same indexed array
# elements of arr and new array
newArr = [0] * N
# Traverse the array
for i in range(N):
# If i is an even number
if (i % 2 == 0):
# Insert arr[i + 1] into
# the new array newArr[]
newArr[i] = arr[i + 1]
else:
# Insert -arr[i - 1] into
# the new array newArr[]
newArr[i] = -arr[i - 1]
# Print new array elements
for i in range(N):
print(newArr[i] ,
end = " ")
# Driver code
arr = [1, 2, 3, -5, -6, 8]
N = len(arr)
constructNewArraySumZero(arr, N)
# This code is contributed by divyeshrabadiya07
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to generate a new array with
// product of same indexed elements with
// arr[] equal to 0
static void constructNewArraySumZero(int[] arr,
int N)
{
// Stores sum of same indexed array
// elements of arr and new array
int[] newArr = new int[N];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If i is an even number
if (i % 2 == 0)
{
// Insert arr[i + 1] into
// the new array newArr[]
newArr[i] = arr[i + 1];
}
else
{
// Insert -arr[i - 1] into
// the new array newArr[]
newArr[i] = -arr[i - 1];
}
}
// Print new array elements
for(int i = 0; i < N; i++)
{
Console.Write(newArr[i] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, -5, -6, 8 };
int N = arr.Length;
constructNewArraySumZero(arr, N);
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to generate a new array with
// product of same indexed elements with
// arr[] equal to 0
function constructNewArraySumZero(arr, N)
{
// Stores sum of same indexed array
// elements of arr and new array
let newArr = [];
// Traverse the array
for(let i = 0; i < N; i++)
{
// If i is an even number
if (i % 2 == 0)
{
// Insert arr[i + 1] into
// the new array newArr[]
newArr[i] = arr[i + 1];
}
else
{
// Insert -arr[i - 1] into
// the new array newArr[]
newArr[i] = -arr[i - 1];
}
}
// Print new array elements
for(let i = 0; i < N; i++)
{
document.write(newArr[i] + " ");
}
}
// Driver Code
let arr = [ 1, 2, 3, -5, -6, 8 ];
let N = arr.length;
constructNewArraySumZero(arr, N);
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
7 min read
Count arrays of length K whose product of elements is same as that of given array Given an integer array arr[] of length N and an integer K, the task is to count the number of possible arrays of length K such that the product of all elements of that array is equal to the product of all elements of the given array arr[]. Since the answer can be very large, return the answer modulo
15+ min read
Minimum steps to make sum and the product of all elements of array non-zero Given an array arr of N integers, the task is to find the minimum steps in which the sum and product of all elements of the array can be made non-zero. In one step any element of the array can be incremented by 1.Examples: Input: N = 4, arr[] = {0, 1, 2, 3} Output: 1 Explanation: As product of all e
7 min read
Find an element in array such that sum of left array is equal to sum of right array Given, an array of size n. Find an element that divides the array into two sub-arrays with equal sums. Examples: Input: 1 4 2 5 0Output: 2Explanation: If 2 is the partition, subarrays are : [1, 4] and [5] Input: 2 3 4 1 4 5Output: 1Explanation: If 1 is the partition, Subarrays are : [2, 3, 4] and [4
15+ min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read