Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 2 From an array source of n integers, in a single ALL operation, choose any even - length subarray. Alternately add and subtract 1

Question 2
From an array source of n integers, in a single ALL operation, choose any even-length subarray. Alternately add and subtract 1 from each element of the subarray from left to right. For example, for source =[1,2,3,4,5,6] the subarray 3,4,5,6 can be chosen and made ,6,5. Then source =[1,2,4,3,6,5].
Given two arrays source and target of n integers, find the minimum number of operations that must be performed on the array source to make it equal to the array targeh Report -1 if it is not possible to make the two arrays equal.
Example
Suppose n=4, source =[4,3,1,3], and target ,2,3,0
It is optimal to choose the subarray 3,1 and get source =[4,4,0,3]. Now choose 0,3 to get source =[4,4,1,2]. Now choose the entire array to get source =[5,3,2,1]. Finally choose 5,1 to get source =[6,2,3,0]. Thus the two arrays can be equalized in at most 4 operations. Hence the answer is 4.
Function Description
Complete the function getMinOperations in the editar helois
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions