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)
PLEASE HELP...DONE IN C++
//For any queries, feel free to comment.
**LOGIC**
To make sure that number of adjacent pairs of students where one student is a 0 and the other student is a 1 minimized, we have to arrange students of same type together.
So possible configurations of array can be ......
[All zeros][All ones]
[All ones][All zeros]
We will have to find answer for both the cases and return the minimum of that answer.
To find answer of each configuration, we know that number_of_moves is equal to number_of_ones present in that desired zero's area (because we have to remove that number of ones).
For more explaination, see the code below.
**CODE START**
#include <bits/stdc++.h>
using namespace std;
int minMoves(int arr[],int n){
//Count the number of zeroes
int cnt_zero = 0;
for(int i=0;i<n;i++){
if(arr[i]==0)
cnt_zero++;
}
int moves,cnt = 0,ans = n;
//Calculating no_moves when all zero are in
the beginning
//Checking first cnt_zero number of elements for
0
moves = 0;
for(int i=0;i<cnt_zero;i++){
if(arr[i]!=0){
moves++;
}
}
//Updating answer
ans = min(ans,moves);
//Calculating no_moves when all zero are in
the end
//Checking last cnt_zero number of elements for
0
moves = 0;
for(int i=n-1;cnt<cnt_zero;i--,cnt++){
if(arr[i]!=0){
moves++;
}
}
//Updating answer
ans = min(ans,moves);
return ans;
}
int main(){
//n = number of elements in array
int n;
cin>>n;
//Taking array input from user
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
//Calling function for desired answer
cout<<minMoves(arr,n);
return 0;
}
**CODE END**


**OUTPUT**

**SNIPPET END**
There are a number of students standing in a single file line. Each student is numbered...
For C++ program Your program should first prompt the user for the number of students they wish to enter. For each student, the user will be prompted to enter the student’s name, how many tests the student has taken, and the grade for each test. Each test’s grade will be inputted as a number representing the grade for that test. Once each student’s information has been entered, the program will display the number of students. Additionally, each student will have...
Your friend Raphael is an elementary school teacher. He has n students in his class, numbered 1,2,...,n. On a particularly chilly Wednesday, he decides to lure his students into attending his class by giving away candies to each of them. However, some children are mischievous and take more candies than allotted. The students who get lesser candies thus get sad, and need to be comforted. Therefore, Raphael wants your help in determining the index of the student who is the...
Python: Printing Student Data Ask the user: "How many students are in the class?" Read in the number of students If there are students For each student Ask for the name of the student: "Enter the name of student:" Read in the name of the student If the student's name is greater than 24 characters Do not display this student's data Do not ask for their gpa Print: "Name of student cannot exceed 24 characters." Ask for the gpa of...
C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything. Your function should be named...
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
Must be done in Java.
PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS
WELL.
PROBLEM 1 INFORMATION IF YOU NEED IT AS
WELL:
Provide the rest of the code with full comments and
explanation and with proper indentation.
Use simple methods for better
understanding.
Must compile.
At the end, show the exact Output that's shown in
Problem3.
CODE PROVIDED FOR PROBLEM 2:
import java.util.Scanner;
public class Problem2
{
public static void main( String [] args )
{
//N...
Here is the (A3b-smallgrades.txt) text file:
Here is the (A3b-largegrades.txt) text file:
Here is the Program A3a without modification:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char
students[COLS][20]);
void printGrades(int ROWS, int COLS, int
grades[ROWS][COLS]);
void getStudents(int COLS, char students[COLS][20]);
void printStudents(int COLS, char students[COLS][20]);
void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char
Fgrades[]);
void printFinalGrades(int COLS, char Fgrades[]);
int main()
{
srand(time(0));
int stu = 0, assign = 0;...
Must be done in Java.
PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS
WELL.
Provide the rest of the code with full comments and
explanation and with proper indentation.
Use simple methods for better
understanding.
Must compile.
At the end show the exact Output that's shown in the
Problem 2.
CODE PROVIDED FOR PROBLEM 1:
import java.util.Scanner;
public class Problem1
{
public static void main( String [] args )
{
//N denoting the size of the
board
int...
Must be done in Java.
PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS
WELL.
Provide the rest of the code with full comments and
explanation and with proper indentation.
Use simple methods for better
understanding.
Must compile.
At the end, show the exact Output that's shown in
Problem 2.
CODE PROVIDED FOR PROBLEM 1:
import java.util.Scanner;
public class Problem1
{
public static void main( String [] args )
{
//N denoting the size of the
board
int n;
...
This is what I have as of right now. I am lost on what to do next#include <stdio.h>#include <iostream>using namespace std;int main(){ double scores, numOfStudents, average = 0, highscore, sum = 0; string names; cout << "Hello, please enter the number of students" << endl; cin >> numOfStudents; do { cout << "Please enter the student's name:\n"; cin >> names; cout << "Please enter the student's...