GOLDMANSACHS Coding Question – Solved

12 Live
There are 'N' coders standing in a line, where i denotes the ith position of a coder with a rating of Ri. All ratings are distinct. You have to form a team of 3 from amongst them with the condition: - Any three coders with positions (i, j, k) and ratings (Ri, Rj, Rk) can form a team when ("Ri < Rj < Rk" or "Ri > Rj > Rk") and (1 <= i < j < k <= N). You have to find out how many such teams exist (1 coder can be part of multiple teams). Input: N - 5 Ratings - 5 23 14 Output: 3 (5,2,1), (5,3,1), (2,3,4)

Asked in: GOLDMANSACHS

Image of the Question

Question Image

All Testcases Passed ✔



Passcode Image

Solution


#!/bin/python3

import math
import os
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, start by understanding the team formation rule clearly. You are given a list of N coders, each with a distinct rating. The coders are positioned in a line, and their position matters in selecting a team. For a valid team of 3 coders, they must satisfy two conditions: their positions must follow the order i < j < k, and their ratings must either strictly increase (Ri < Rj < Rk) or strictly decrease (Ri > Rj > Rk).

So essentially, the problem is about identifying all increasing or decreasing triplets based on rating, while maintaining the order of positions. Since the ratings are distinct, there will be no equal values, which simplifies the condition to strictly greater or less than comparisons.

Begin by considering every possible triplet in the list where the indices i, j, and k follow the positional constraint i < j < k. The total number of such triplets grows roughly with the cube of N (specifically, it’s the combination of N taken 3 at a time), but you’re not required to generate all combinations blindly. Instead, you can think about an efficient way to count how many triplets satisfy either the increasing or decreasing rating condition.

One intuitive strategy is to fix the middle element of the triplet, j, and explore how many valid i and k pairs exist for that j. By doing this, you can analyze elements before and after j in a targeted way. For each j (from 1 to N-2, assuming 1-based indexing), you can look at all positions i before j and all positions k after j. Your task is to count how many i's and k's satisfy the necessary conditions to complete a valid team centered around j.

Let’s break this down further. Suppose you fix the j-th coder. Now, for every i < j, you can compare Ri with Rj to see if it forms an increasing or decreasing pair. Similarly, for every k > j, you can compare Rj with Rk to complete the triplet. The key insight here is that if you know how many such i’s and k’s exist around a fixed j, you can compute the number of valid teams that include coder j as the center.

For example:
- If Ri < Rj and Rj < Rk, then the triplet (i, j, k) forms a valid increasing team.
- If Ri > Rj and Rj > Rk, then the triplet (i, j, k) forms a valid decreasing team.

So, for each j, you can count:
1. The number of Ri < Rj for i < j (let’s call this count_left_smaller).
2. The number of Rk > Rj for k > j (let’s call this count_right_greater).
Multiplying count_left_smaller and count_right_greater gives the number of increasing teams with j in the middle.

Similarly:
1. Count of Ri > Rj for i < j (count_left_greater).
2. Count of Rk < Rj for k > j (count_right_smaller).
Multiplying these gives the number of decreasing teams with j in the middle.

Summing all these counts for every possible j (from 2nd to second last position) will give the total number of valid teams. This approach is more efficient than checking all combinations of triplets and avoids unnecessary computation.

Let’s take the sample input to visualize:
N = 5, Ratings = [5, 2, 3, 4, 1]

Go through each position j from 1 to N:
- For j = 1, no i < j exists, so skip.
- For j = 2, check i = 1 and k = 3,4,5.
- For j = 3, check i = 1,2 and k = 4,5.
- For j = 4, check i = 1,2,3 and k = 5.
- For j = 5, no k > j exists, so skip.

At each of these, compute the counts of elements smaller/greater before and after j and compute the product as described. Sum these results across all valid j to get the final answer.

By organizing your logic around a central element and using simple counting in the left and right segments relative to that element, you maintain control over the problem’s complexity and avoid brute force iteration over all triplets. This keeps the solution both correct and efficient, especially given the constraints of up to 1000 elements.
```


Related Questions