0% found this document useful (0 votes)
46 views6 pages

GRAB Test

Uploaded by

Khoa Lý
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views6 pages

GRAB Test

Uploaded by

Khoa Lý
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

You are given an array of N integers.

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:

bool solution(vector<int> &A) ;

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.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [2..100,000];

• N is even;

 each element of array A is an integer within the range [-1,000,000,000..1,000,000,000].

bool solution(vector<int> &A) {


// Implement your solution here
}
Tell me about a time you received a negative feedback?

(Awnser in maximum 300 words in 15 mins)


You are given a string S, which consists entirely of decimal digits (0-9). Using digits from S, create a
palindromic number with the largest possible decimal value. You should use at least one digit and you
can reorder the digits. A palindromic number remains the same when its digits are reversed; for
instance, "7", "44" or "83238". Any palindromic number you create should not, however, have any
leading zeros, such as in "0990" or "010".

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:

string solution(string &S) ;

that, given a string S of N digits, returns the string representing the palindromic number with the largest
value. Examples:

1. Given "39878", your function should return "898".

2. Given "00900", your function should return "9".

3. Given "0000", your function should return "0".

4. Given "54321", your function should return "5".

Write an efficient algorithm for the following assumptions:

• N is an integer within the range [1..100,000);

• string S is made only of digits (0-9).

Bắt đầu từ chữ số lớn đến bé -> viết thành tần số cho mỗi chữ số{

Kiếm số giưã (lớn nhất)

Lấy tần số mỗi chữ số -> chia 2, để ra hai bên}

string solution(string &S) {


// Implement your solution here
}
There is an array A of N integers and two types of operations that can be performed on the elements of
the array:

 increment a single element of A, which costs C1;


 increment two elements of A, which costs C2. The chosen elements need to be in different
positions.

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:

int solution(vector<int> &A, int C1, int C2);

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.

Write an efficient algorithm for the following assumptions:

 N is an integer within the range [1..100,000];


 C1 and C2 are integers within the range [0..10,000];
 each element of array A is an integer within the range [0..50,000,000].
 int solution(vector<int> &A, int C1, int C2) {
 // Implement your solution here
 }
Describe a time when you rally your classmates/colleagues that you have no authority over to go the
extra mile? How did you motivate them? How did you deal with resistance or indifference?

(Answer in maximum 300 words and 15 minutes)

You might also like