Reduce all array elements to zero by performing given operations thrice
Last Updated :
21 Apr, 2021
Given an array arr[] of size N, the task is to convert every array element to 0 by applying the following operations exactly three times:
- Select a subarray.
- Increment every element of the subarray by the integer multiple of its length.
Finally, print the first and last indices of the subarray involved and the elements added to every index of the subarray in each step.
Examples:
Input: arr[] = {1, 3, 2, 4}
Output:
Operation 1: 1 1
Added elements: 1
Operation 2: 3 4
Added elements: 4 2
Operation 3: 2 4
Added elements: -3 -6 -6
Explanation:
Step 1: Select subarray {arr[1]} and add -1 to the only element in the subarray. Hence, the array arr[] modifies to {0, 3, 2, 4}.
Step 2: Select subarray {arr[3], arr[4]} and add {4, 2} to the subarray elements respectively. Hence, the array arr[] modifies to {0, 3, 6, 6}.
Step 3: Select subarray {arr[2], arr[4]} and add {-3, -6, -6} to the subarray elements respectively. Hence, the array arr[] modifies to {0, 0, 0, 0}
Input: arr[] = { 5 }
Output:
Operation 1 : 1 1
Added elements: -5
Operation 2 : 1 1
Added elements: 5
Operation 3 : 1 1
Added elements: -5
Approach: The given problem can be solved based on the following observations:
Operation 1: Select subarray {arr[1], .., arr[N]}. Set A[i] = A[i] – N * A[i] = (N – 1) * (-A[i]).
After operation 1, each element is a multiple of (N – 1).
Operation 2: Select subarray {arr[1], .. arr[N – 1]}. Add / Subtract a multiple of (N – 1) to all values of the subarray till they reduce to 0.
Operation 3: Select subarray {arr[N]}, of size 1. Add / Subtract a multiple of 1 to make A[N] = 0.
Follow the steps below to solve the problem:
- If N is equal to 1, then print -arr[0], +arr[0], -arr[0] respectively in three moves.
- Otherwise, perform the following operations:
- Print 1 N. Subtract N * arr[i] from each element of the array print the subtracted elements.
- Print 1 N – 1. Subtract (N – 1) * arr[i] from each element of the subarray and print the subtracted values.
- Finally, print N. Subtract arr[i – 1] from the element. Print arr[i – 1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ConvertArray( int arr[], int N)
{
if (N == 1) {
cout << "Operation 1 : " << 1
<< " " << 1 << endl;
cout << "Added elements: "
<< -1 * arr[0] << endl;
cout << endl;
cout << "Operation 2 : "
<< 1 << " " << 1 << endl;
cout << "Added elements: "
<< 1 * arr[0] << endl;
cout << endl;
cout << "Operation 3 : "
<< 1 << " " << 1 << endl;
cout << "Added elements: "
<< -1 * arr[0] << endl;
}
else {
cout << "Operation 1 : "
<< 1 << " " << N << endl;
cout << "Added elements: " ;
for ( int i = 0; i < N; i++) {
cout << -1 * arr[i] * N << " " ;
}
cout << endl;
cout << endl;
cout << "Operation 2 : "
<< 1 << " " << N - 1 << endl;
cout << "Added elements: " ;
for ( int i = 0; i < N - 1; i++) {
cout << arr[i] * (N - 1) << " " ;
}
cout << endl;
cout << endl;
cout << "Operation 3 : " << N
<< " " << N << endl;
cout << "Added elements: " ;
cout << arr[N - 1] * (N - 1) << endl;
}
}
int main()
{
int arr[] = { 1, 3, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
ConvertArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void ConvertArray( int arr[], int N)
{
if (N == 1 ) {
System.out.println( "Operation 1 : " + 1
+ " " + 1 );
System.out.println( "Added elements: "
+ - 1 * arr[ 0 ] );
System.out.println();
System.out.println( "Operation 2 : "
+ 1 + " " + 1 );
System.out.println( "Added elements: "
+ 1 * arr[ 0 ] );
System.out.println();
System.out.println( "Operation 3 : "
+ 1 + " " + 1 );
System.out.println( "Added elements: "
+ - 1 * arr[ 0 ] );
}
else {
System.out.println( "Operation 1 : "
+ 1 + " " + N );
System.out.print( "Added elements: " );
for ( int i = 0 ; i < N; i++) {
System.out.print(- 1 * arr[i] * N + " " );
}
System.out.println();
System.out.println();
System.out.println( "Operation 2 : "
+ 1 + " " + (N - 1 ) );
System.out.print( "Added elements: " );
for ( int i = 0 ; i < N - 1 ; i++) {
System.out.print(arr[i] * (N - 1 ) + " " );
}
System.out.println();
System.out.println();
System.out.println( "Operation 3 : " + N
+ " " + N );
System.out.print( "Added elements: " );
System.out.println(arr[N - 1 ] * (N - 1 ) );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 2 , 4 };
int N = arr.length;
ConvertArray(arr, N);
}
}
|
Python3
def ConvertArray(arr, N):
if (N = = 1 ):
print ( "Operation 1 :" , 1 , 1 )
print ( "Added elements:" , - 1 * arr[ 0 ])
print ( "\n" ,end = "")
print ( "Operation 2 :" , 1 , 1 )
print ( "Added elements:" , 1 * arr[ 0 ])
print ( "\n" ,end = "")
print ( "Operation 3 :" , 1 , 1 )
print ( "Added elements:" , - 1 * arr[ 0 ])
print ( "\n" ,end = "")
else :
print ( "Operation 1 :" , 1 ,N)
print ( "Added elements:" ,end = " " )
for i in range (N):
print ( - 1 * arr[i] * N,end = " " )
print ( "\n" )
print ( "Operation 2 :" , 1 ,N - 1 )
print ( "Added elements:" ,end = " " )
for i in range (N - 1 ):
print (arr[i] * (N - 1 ),end = " " )
print ( "\n" )
print ( "Operation 3 : " ,N,N)
print ( "Added elements:" ,end = " " )
print (arr[N - 1 ] * (N - 1 ))
if __name__ = = '__main__' :
arr = [ 1 , 3 , 2 , 4 ]
N = len (arr)
ConvertArray(arr, N)
|
C#
using System;
class GFG{
static void ConvertArray( int [] arr, int N)
{
if (N == 1) {
Console.WriteLine( "Operation 1 : " + 1
+ " " + 1 );
Console.WriteLine( "Added elements: "
+ -1 * arr[0] );
Console.WriteLine();
Console.WriteLine( "Operation 2 : "
+ 1 + " " + 1 );
Console.WriteLine( "Added elements: "
+ 1 * arr[0] );
Console.WriteLine();
Console.WriteLine( "Operation 3 : "
+ 1 + " " + 1 );
Console.WriteLine( "Added elements: "
+ -1 * arr[0] );
}
else {
Console.WriteLine( "Operation 1 : "
+ 1 + " " + N );
Console.Write( "Added elements: " );
for ( int i = 0; i < N; i++) {
Console.Write(-1 * arr[i] * N + " " );
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine( "Operation 2 : "
+ 1 + " " + (N - 1) );
Console.Write( "Added elements: " );
for ( int i = 0; i < N - 1; i++) {
Console.Write(arr[i] * (N - 1) + " " );
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine( "Operation 3 : " + N
+ " " + N );
Console.Write( "Added elements: " );
Console.WriteLine(arr[N - 1] * (N - 1) );
}
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 2, 4 };
int N = arr.Length;
ConvertArray(arr, N);
}
}
|
Javascript
<script>
function ConvertArray(arr, N)
{
if (N == 1)
{
document.write( "Operation 1 : " + 1 + " " + 1+ "<br/>" );
document.write( "Added elements: " + -1 * arr[0]+ "<br/>" );
document.write( "<br/>" );
document.write( "Operation 2 : " + 1 + " " + 1+ "<br/>" );
document.write( "Added elements: " + 1 * arr[0]+ "<br/>" );
document.write( "<br/>" );
document.write( "Operation 3 : " + 1 + " " + 1+ "<br/>" );
document.write( "Added elements: " + -1 * arr[0]+ "<br/>" );
}
else
{
document.write( "Operation 1 : " + 1 + " " + N+ "<br/>" );
document.write( "Added elements: " );
for (i = 0; i < N; i++) {
document.write(-1 * arr[i] * N + " " );
}
document.write( "<br/>" );
document.write( "<br/>" );
document.write( "Operation 2 : " + 1 + " " + (N - 1)+ "<br/>" );
document.write( "Added elements: " );
for (i = 0; i < N - 1; i++) {
document.write(arr[i] * (N - 1) + " " );
}
document.write( "<br/>" );
document.write( "<br/>" );
document.write( "Operation 3 : " + N + " " + N+ "<br/>" );
document.write( "Added elements: " );
document.write(arr[N - 1] * (N - 1)+ "<br/>" );
}
}
var arr = [ 1, 3, 2, 4 ];
var N = arr.length;
ConvertArray(arr, N);
</script>
|
Output:
Operation 1 : 1 4
Added elements: -4 -12 -8 -16
Operation 2 : 1 3
Added elements: 3 9 6
Operation 3 : 4 4
Added elements: 12
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...