Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given an integer N, you are asked to divide N into a sum of a maximal number of positive even integers. All the numbers
Given an integer N, you are asked to divide N into a sum of a maximal number of positive even integers. All the numbers should also be different. For example, for N = 12, the following splits are valid: (2 + 10), (2 + 4 + 6) and (4 + 8). Among them, (2 + 4 + 6) contains the maximal number of integers. Note that N cannot be split into (2 + 2+4+4) as all the numbers should be different. Write a function: class Solution {public int[] solution (int N); } which, given a positive integer number N, returns an array containing the numbers from any maximal possible answer (any valid combination may be returned). If N cannot be divided in such a way, return an empty array. Result array should be returned as an array of integers. Examples: 1. Given N = 6, your function should return [2, 4] or [4, 2]. 2. Given N = 7, your function should return [] (an empty array) as there is no valid split. 3. Given N = 22, your function should return [2, 4, 6, 10] in any order. 4. Given N = 4, your function should return [4]. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000,000].
Step by Step Solution
★★★★★
3.52 Rating (159 Votes )
There are 3 Steps involved in it
Step: 1
An array may be a instrumentality object that holds a hard and fast range of values of one kind The ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started