Given an array of N size denotes number of students and element of array denotes marks of students.
Marks belongs between 1 to 100, where A[i] is less or equal to 32 denotes that student was fail and A[i] grater or equal to 91 denotes it was passed with hons.
The task is to find minimum number of room allotted their all student who are fulfill the condition given blow.
Conditions:
- If student was fail then not allot the room.
- If passed with hons then he was stay single in a room.
- If student obtain 33 to 90 marks then they are stay double in a room.
Examples:
Input : a[] = {23, 56, 89, 34, 67, 54, 56} Output : 3 Since total student are 7, and one student get 23 marks that student fail and not apply for hostel, other six student are passed and marks in range of 33 to 90 So they are stay in double in a room, hence minimum number of room allotted is 3. Input : A[] = {24, 1, 8, 32, 45, 45, 45, 67, 87, 96, 75, 96, 98, 96, 96} Output : 8
Recommended: Features Introduced in C++ 14
Approach:
Since the problem has a restriction on array if any element are lesser then 33 then no room allotted.
If grater then 90 then count one by one if 33 to 90 then its count two in one.
Below is the implementation of the above approach:
// C++ program for finding the minimum number of rooms required #include <bits/stdc++.h> using namespace std; // Find the minimum number of rooms required int minimumRoomRequired(int a[], int N) { int i, b[2] = {0}, sum = 0; for (i = 0; i < N; i++) { if (a[i] < 33) continue; else if (a[i] >= 33 && a[i] <= 90) b[0]++; else b[1]++; } b[0] = (b[0] + 1) / 2; sum = b[1] + b[0]; return sum; } // Driver main function int main() { int a[] = {32, 56, 76, 54, 87, 56, 98, 34, 65, 56, 91, 91}; cout << minimumRoomRequired(a, sizeof(a) / sizeof(a[0])); return 0; }
Output:
7
Time Complexity: O(N)
Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.