FastPrepFind Niceness ๐ŸŒบ
Problem ยท Dynamic Programming

Find Niceness ๐ŸŒบ

Learn this problem
โ— HardGoldman Sachs logoGoldman SachsOA

Problem statement

Ramu and Sonu each have N chalks with distinct positive lengths. Array a gives Ramu's arrangement, while array b contains Sonu's chalk lengths.

Permute b so that every adjacent comparison has the same direction as the corresponding comparison in a. If a[i] < a[i + 1], Sonu's values at those positions must increase; if a[i] > a[i + 1], they must decrease.

The niceness of an arrangement is the sum of the absolute differences between adjacent lengths. Return the maximum niceness among all arrangements that match Ramu's comparison pattern.

Function

findNiceness(N: int, a: int[], b: int[]) โ†’ int

Examples

Example 1

N = 4a = [5, 7, 4, 9]b = [1, 2, 3, 4]return = 7

Ramu's comparison pattern is increase, decrease, increase. The arrangement [2,4,1,3] follows that pattern and has niceness |2-4| + |4-1| + |1-3| = 7. No valid permutation has a larger value, so the answer is 7.

Constraints

  • N == a.length == b.length
  • Both arrays contain N distinct positive integers.

More Goldman Sachs problems

drafts saved locally
public int findNiceness(int N, int a[], int b[]) {
    // WRITE YOUR CODE HERE

}
N4
a[5, 7, 4, 9]
b[1, 2, 3, 4]
expected7
checking account