BackmediumArraysRazorpayOracle

Optimal Stage Index Shifts Solution

Problem Statement

Given a list of group names and their original lineup indices, determine which groups that are not on the first position of the stage are able to move to a new index to maintain the correct order.

Example 1
Input
[['Group A', 0], ['Group B', 1], ['Group C', 2]]
Output
[0]

Explanation: Step 1: Check if the first group is not at the first position. In this case, 'Group A' is not at the first position. Step 2: Since 'Group A' is the first group and it's not at the first position, it can move to a new index. Therefore, the output is [0].

Example 2
Input
[['Group A', 1], ['Group B', 0], ['Group C', 2]]
Output
[0]

Explanation: Step 1: Check if the first group is not at the first position. In this case, 'Group A' is not at the first position. Step 2: Since 'Group A' is the first group and it's not at the first position, it can move to a new index. Therefore, the output is [0].

Constraints

  • The number of groups is between 2 and 10 (inclusive).
  • The original lineup index for each group is between 1 and the number of stages (inclusive).
  • The number of stages is 10 (unique and does not repeat).
  • Groups are considered not shifted if they are already at the first position.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Optimal Stage Index Shifts — Problem Statement & Solution Guide

ArraysMediumNEW OR EXISTING ID
TimeO(n)
|
SpaceO(n)

Problem Description

Given a list of group names and their original lineup indices, determine which groups that are not on the first position of the stage are able to move to a new index to maintain the correct order.

Examples

Example 1

Input

[['Group A', 0], ['Group B', 1], ['Group C', 2]]

Output

[0]

Explanation: Step 1: Check if the first group is not at the first position. In this case, 'Group A' is not at the first position. Step 2: Since 'Group A' is the first group and it's not at the first position, it can move to a new index. Therefore, the output is [0].

Example 2

Input

[['Group A', 1], ['Group B', 0], ['Group C', 2]]

Output

[0]

Explanation: Step 1: Check if the first group is not at the first position. In this case, 'Group A' is not at the first position. Step 2: Since 'Group A' is the first group and it's not at the first position, it can move to a new index. Therefore, the output is [0].

Constraints

  • The number of groups is between 2 and 10 (inclusive).
  • The original lineup index for each group is between 1 and the number of stages (inclusive).
  • The number of stages is 10 (unique and does not repeat).
  • Groups are considered not shifted if they are already at the first position.

Optimal Approach & Strategy

To optimize this solution, we can maintain an index to track the current position in the output array and only shift groups that are not at their correct position.

Brute Force Approach

This problem can be solved by iterating through each group in the list, checking if its original index is not equal to the index in the output array. If they are not equal, shift all groups in between their original and new indices to their new indices.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function optimalStageIndexShifts(groups, originalLineupIndices) { let result = []; for (let i = 1; i < groups.length; i++) { if (originalLineupIndices[i] < originalLineupIndices[0]) { result.push(groups[i]); } else if (originalLineupIndices[i] === originalLineupIndices[0]) { result.push(groups[i]); } } return result.length > 0 ? result : (originalLineupIndices[0] !== 0 ? [0] : []); }

Asked in Top Tech Interviews

RazorpayOracle

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.