Question
Asked By EnchantedEcho88 at
Answered By Expert
Shawn
Expert · 2.1k answers · 2k people helped
Step 1/4
In this question, we have M workers and N streets to clean.
Each worker is assigned a set of streets in sequential order but due to a bug, sometimes the streets assigned get overlapped or some street gets left out.
We need to find how many streets got left out.
Step 2/4
First we take an array 'a' of size 2 to store the start and end street numbers of every worker.
Then we take an empty array named 'h' with all elements as 0.
We take input from user for the values of m and n.
Step 3/4
Now we take a for loop starting from 0 till m (number of workers)
Inside the loop, we input the values of start and end street number from the user into a[0] and a[1] respectively.
Now we take an inner loop starting from start street number or a[0], ending at end street number or a[1] (inclusive) and then for the values found inside this range, we change the corresponding value in the 'h' array from 0 to 1.
In other words we mark the streets covered (from start till end for every worker) by making their values as '1' in the 'h' array which initially contained '0'.
Step 4/4
in the end, we take another loop starting from 0 till the total number of streets.
we check if in the 'h' array there is an element whose value is still 0, that means that street hasn't been assigned to any worker.
if the value is 0, we count how many 0's are there in a variable count.
Final Answer
Final Code:
Input:
2 7
0 3
3 5
Output:
1
🧑🏫 More Questions
👉 Interested in exploring further?
Chrome Extension
1. Search answers from our 90+ million questions database.
2. Get instantly AI Solutions powered by most advanced models like GPT-4, Bard, Math GPT, etc.
3. Enjoy one-stop access to millions of textbook solutions.
4. Chat with 50+ AI study mates to get personalized course studies.
5. Ask your questions simply with texts or screenshots everywhere.