Showing posts with label Code vita 9. Show all posts
Showing posts with label Code vita 9. Show all posts

Railway Station - Code Vita 9 | Zonal Round

 Railway Station

Given schedule of trains and their stoppage time at a Railway Station, find minimum number of platforms needed.

Note -If Train A's departure time is x and Train B's arrival time is x, then we can't accommodate Train B on the same platform as Train A.

Constraints

1 <= N <= 10^5

0 <= a <= 86400

0 < b <= 86400

Number of platforms > 0

Input

First line contains N denoting number of trains.

Next N line contain 2 integers, a and b, denoting the arrival time and stoppage time of train.

Output

Single integer denoting the minimum numbers of platforms needed to accommodate every train.

Example 1

Input

3

10 2

5 10

13 5

Output

2

Explanation

The earliest arriving train at time t = 5 will arrive at platform# 1. Since it will stay there till t = 15, train arriving at time t = 10 will arrive at platform# 2. Since it will depart at time   t = 12, train arriving at time t = 13 will arrive at platform# 2.

Example 2

Input

2

2 4

6 2

Output

2

Explanation

Platform #1 can accommodate train 1.

Platform #2 can accommodate train 2.

Note that the departure of train 1 is same as arrival of train 2, i.e. 6, and thus we need a separate platform to accommodate train 2.

Program:

#include<stdio.h>

#include<math.h>

int main()

{

int n,swap=0;

scanf("%d",&n);

int a[n],b[n];

for(int i=0;i<n;i++)

{

scanf("%d",&a[i]);

scanf("%d",&b[i]);

b[i]=a[i]+b[i];

}

for (int c = 0 ; c < n ; c++)

    {

    for ( int d = 0 ; d < n - c - 1; d++)

    {

      if (a[d] > a[d+1]) /* For decreasing order use '<' instead of '>' */

      {

        swap       = a[d];

        a[d]   = a[d+1];

        a[d+1] = swap;

      }

    }

    }

    for (int c = 0 ; c < n ; c++)

    {

    for ( int d = 0 ; d < n - c - 1; d++)

    {

      if (b[d] > b[d+1]) /* For decreasing order use '<' instead of '>' */

      {

        swap       = b[d];

        b[d]   = b[d+1];

        b[d+1] = swap;

      }

    }

    }

int p=1,r=1,i=1,j=0;

while(i<n && j<n)

{

if(a[i]<=b[j])

{

p++;

i++;

}

else if(a[i]>b[j])

{

p--;

j++;

}

if(p>r)

r=p;

}

printf("%d",r);

}


You can also run it on an online IDE:    

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Related Links:


Minimum Gift - CodeVita 9 | Zonal Round

 Minimum Gift

A Company has decided to give some gifts to all of its employees. For that, company has given some rank to each employee. Based on that rank, company has made certain rules to distribute the gifts.

The rules for distributing the gifts are:

Each employee must receive at least one gift.

Employees having higher ranking get a greater number of gifts than their neighbours.

What is the minimum number of gifts required by company?

Constraints

1 < T < 10

1 < N < 100000

1 < Rank < 10^9

Input

First line contains integer T, denoting the number of test cases.

For each test case:

First line contains integer N, denoting the number of employees.

Second line contains N space separated integers, denoting the rank of each employee.

Output

For each test case print the number of minimum gifts required on new line.

Example 1

Input

2

5

1 2 1 5 2

2

1 2

Output

7

3

Explanation

For testcase 1, adhering to rules mentioned above,

Employee # 1 whose rank is 1 gets one gift

Employee # 2 whose rank is 2 gets two gifts

Employee # 3 whose rank is 1 gets one gift

Employee # 4 whose rank is 5 gets two gifts

Employee # 5 whose rank is 2 gets one gift

Therefore, total gifts required is 1 + 2 + 1 + 2 + 1 = 7

Similarly, for testcase 2, adhering to rules mentioned above,

Employee # 1 whose rank is 1 gets one gift

Employee # 2 whose rank is 2 gets two gifts

Therefore, total gifts required is 1 + 2 = 3

Program:

#include<stdio.h>

long int arr[100010];

long int brr[100010];

int main()

{

  int test_case;

  scanf("%d",&test_case);

  for(int i = 1; i <= test_case; i++)

  {

    int n;

    long int gift = 0, temp = 0;

    scanf("%d",&n);

    for(int i = 0; i < n; i++)

    {

        scanf("%ld",&arr[i]);

    }

    brr[0] = 1;

    for(int i = 1; i < n; i++)

    {

      if(arr[i] > arr[i-1])

      {

        brr[i] = brr[i-1] + 1;

      }

      else

      {

        brr[i] = 1;

      }

    }

    gift = brr[n-1];

    for(int i = n-2; i >= 0; i--)

    {

      if(arr[i] > arr[i+1])

      {

        temp = brr[i+1] + 1;

      }

      else

        temp = 1;

      gift = gift + ((temp > brr[i]) ? temp : brr[i]);

      brr[i] = temp;

    }

    printf("%ld\n",gift);

  }

  return 0 ;

}

You can also run it on an online IDE:    

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Related Links:




Prime Time Again - CodeVita 9 | Zonal Round

Prime Time Again


Here on earth, our 24-hour day is composed of two parts, each of 12 hours. Each hour in each part has a corresponding hour in the other part separated by 12 hours: the hour essentially measures the duration since the start of the day part. For example, 1 hour in the first part of the day is equivalent to 13, which is 1 hour into the second part of the day.

Now, consider the equivalent hours that are both prime numbers. We have 3 such instances for a 24-hour 2-part day:

5~17

7~19

11~23

Accept two natural numbers D, P >1 corresponding respectively to number of hours per day and number of parts in a day separated by a space. D should be divisible by P, meaning that the number of hours per part (D/P) should be a natural number. Calculate the number of instances of equivalent prime hours. Output zero if there is no such instance. Note that we require each equivalent hour in each part in a day to be a prime number.


Example:

Input: 
24 2

Output: 
3 (We have 3 instances of equivalent prime hours: 5~17, 7~19 and 11~23.)

Constraints

10 <= D < 500

2 <= P < 50

Input

Single line consists of two space separated integers, D and P corresponding to number of. hours per day and number of parts in a day respectively

Output

Output must be a single number, corresponding to the number of instances of equivalent prime number, as described above


Example 1

Input

36 3

Output

2

Explanation

In the given test case D = 36 and P = 3

Duration of each day part = 12

2~14~X

3~15~X

5~17~29 - instance of equivalent prime hours

7~19~31 - instance of equivalent prime hours

11~23~X

Hence the answers is 2.


Program:

#include<stdio.h>
#include<math.h>
int isprime(int n)
{
if(n==1)
return 0;
for(int i=2;i<=(int)sqrt(n);i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main()
{
int D,P,i,j,p,t=1;
scanf("%d",&D);
scanf("%d",&P);
p=D/P;
int time[p][P];
for(i=0;i<P;i++)
{
for(j=0;j<p;j++)
{
time[j][i]=t++;
}
}
t=0;
for(i=0;i<p;i++)
{
int flag=1;
for(j=0;j<P;j++)
{
if(!isprime(time[i][j]))
{
flag=0;
break;
}
}
if(flag)
t++;
}
printf("%d",t);

}


You can also run it on an online IDE:    

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Related Links:

https://codepiggy.blogspot.com/2021/02/constellation-codevita-9-zonal-round.html

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...