Given an array arr[], the task is to perform arithmetic operations (+, -, *, /) on arr[] in sequence. These operations are performed on adjacent elements until array size reduces to 1. Finally, return the reduced value and number of operations required for the same. Refer to examples for more clarity.
Examples:
Input: arr = {12, 10, 2, 9, 1, 2} Output: Reduced Value: 10 Operations Performed: + : 2 – : 1 * : 1 / : 1
Explanation: Step 1: perform addition of consecutive element [22, 12, 11, 10, 3] Step 2: perform subtraction of consecutive element [10, 1, 1, 7] Step 3: perform multiplication of consecutive element[10, 1, 7] Step 4: perform division of consecutive element [10, 0] Step 5: Again perform addition of consecutive element[10]

Input: arr = {5, -2, -1, 2, 4, 5} Output: Reduced Value: -3 Operations Performed: + : 2 – : 2 * : 1 / : 1
Approach: Since in this problem we have to perform operations based on the sequence like first addition then subtraction, then multiplication and then division hence we can use Switch case to solve this problem.
Initialize an dictionary where operator (+, -, *, /) as key and 0 as value. Using functions add, sub, mul and div, operation on array will be performed. It has as function Operation which maps the array with functions based on the value of c%4 and return the reduced array. where c keeps track of number of operations performed. Dictionary keeps track of each operations performed till size of array get reduced to 1. Follow the steps below to solve given problem.
Step 1: Assign c to 1 and declare dictionary d.
- Step 2: if c%4==1 then perform addition operation on consecutive element using Operation function.
- Step 3: if c%4==2 then perform subtraction operation on consecutive element using Operation function.
- Step 4: if c%4==3 then perform multiplication operation on consecutive element using Operation function.
- Step 5: if c%4==0 then perform division operation on consecutive element using Operation function.
- Step 6: step 2 will be repeated till size of array become equal to 1.
- Step 7: d is used to keep track of each operation performed.
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector< int > add(vector< int > a)
{
vector< int > res;
for ( int i = 0; i < a.size() - 1; i++) {
res.push_back(a[i] + a[i + 1]);
}
return res;
}
vector< int > sub(vector< int > a)
{
vector< int > res;
for ( int i = 0; i < a.size() - 1; i++) {
res.push_back(a[i] - a[i + 1]);
}
return res;
}
vector< int > mul(vector< int > a)
{
vector< int > res;
for ( int i = 0; i < a.size() - 1; i++) {
res.push_back(a[i] * a[i + 1]);
}
return res;
}
vector< int > div (vector< int > a)
{
vector< int > res;
for ( int i = 0; i < a.size() - 1; i++) {
res.push_back((a[i] == 0 || a[i + 1] == 0)
? 0
: floor (a[i] / a[i + 1]));
}
return res;
}
vector< int > Operation( int i, vector< int > A)
{
switch (i) {
case 1:
return add(A);
case 2:
return sub(A);
case 3:
return mul(A);
case 0:
return div (A);
default :
return vector< int >();
}
}
int main()
{
unordered_map< char , int > d;
d[ '/' ] = 0;
d[ '*' ] = 0;
d[ '-' ] = 0;
d[ '+' ] = 0;
vector< int > arr = { 5, -2, -1, 2, 1, 4, 5 };
int c = 1;
while (arr.size() != 1) {
int x = c % 4;
switch (x) {
case 1:
d[ '+' ]++;
arr = Operation(x, arr);
break ;
case 2:
d[ '-' ]++;
arr = Operation(x, arr);
break ;
case 3:
d[ '*' ]++;
arr = Operation(x, arr);
break ;
case 0:
d[ '/' ]++;
arr = Operation(x, arr);
break ;
}
c++;
}
cout << "Reduced value: " << arr[0] << endl;
cout << "Operations Performed:\n" ;
for ( auto i : d) {
cout << i.first << ": " << i.second << endl;
}
}
|
Java
import java.util.*;
public class Main {
public static ArrayList<Integer> add(ArrayList<Integer> a) {
ArrayList<Integer> res = new ArrayList<Integer>();
for ( int i = 0 ; i < a.size() - 1 ; i++) {
res.add(a.get(i) + a.get(i + 1 ));
}
return res;
}
public static ArrayList<Integer> sub(ArrayList<Integer> a) {
ArrayList<Integer> res = new ArrayList<Integer>();
for ( int i = 0 ; i < a.size() - 1 ; i++) {
res.add(a.get(i) - a.get(i + 1 ));
}
return res;
}
public static ArrayList<Integer> mul(ArrayList<Integer> a) {
ArrayList<Integer> res = new ArrayList<Integer>();
for ( int i = 0 ; i < a.size() - 1 ; i++) {
res.add(a.get(i) * a.get(i + 1 ));
}
return res;
}
public static ArrayList<Integer> div(ArrayList<Integer> a) {
ArrayList<Integer> res = new ArrayList<Integer>();
for ( int i = 0 ; i < a.size() - 1 ; i++) {
res.add((a.get(i) == 0 || a.get(i + 1 ) == 0 )
? 0
: ( int ) Math.floor(a.get(i) / a.get(i + 1 )));
}
return res;
}
public static ArrayList<Integer> Operation( int i, ArrayList<Integer> A) {
switch (i) {
case 1 :
return add(A);
case 2 :
return sub(A);
case 3 :
return mul(A);
case 0 :
return div(A);
default :
return new ArrayList<Integer>();
}
}
public static void main(String[] args)
{
HashMap<Character, Integer> d = new HashMap<Character, Integer>();
d.put( '/' , 0 );
d.put( '*' , 0 );
d.put( '-' , 0 );
d.put( '+' , 0 );
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add( 5 );
arr.add(- 2 );
arr.add(- 1 );
arr.add( 2 );
arr.add( 1 );
arr.add( 4 );
arr.add( 5 );
int c = 1 ;
while (arr.size() != 1 ) {
int x = c % 4 ;
switch (x) {
case 1 :
d.put( '+' , d.get( '+' ) + 1 );
arr = Operation(x, arr);
break ;
case 2 :
d.put( '-' , d.get( '-' ) + 1 );
arr = Operation(x, arr);
break ;
case 3 :
d.put( '*' , d.get( '*' ) + 1 );
arr = Operation(x, arr);
break ;
case 0 :
d.put( '/' , d.get( '/' ) + 1 );
arr = Operation(x, arr);
break ;
}
c++;
}
System.out.println( "Reduced value: " + arr.get( 0 ));
System.out.println( "Operations Performed:" );
for (Map.Entry<Character, Integer> entry : d.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}}
|
Python3
def add(a):
return [a[i] + a[i + 1 ] for i in range ( len (a) - 1 )]
def sub(a):
return [a[i] - a[i + 1 ] for i in range ( len (a) - 1 )]
def mul(a):
return [a[i] * a[i + 1 ] for i in range ( len (a) - 1 )]
def div(a):
return [ 0 if a[i] = = 0 or a[i + 1 ] = = 0 \
else a[i] / / a[i + 1 ] \
for i in range ( len (a) - 1 )]
def Operation(i, A):
switcher = {
1 : add,
2 : sub,
3 : mul,
0 : div
}
func = switcher.get(i, lambda : 'Invalid' )
return func(A)
c = 1
d = { '+' : 0 , '-' : 0 , '*' : 0 , '/' : 0 }
arr = [ 5 , - 2 , - 1 , 2 , 1 , 4 , 5 ]
while len (arr)! = 1 :
x = c % 4
if x = = 1 :
d[ '+' ] + = 1
arr = Operation(x, arr)
elif x = = 2 :
d[ '-' ] + = 1
arr = Operation(x, arr)
elif x = = 3 :
d[ '*' ] + = 1
arr = Operation(x, arr)
elif x = = 0 :
d[ '/' ] + = 1
arr = Operation(x, arr)
c + = 1
print ( "Reduced value:" , * arr)
print ( "Operations Performed:" )
for i in d.keys():
print ( str (i) + " : " + str (d[i]))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
public static List< int > add(List< int > a)
{
List< int > res = new List< int >();
for ( int i = 0; i < a.Count - 1; i++) {
res.Add(a[i] + a[i + 1]);
}
return res;
}
public static List< int > sub(List< int > a)
{
List< int > res = new List< int >();
for ( int i = 0; i < a.Count - 1; i++) {
res.Add(a[i] - a[i + 1]);
}
return res;
}
public static List< int > mul(List< int > a)
{
List< int > res = new List< int >();
for ( int i = 0; i < a.Count - 1; i++) {
res.Add(a[i] * a[i + 1]);
}
return res;
}
public static List< int > div(List< int > a)
{
List< int > res = new List< int >();
for ( int i = 0; i < a.Count - 1; i++) {
res.Add((a[i] == 0 || a[i + 1] == 0)
? 0
: ( int )Math.Floor(( double )a[i]
/ a[i + 1]));
}
return res;
}
public static List< int > Operation( int i, List< int > A)
{
switch (i) {
case 1:
return add(A);
case 2:
return sub(A);
case 3:
return mul(A);
case 0:
return div(A);
default :
return new List< int >();
}
}
public static void Main()
{
Dictionary< char , int > d
= new Dictionary< char , int >();
d.Add( '/' , 0);
d.Add( '*' , 0);
d.Add( '-' , 0);
d.Add( '+' , 0);
List< int > arr = new List< int >();
arr.Add(5);
arr.Add(-2);
arr.Add(-1);
arr.Add(2);
arr.Add(1);
arr.Add(4);
arr.Add(5);
int c = 1;
while (arr.Count != 1) {
int x = c % 4;
switch (x) {
case 1:
d[ '+' ]++;
arr = Operation(x, arr);
break ;
case 2:
d[ '-' ]++;
arr = Operation(x, arr);
break ;
case 3:
d[ '*' ]++;
arr = Operation(x, arr);
break ;
case 0:
d[ '/' ]++;
arr = Operation(x, arr);
break ;
}
c++;
}
Console.WriteLine( "Reduced value: " + arr[0]);
Console.WriteLine( "Operations Performed:" );
foreach (KeyValuePair< char , int > entry in d
.OrderBy(a => -a.Value)
.ThenBy(b => b.Key))
{
Console.WriteLine(entry.Key + ": "
+ entry.Value);
}
}
}
|
Javascript
function add(a) {
return a.slice(0, a.length - 1).map((val, i) => val + a[i + 1]);
}
function sub(a) {
return a.slice(0, a.length - 1).map((val, i) => val - a[i + 1]);
}
function mul(a) {
return a.slice(0, a.length - 1).map((val, i) => val * a[i + 1]);
}
function div(a) {
return a.slice(0, a.length - 1).map((val, i) => (val === 0 || a[i + 1] === 0) ? 0 : Math.floor(val / a[i + 1]));
}
function Operation(i, A) {
switch (i) {
case 1: return add(A);
case 2: return sub(A);
case 3: return mul(A);
case 0: return div(A);
default : return 'Invalid' ;
}
}
let c = 1;
let d = { '+' : 0, '-' : 0, '*' : 0, '/' : 0 };
let arr = [5, -2, -1, 2, 1, 4, 5];
while (arr.length !== 1) {
let x = c % 4;
switch (x) {
case 1:
d[ '+' ]++;
arr = Operation(x, arr);
break ;
case 2:
d[ '-' ]++;
arr = Operation(x, arr);
break ;
case 3:
d[ '*' ]++;
arr = Operation(x, arr);
break ;
case 0:
d[ '/' ]++;
arr = Operation(x, arr);
break ;
}
c++;
}
console.log( "Reduced value:" , ...arr);
console.log( "Operations Performed:" );
for (let i in d) {
console.log(`${i} : ${d[i]}`);
}
|
Output
Reduced value: -3
Operations Performed:
+ : 2
- : 2
* : 1
/ : 1
Time Complexity: O(N) Auxiliary Space: O(N)
|