Number of Students Doing Homework at a Given Time

Given two integer arrays startTime and endTime and given an integer queryTime.

The ith student started doing their homework at the time startTime[i] and finished it at time endTime[i].

Return the number of students doing their homework at the time queryTime.

Example 1:

Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1

Approach

Java

package com.practic.array;

public class BusyStudent {
    public static void main(String[] args) {
        int startTime[] = { 123 };
        int endTime[] = { 327 };
        int queryTime = 4;
        int totalBusy = busyStudent(startTime, endTime, queryTime);
        System.out.println(totalBusy);
    }

// method to find total busy student 
    public static int busyStudent(int[] startTimeint[] endTimeint queryTime) {
        int total = 0;
        // iterate till end of array
        for (int i = 0; i < startTime.length; i++) {
            // busy check condition here
            if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
                total++;
            }
        }
        return total;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int busyStudent(vector<int>& startTime
      vector<int>& endTimeint queryTime
{
    int n=startTime.size(),cnt=0;
    for(int i=0;i<n;i++)
       {
         if(queryTime>=startTime[i]&&queryTime<=endTime[i])
                     cnt++;
       }
     return cnt;
}
int main()
{
  vector<intstartTime ={1,2,3};
  vector<intendTime = {3,2,7};
  int queryTime = 4;
  int busy=busyStudent(startTime,endTime,queryTime);
  cout<<busy<<"\n";
  return 0;
}


No comments:

Post a Comment