Today...we should see an interesting application of hashing technique....
The Question is Quite simple and required basic programming skills.....
The Question is Quite simple and required basic programming skills.....
One Way Road
There are 4 Trucks A,B,C and D.
But the road is of only one way and we are interested for the departure of the truck and not the arrival.
Truck A will depart on every p,2p,3p day and so on.
Truck B will depart on every q day ,Truck C will depart on every r day and truck D will depart on every s day.
Find maximum number of Trucks that can be depart in the period of N days.
But the road is of only one way and we are interested for the departure of the truck and not the arrival.
Truck A will depart on every p,2p,3p day and so on.
Truck B will depart on every q day ,Truck C will depart on every r day and truck D will depart on every s day.
Find maximum number of Trucks that can be depart in the period of N days.
Note No more than 1 truck can depart in single day
Examples:The first input is number of total days followed by p,q,r,s
Input :10 2 3 4 5 Output :4//on day 2 Truck A can depart,on day 3 and 9 Truck B can depart. Truck C cannot depart and Truck D can depart on day 5.Input :10 2 2 4 4 Output :0//Since moret than 1 truck is having same pattern of departure
Approach
We can apply the concept of hashing in this question.We know that the count will only increase if and only if there is only 1 Truck depart on particular day.
So we will maintain an array arr which will hold the records of the Truck which have already departed on a particular day.If after all the Trucks i.e A,B,C and D have departed in total N days then if the count is still 1 in that index then it is valid.
Else we will discard that day.
So we will maintain an array arr which will hold the records of the Truck which have already departed on a particular day.If after all the Trucks i.e A,B,C and D have departed in total N days then if the count is still 1 in that index then it is valid.
Else we will discard that day.
#include<bits/stdc++.h>using namespace std;int Check_Truck(int N,int p,int q,int r,int s){ //make an array of size N to store the take off day int arr[N+1]; int count=0; //To store the total number of days. //Initialize every element of the array with 0 memset(arr,0,sizeof(arr)); //check for the Truck A i.e on p day for(int i=p;i<=N;i=i+p) arr[i]=arr[i] + 1; //If there are trucks which depart on these day then increment the value. //check for the Truck B i.e on q day for(int i=q;i<=N;i=i+q) arr[i]=arr[i] + 1; //If there are trucks which depart on these day then increment the value. //check for the Truck C i.e on r day for(int i=r;i<=N;i=i+r) arr[i]=arr[i] + 1; //If there are trucks which depart on these day then increment the value. //check for the Truck D i.e on s day for(int i=s;i<=N;i=i+s) arr[i]=arr[i] + 1; //If there are trucks which depart on these day then increment the value. //finally check for the answer for(int i=1;i<=N;i++) { if(arr[i]==1) count++; } return count;}//Driver programint main(){ int N,p,q,r,s; N=10,p=2,q=3,r=4,s=5; int answer=Check_Truck(N,p,q,r,s); cout<<answer; return 0;}
Time Complexity :O(n)
Kindly follow the Blog for more such updates...
If you find anything wrong kindly mention that in the comments or email us....
Comments
Post a Comment