Question
There are a number of students standing in a single file line. Each student is numbered sequentially from 0. Each student also has a binary
There are a number of students standing in a single file line. Each student is numbered sequentially from 0. Each student also has a binary digit associated with them, where 0 indicate the student's performance is above average and 1 indicates the student's performance is below average.
The teacher wants to groupe the sudent by performance opposite ends of the line such that the number of adjacent pairs of students where one student is a 0 and the other student is a 1 minimized. To accomplish this, any student can swap places in line with the student located immediately in front of or behind them. Each time a pair of student swap places, it counts as a move. Determine the minimum number of moves needed to create and optimal configuration.
For example, there are n=4 students arranged as avg=[0,1,0,1]. With 1 move, switching students 1 and 2, we get the array [0,0,1,1] which is optimal.
Function Description:
Complete the function MinMoves in the editor below. The function must return an integer denoting the minimum number of moves necessary to achieve an optimal configuration.
minMoves has the following parameter(s):
avg[avg[0],....avg[n-1]]: an array of binary digits
Constraints:
1 ≤ n ≤ 10^5
avg[i] E {0,1)
Step by Step Solution
3.50 Rating (163 Votes )
There are 3 Steps involved in it
Step: 1
Code include include using namespace std int minMo...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started