Top Coding Interview Question – Solved

6 Live
Students in a class are asked to stand in ascending order according to their heights for the annual class photograph. Determine the number of students not currentlylstanding in their correct positions. Example height = [1, 1, 3, 3, 4, 1] The 3 students indicated in red at indices 2, 4 and 5, are not in the right positions. The correct positions are [1, 1, 1, 3, 3, 4]. Return 3. Function Description Complete the function countStudents in the editor below. countStudents has the following parameter(s): int height[n]: an array of heights in the order the students are standing Returns: int: the number of students not standing in the correct positions. Constraints · 1≤n≤ 105

Asked in: No companies listed

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def countStudents(height):
    temp = height[::]
    ans = 0
    temp.sort()
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach the problem of determining how many students are not currently standing in the correct positions for a class photograph, you first need to understand the objective clearly. Each student has a height, and they are currently standing in a certain order, represented by an array of integers. The task is to find out how many of those students are not in the position they would be if all the students were arranged in ascending order of their heights.

The concept is simple: if you take the current array of heights and compare it to what the array would look like if it were sorted in ascending order, then any mismatch between the two at a particular index indicates that the student at that index is not in their correct position.

Your thinking should start with this high-level process:
1. You have an array representing the current standing order of the students.
2. You need to determine the number of positions in this array where the current height does not match the height at that same position in a sorted version of the array.

This leads to a direct and efficient method. You can simulate what the “correct” order should be by sorting the original array. Then, iterate through both the original and the sorted array simultaneously, comparing each element one-by-one. For each position where the values differ, it means the student at that position is not standing where they should be in the sorted order. You simply count such mismatches.

To clarify this with a mental walkthrough using the example `[1, 1, 3, 3, 4, 1]`, the sorted version would be `[1, 1, 1, 3, 3, 4]`. Comparing the two:
- Index 0: 1 vs 1 (correct)
- Index 1: 1 vs 1 (correct)
- Index 2: 3 vs 1 (incorrect)
- Index 3: 3 vs 3 (correct)
- Index 4: 4 vs 3 (incorrect)
- Index 5: 1 vs 4 (incorrect)

You see mismatches at indices 2, 4, and 5. So, the count of students not in the correct positions is 3.

From a conceptual standpoint, this approach works because sorting gives you the desired final configuration. By comparing this with the current configuration, you can determine how far off each student is. You're not being asked to actually move students or fix the order; just to count those who are misplaced.

There are a few important considerations and edge cases to keep in mind:
- If the original array is already sorted in ascending order, then there would be zero mismatches, so the output would be 0.
- If all the students have the same height, then the sorted array would be identical to the original, resulting in no mismatches.
- If the original array is in reverse order (completely unsorted), then the number of mismatches would be at its maximum.

This problem doesn’t require you to track how to sort or where to move the students; you only need to detect how many are not where they should be. As such, your main tasks are:
- Create a sorted version of the input array.
- Compare the original array with the sorted one.
- Count how many positions differ between the two.

This approach is also efficient and scalable to large inputs, up to the constraint of 100,000 students. Sorting an array of that size is computationally reasonable with modern sorting algorithms, which operate in O(n log n) time. The comparison step is a simple linear pass, taking O(n) time.

So, to summarize your thought process:
- Recognize that the correct positions are determined by sorting the original array.
- Understand that the number of students out of place corresponds to the number of positions where the original and sorted arrays differ.
- Execute a one-to-one comparison between the two arrays to count the number of mismatches.
- This count gives the number of students not standing in their correct position.

By thinking in this structured and efficient manner, you can solve the problem cleanly without needing to actually rearrange the students or simulate any sorting process beyond generating the sorted array once.
```


Related Questions