GRAB Test
GRAB Test
You want to split them into N/2 pairs in such a way that the sum of
integers in each pair is odd. N is even and every element of the array must be present in exactly one
pair.
Your task is to determine whether it is possible to split the numbers into such pairs. For example, given
[2, 7, 4, 6, 3, 1], the answer is true. One of the possible sets of pairs is (2, 7), (6, 3) and (4, 1). Their sums
are respectively 9, 9 and 5, all of which are odd.
Write a function:
which, given an array of integers A of length N, returns true when it is possible to create the required
pairs and false otherwise. Examples:
1. Given A = [2, 7, 4, 6, 3, 1], the function should return true, as explained above.
2. Given A = [-1, 1], the function should return false. The only possible pair has the sum -1 + 1 = 0 which
is even.
3. Given A = [2, -1], the function should return true. The only pair has sum-1 + 2 = 1, which is odd.
4. Given A = [1, 2, 4, 3], the function should return true. Possible pairs are (1, 2), (4, 3). They both have
an odd sum.
5. Given A = [-1, -3, 4, 7, 7, 7], the function should return false. We can create only one pair with an odd
sum by taking 4 and any of the other numbers: for example, 4 + 7 = 11. All the other pairs have an even
sum.
• N is even;
For example, decimal palindromic numbers that can be created from "8199" are: "1", "8", "9", "99",
"919" and "989". Among them, "989" has the largest value.
Write a function:
that, given a string S of N digits, returns the string representing the palindromic number with the largest
value. Examples:
Bắt đầu từ chữ số lớn đến bé -> viết thành tần số cho mỗi chữ số{
What is the minimum total cost of operations that will make all elements of A equal? As the result may
be large, return its last nine digits without leading zeros (in other words, return the result modulo 10^9).
Write a function:
that, given an array A of N integers and two integers C1 and C2, returns the minimum cost of equalizing
the array (modulo 109). Examples:
1. Given A = [1, 4], C1 = 15 and C2 = 3, the function should return 45. We may increment the first
element three times.
2. Given A = [2, 11, 11, 11, 12], C1 = 10 and C2 = 4, the function should return 54. We may perform the
following operations:
increment the first and the second element using the second operation three times: A = [5, 14,
11,11, 12];
increment the first and the third element using the second operation three times: A = [8, 14, 14,
11,12];
increment the first and the fourth element using the second operation three times: A = [11, 14,
14, 14, 12];
increment the first and the fifth element using the second operation twice: A = [13, 14, 14, 14,
14];
increment the first element using the first operation once: A = [14, 14, 14, 14, 14].
3. Given A = [1000000, 2, 1, 2, 1000000], C1 = 10000 and C2 = 4000, the function should return
999998000. We may perform the following operations:
increment the second and the third element using the second operation 499,999 times: A =
[1000000, 500001, 500000, 2, 1000000]; increment the third and the fourth element using the
second operation 499,999 times: A = [1000000, 500001, 999999, 500001, 1000000];
increment the second and the fourth element using the second operation 499,999 times: A =
[1000000, 1000000, 999999, 1000000, 1000000];
increment the third element using the first operation once: A = [1000000, 1000000, 1000000,
1000000, 1000000].
The total cost is equal to 499999* 4000 * 3 + 10000 = 5999998000 but it should be returned
modulo 109.
4. Given A = [500000, 0, 500000, 500000, 500000], C1 = 10000 and C2 = 3000, the function should
return 10000. We may perform the follow operations:
increment the first and the second element using the second operation 166,667 times: A =
[666667, 166667, 500000, 500000, 500000];
increment the second and the third element using the second operation 166,667 times: A =
[666667, 333334, 666667, 500000, 500000];
increment the second and the fourth element using the second operation 166,667 times: A =
[666667, 500001, 666667, 666667, 500000];
increment the second and the fifth element using the second operation 166,667 times: A =
[666667, 666668, 666667, 666667, 666667];
increment the first and the third element using the second operation once: A = [666668,
666668, 666668, 666667, 666667];
increment the fourth and the fifth element using the second operation once: A = [666668,
666668, 666668, 666668, 666668].
The total cost is equal to 166667 * 3000 * 4 + 3000 * 2 = 2000010000 but it should be returned modulo
10^9.