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

Questions 1

The document describes grouping arrays based on their mean values. Given an array of arrays, it finds the mean of each inner array and groups any arrays with equal means together. It returns the groups as an outer array of inner arrays, where each inner array contains the indices of arrays with the same mean. The groups
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views6 pages

Questions 1

The document describes grouping arrays based on their mean values. Given an array of arrays, it finds the mean of each inner array and groups any arrays with equal means together. It returns the groups as an outer array of inner arrays, where each inner array contains the indices of arrays with the same mean. The groups
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

You are given a string 

s. Consider the following algorithm applied to this string:


1.Take all the prefixes of the string, and choose the longest palindrome between them.

2.If this chosen prefix contains at least two characters, cut this prefix from s and go back to the first
step with the updated string. Otherwise, end the algorithm with the current string s as a result.
Your task is to implement the above algorithm and return its result when applied to string s.
Note: you can click on the prefixes and palindrome words to see the definition of the terms if you're not
familiar with them.

Example

•For s = "aaacodedoc", the output should be solution(s) = "".


•The initial string s = "aaacodedoc" contains only three prefixes which are also
palindromes - "a", "aa", "aaa". The longest one between them is "aaa", so we cut if
from s.
•Now we have string "codedoc". It contains two prefixes which are also palindromes
- "c" and "codedoc". The longest one between them is "codedoc", so we cut if from
the current string and obtain the empty string.
•Finally the algorithm ends on the empty string, so the answer is "".
•For s = "codesignal", the output should be solution(s) = "codesignal".
The initial string s = "codesignal" contains the only prefix, which is also palindrome - "c".
This prefix is the longest, but doesn't contain two characters, so the algorithm ends with
string "codesignal" as a result.
•For s = "", the output should be solution(s) = "".
Input/Output

•[execution time limit] 3 seconds (java)

•[input] string s

A string consisting of English lowercase letters.

Guaranteed constraints:
0 ≤ s.length ≤ 1000.

•[output] string

The result of the described algorithm.

[Java] Syntax Tips

// Prints help message to the console


// Returns a string
//
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
System.out.println("This prints to the console when you Run Tests");
return "Hello, " + name;
}
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that
arrays with equal mean values are in the same group, and arrays with different mean values are in
different groups.
Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j],
etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within each
group are sorted in ascending order, and the groups are sorted in ascending order of their minimum
element.
Example

•For

a = [[3, 3, 4, 2],
[4, 4],
[4, 0, 3, 3],
[2, 3],
[3, 3, 3]]

the output should be

solution(a) = [[0, 4],


[1],
[2, 3]]
•mean(a[0]) = (3 + 3 + 4 + 2) / 4 = 3;
•mean(a[1]) = (4 + 4) / 2 = 4;
•mean(a[2]) = (4 + 0 + 3 + 3) / 4 = 2.5;
•mean(a[3]) = (2 + 3) / 2 = 2.5;
•mean(a[4]) = (3 + 3 + 3) / 3 = 3.
There are three groups of means: those with mean 2.5, 3, and 4. And they form the following
groups:
•Arrays with indices 0 and 4 form a group with mean 3;
•Array with index 1 forms a group with mean 4;
•Arrays with indices 2 and 3 form a group with mean 2.5.

Note that neither

solution(a) = [[0, 4],


[2, 3],
[1]]

nor

solution(a) = [[0, 4],


[1],
[3, 2]]

will be considered as a correct answer:


•In the first case, the minimal element in the array at index 2 is 1, and it is less then the
minimal element in the array at index 1, which is 2.
•In the second case, the array at index 2 is not sorted in ascending order.

•For

a = [[-5, 2, 3],
[0, 0],
[0],
[-100, 100]]

the output should be

solution(a) = [[0, 1, 2, 3]]


The mean values of all of the arrays are 0, so all of them are in the same group.
Input/Output

•[execution time limit] 3 seconds (java)

•[input] array.array.integer a

An array of arrays of integers.

Guaranteed constraints:
1 ≤ a.length ≤ 100,
1 ≤ a[i].length ≤ 100,
-100 ≤ a[i][j] ≤ 100.

•[output] array.array.integer

An array of arrays, representing the groups of indices.

[Java] Syntax Tips

// Prints help message to the console


// Returns a string
//
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
System.out.println("This prints to the console when you Run Tests");
return "Hello, " + name;
}
You are given two integer arrays a and b of the same length.
Let's define the difference between a and b as the sum of absolute differences of corresponding elements:
difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length -
1]|
You can replace one element of a with any other element of a. Your task is to return the minimum
possible difference between a and b that can be achieved by performing at most one such replacement
on a. You can also choose to leave the array intact.
Example

For a = [1, 3, 5] and b = [5, 3, 1], the output should be solution(a, b) = 4.


•If we leave the array a intact, the difference is |1 - 5| + |3 - 3| + |5 - 1| = 8;
•If we replace a[0] with a[1], we get a = [3, 3, 5] and the difference is |3 - 5| + |3 -
3| + |5 - 1| = 6;
•If we replace a[0] with a[2], we get a = [5, 3, 5] and the difference is |5 - 5| + |3 -
3| + |5 - 1| = 4;
•If we replace a[1] with a[0], we get a = [1, 1, 5] and the difference is |1 - 5| + |1 -
3| + |5 - 1| = 10;
•If we replace a[1] with a[2], we get a = [1, 5, 5] and the difference is |1 - 5| + |5 -
3| + |5 - 1| = 10;
•If we replace a[2] with a[0], we get a = [1, 3, 1] and the difference is |1 - 5| + |3 -
3| + |1 - 1| = 4;
•If we replace a[2] with a[1], we get a = [1, 3, 3] and the difference is |1 - 5| + |3 -
3| + |3 - 1| = 6;
So the final answer is 4, since it's the minimum possible difference.
Input/Output

•[execution time limit] 3 seconds (java)

•[input] array.integer a

The first array of integers.

Guaranteed constraints:
2 ≤ a.length ≤ 105,
-104 ≤ a[i] ≤ 104.

•[input] array.integer b

The second array of integers.

Guaranteed constraints:
b.length = a.length,
-104 ≤ b[i] ≤ 104.

•[output] integer
The minimum possible difference between a and b after replacing at most one element of a to any
element from the same array, or leaving everything intact.
[Java] Syntax Tips

// Prints help message to the console


// Returns a string
//
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
System.out.println("This prints to the console when you Run Tests");
return "Hello, " + name;
}

You might also like