Count Pairs - Code Vita 9 | Zonal Round

 Count Pairs

Given an array of integers A, and an integer K find number of happy elements.Element X is happy if there exists at least 1 element whose difference is less than K i.e. an element X is happy, if there is another element in the range [X-K, X+K] other than X itself.

Constraints

1 <= N <= 10^5

0 <= K <= 10^5

0 <= A[i] <= 10^9

Input

First line contains two integers N and K where N is size of the array and K is a number as described above. Second line contains N integers separated by space.

Output

Print a single integer denoting the total number of happy elements.

Example 1

Input

6 3

5 5 7 9 15 2

Output

5

Explanation

Other than number 15, everyone has at least 1 element in the range [X-3, X+3]. Hence they are all happy elements. Since these five are in number, the output is 5.

Example 2

Input

3 2

1 3 5

Output

3

Explanation

All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy elements. Since these three are in number, the output is 3.

Program:

#include <stdio.h>

int pairs(int elementlst[],int n,int z){

 int count=0;

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

    int a=elementlst[i];

    int id1=i;

    int id2=i;

    if(i==0){

      while(elementlst[id2+1]==a)

         id2+=1;

      if(elementlst[id2+1]<=a+z && elementlst[id2+1]>=a-z)

        count+=1;

    }

    else if(i<n-1){

      while(elementlst[id2+1]==a)

        id2+=1;

      while(elementlst[id1-1]==a)

        id1-=1;

      if(((elementlst[id1-1]<=a+z) && (elementlst[id1-1]>=a-z)) || ((elementlst[id2+1]<=a+z) && (elementlst[id2+1]>=a-z)))

        count+=1;

    }

    else{

      while(elementlst[id1-1]==a)

        id1-=1;

      if(elementlst[id1-1]<=a+z && elementlst[id1-1]>=a-z)

        count+=1;

    }

  }

  return count;

}

int main() {

int n,z,swap=0;

scanf("%d",&n);

scanf("%d",&z);

int elementlst[n];

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

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

}

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

    {

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

    {

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

      {

       swap       = elementlst[d];

        elementlst[d]   = elementlst[d+1];

        elementlst[d+1] = swap;

      }

    }

    }

printf("%d",pairs(elementlst,n,z));

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:


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

Constellation - CodeVita 9 | Zonal Round

Constellation 

 Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in shape of vowels { A, E, I, O, U } . A collection of * in the shape of the vowels is a star. A star is contained in a 3x3 block. Stars cannot be overlapping. The dot(.) character denotes empty space. 

 Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them. 

 Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples section below. 

 Constraints 

 3 <= N <= 10^5 

 Input

 Input consists of single integer N denoting number of columns. 

 Output 

 Output contains vowels (stars) in order of their occurrence within the given galaxy. Galaxy itself is represented by # character. 

Example 1:

Input:

18

* . * # * * * # * * * # * * * . * .

* . * # * . * # . * . # * * * * * *

* * * # * * * # * * * # * * * * . *

Output: 

U#O#I#EA

Example 2:

Input:

12

* . * # . * * * # . * .

* . * # . . * . # * * *

* * * # . * * * # * . *
Output:

U#I#A

Program:

#include <stdio.h>
int main()
{
  int n,x1,y1;
  scanf("%d",&n);
  char x[3][n];
  for(int i=0;i<3;i++)
{
    for(int j=0;j<n;j++)
{
      scanf("%s",&x[i][j]);
    }
  }
  for(int i=0;i<n;i++)
  {
    if(x[0][i]=='#' && x[1][i]=='#' && x[2][i]=='#')
{
      printf("#");
    }
else if(x[0][i]=='.' && x[1][i]=='.' && x[2][i]=='.')
{}
else
{
      char a,b,c,a1,b1,c1,a2,b2,c2;
      x1 = i;
      a = x[0][x1];
      b = x[0][x1+1];
      c = x[0][x1+2];
      a1 = x[1][x1];
      b1 = x[1][x1+1];
      c1 = x[1][x1+2];
      a2 = x[2][x1];
      b2 = x[2][x1+1];
      c2 = x[2][x1+2];
      if(a=='.' && b=='*' && c=='.' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='.' && c2=='*')
 
      printf("A");
        i = i + 2;
      }
      if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
 
        printf("E");
        i = i + 2;
      }
      if(a=='*' && b=='*' && c=='*' && a1=='.' && b1=='*' && c1=='.' && a2=='*' && b2=='*' && c2=='*')
 
        printf("I");
        i = i + 2;
      }
      if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
 
        printf("O");
        i = i + 2;
      }
      if(a=='*' && b=='.' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
 
          printf("U");
        i = i + 2;
      }
    }
  }

}

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:

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...