Need help Urgent
There is a string representing a date in "MM-DD" format, where MM denotes a month in a two-digit format and DD denotes a day in a two-digit format. Some digits were replaced by "?". Replace all the question marks with digits (0-9) in such a way as to obtain the latest possible date.
Assume that the maximum number of days in each month is as follows:
mm | month | number of days
01 | January | 31
02 | February | 28
03 | March | 31
04 | April | 30
05 | May | 31
06 | June | 30
07 | July | 31
08 | August | 31
09 | September | 30
10 | October | 31
11 | November | 30
12 | December | 31• Write a function:
public String solution(String date);
that, given a string date, returns the latest valid date as a string in the format "MM- DD". If it is not possible to obtain any valid date, return the string "xx-xx".
Examples:
Assuming date = "?1-31", the date is clearly from January (01) or November (11). Only January has 31 days, so the function should return "01-31".
Assuming date = "02-??", the date is clearly from February (02), which has 28 days. The function should return "28-02".
Assuming date = "??-4?", no month has at least 40 days. The function should return "XX-xx".
Assuming date = "09-31", the date is not valid. There are only 30 days in September, so the function should return "XX-Xx".Assume that:
• date consists of exactly five characters; the third one is "-", the others are digits (0-9) or "?".
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
Q2.
We are provided with a plan of an industrial village, represented by an array A consisting of N integers. The K-th value (for K within the range O...N-1) represents a field which may contain:
a forest consisting of A[k] trees (if A[k] is positive);
an industrial building (if A[K] is non-positive), producing -A[K] units of pollution.One tree is able to neutralize one unit of pollution. Our goal is to make every neighborhood sustainable, i.e. for every field, the sum of its value and the values of its neighbors (adjacent fields to the left and right) should be greater than or equal to zero. To achieve this goal, we can plant additional trees in any chosen field (note that we can plant trees in fields containing industrial buildings).
For example, given A = [1, -3, 2], there is one tree in the field number 0, an industrial building producing 3 units of pollution in field number 1 and two trees in
field number 2. The sums of values of the fields and their neighbors are 1 + (-3) = -2 for field number 0, 1 + (-3) + 2 = 0 for field number 1, and (-3) + 2 = -1 fo
field number 2. The neighborhoods of fields 0 and 2 are not sustainable, as their sums are negative. After planting two trees in field 1, we obtain A = [1, -1, 2]. In
the new array, the sums are respectively 0, 2, and 1, which makes every neighborhood sustainable
What is the minimum number of trees we have to plant in order to make every field's neighborhood sustainable?
Write a function:
public int solution (int[] A);
that, given an array A consisting of N integers, returns the minimum number of trees we have to plant in order to make every field's neighborhood sustainable.
Examples:
Given A = [1, -3, 2], the function should return 2, as described above.
Given A = [-3, 2, 4, -5, 3], the function should return 3. We can plant one tree in field number 0 and two trees in field number 4, achieving (-2, 2, 4, -5, 5].
Given A = [-2, 1, -3, 1], the function should return 4. We can plant two trees each in fields number 1 and 2. After that, we obtain values [-2, 3, -1, 1].Write an efficient algorithm for the following assumptions:
• N is an integer within the range [1..100,000];
• each element of array A is an integer within the range -10,000..10,000].