1. Array Subsets Given an integer array, divide the array into 2 subsets A and B...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. Array Subsets Given an integer array, divide the array into 2 subsets A and B while respecting the following conditions: The intersection of A and B is null. • The union A and B is equal to the original array. • The number of elements in subset A is minimal. . • The sum of A's elements is greater than the sum of B's elements. Return the subset A in increasing order where the sum of A's elements is greater than the sum of B's elements. If more than one subset exists, return the one with the maximal sum. Example n=5 arr = [3, 7, 5, 6, 2] The 2 subsets in arr that satisfy the conditions for A are [5, 7] and [6, 7]: A is minimal (size 2) • Sum(A) = (5 + 7) = 12 > Sum(B) = (2+ 3 + 6) = 11 • Sum(A) = (6 + 7) = 13 > Sum(B) = (2+ 3+5) = 10 • The intersection of A and B is null and their union is equal to arr. The subset A where the sum of its elements is maximal is [6, 7]. Function Description Complete the subsetA function in the editor below. subsetA has the following parameter(s): int arr[]: an integer array Returns: int[]: an integer array with the values of subset A. Constraints • 1≤n≤105 • 1 ≤ arr[i] ≤ 105 (where 0<i<n) 1. Array Subsets Given an integer array, divide the array into 2 subsets A and B while respecting the following conditions: The intersection of A and B is null. • The union A and B is equal to the original array. • The number of elements in subset A is minimal. . • The sum of A's elements is greater than the sum of B's elements. Return the subset A in increasing order where the sum of A's elements is greater than the sum of B's elements. If more than one subset exists, return the one with the maximal sum. Example n=5 arr = [3, 7, 5, 6, 2] The 2 subsets in arr that satisfy the conditions for A are [5, 7] and [6, 7]: A is minimal (size 2) • Sum(A) = (5 + 7) = 12 > Sum(B) = (2+ 3 + 6) = 11 • Sum(A) = (6 + 7) = 13 > Sum(B) = (2+ 3+5) = 10 • The intersection of A and B is null and their union is equal to arr. The subset A where the sum of its elements is maximal is [6, 7]. Function Description Complete the subsetA function in the editor below. subsetA has the following parameter(s): int arr[]: an integer array Returns: int[]: an integer array with the values of subset A. Constraints • 1≤n≤105 • 1 ≤ arr[i] ≤ 105 (where 0<i<n)
Expert Answer:
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Management of the firm recruits from what major external environmental factor?
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
FIGURE Q38.5 is the current-versus-potential-difference graph for a photoelectric-effect experiment with an unknown metal. If classical physics provided the correct description of the photoelectric...
-
Determine the magnitude of the resultant force FR = F1 + F2 and its direction, measured counterclockwise from the positive u axis. Given: F1 = 300 N F2 = 500 N α = 30 deg β = 45 deg...
-
How do executive leaders integrate cutting-edge technologies and data-driven insights to optimize decision-making processes and enhance organizational performance in an increasingly digitized global...
-
How does architecture relate to design?
-
Cranston LTD. prepares its financial statements according to International Financial Reporting Standards. In October 2013, the company received a $2 million government grant. The grant represents 20%...
-
If I assume the WACC= 9% and a growth rate of 5% after year 1. What would the FCF for Year 1 be? What would the market value of the common stock be? 3. ($ millions) Sales Actual Forecast X 0 Xr1 $...
-
Damon Manufacturing is preparing its master budget for the first quarter of the upcoming year. The following data pertain to Damon Manufacturing's operations: Current Assets as of December 31 (prior...
-
You are a new supervisor. Attached is an email from a very loyal and high performing but very upset employee. What are some ways to handle the email with tact, preserving the relationship with this...
-
Derive the negative binomial (NB) distribution. (a) Suppose \(y\) follows a Poisson \((\lambda)\), where the parameter \(\lambda\) itself is a random variable following a gamma distribution...
-
Generalize the model considered in Example 4.11 to a marginal model for the longitudinal DOS data and compare the findings with that in Example4.11 Example 4.11 For the models in Example 4.8 DOS,...
-
Let \(S\) be a curve in the two-dimensional \(x-y\) plane defined parametrically by \(x=F(t)\) and \(y=G(t)\), where \(F\) and \(G\) are smooth functions. Show that the slope of the tangent line at...
-
Prove that (a) If \(y \sim \operatorname{Poisson}(\lambda)\), then both the mean and variance of \(y\) are \(\lambda\). (b) If \(y_{1}\) and \(y_{2}\) are independent and \(y_{j} \sim...
-
Prove under the paradigm of a multinomial distribution that if \(x\) and \(y\) are homogeneously associated, then \(y\) and \(z\) as well as \(x\) and \(z\) are also homogeneously associated.
-
5. To manage is to forecast and plan, to organize, to compound, to co-ordinate and to control. This definition was given by A. O Peter F Drucker B. O Hendry Fay C. O Louis Allan D. O Terry
-
The Dow Jones Industrial Average reached a high of $ 7801.63 on December 29, 1997. Recall from Example 18.4 that it reached a high of $ 1003 on November 14, 1972. The Consumer Price Index for...
-
Suppose we have stored n keys in a hash table of size m, with collisions resolved by chaining, and that we know the length of each chain, including the length L of the longest chain. Describe a...
-
Professor Stewart is consulting for the president of a corporation that is planning a company party. The company has a hierarchical structure, that is, the supervisor relation forms a tree rooted at...
-
Given a list of values z 0 , z 1 , . . . ,z n - 1 (possibly with repetitions), show how to find the coefficients of a polynomial P(x) of degree-bound n + 1 that has zeros only at z 0 , z 1 , . . . ,z...
-
Why can staging investment decisions add value?
-
How do you use a decision tree to make the best investment decision?
-
How can you identify a real option in a decision tree?
Study smarter with the SolutionInn App