File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ int minMovesToSeat (vector<int >& seats, vector<int >& students) {
7
+ sort (seats.begin (), seats.end ());
8
+ sort (students.begin (), students.end ());
9
+ int moves = 0 ;
10
+ for (int i=0 ; i<seats.size (); i++)
11
+ moves += abs (seats[i] - students[i]);
12
+
13
+ return moves;
14
+ }
15
+ };
16
+
17
+ int main () {
18
+ int num_seats, num_students;
19
+
20
+
21
+ cout << " Enter the number of seats: " ;
22
+ cin >> num_seats;
23
+ cout << " Enter the number of students: " ;
24
+ cin >> num_students;
25
+
26
+
27
+ if (num_seats <= 0 || num_students <= 0 || num_seats != num_students) {
28
+ cerr << " Error: Invalid input. Number of seats and students must be positive and equal." << endl;
29
+ return 1 ;
30
+ }
31
+ vector<int > seats (num_seats);
32
+ cout << " Enter the desired seat positions for students (space-separated): " ;
33
+ for (int i = 0 ; i < num_seats; ++i) {
34
+ cin >> seats[i];
35
+ }
36
+
37
+ vector<int > students (num_students);
38
+ cout << " Enter the student IDs (space-separated): " ;
39
+ for (int i = 0 ; i < num_students; ++i) {
40
+ cin >> students[i];
41
+ }
42
+
43
+ Solution s;
44
+ int min_moves = s.minMovesToSeat (seats, students);
45
+
46
+ cout << " Minimum number of moves required to seat all students: " << min_moves << endl;
47
+
48
+ return 0 ;
49
+ }
You can’t perform that action at this time.
0 commit comments