4 Particles Codevita 9 Solution

4 Particles Codevita 9 Solution

There is a cube of height H, and there are 4 moving particles on the vertical edges of the cube. Initially particles are at some height A, B, C and
D respectively. These particles are moving in different direction (Only upward or downward, no sideways movement) with different speed.

If the particle is moving upward or downward reaches the tip of the cube then it remain at the tip only and will not move further. If other particles
have not reach the tip they continue to move along their respective edges in their respective direction till the last particle reaches the tip.

These 4 particles will make two triangles in a 3-D plane. Since the particles are moving, sum of the area of these two triangles will change
every moment.

Find out the maximum and minimum of the sum of the areas of these two triangles.

Refer the Examples section for better understanding.

Constraints

  • 1 <= H <= 100
  • 0 <= A, B, C, D <= H
  • 0 <= V1, V2, V3, V4 <= 100

Input

  • First line contains an integer H which denotes the length of the side of cube.
  • Second line contains 4 space separated integers denoting the initial position of all 4 particles on the 4 vertical edges, say A, B, C and D
    respectively.
  • Third line contains 4 space separated integers denoting the speed of all particles, say V1, V2, V3, and V4 per time unit respectively.
  • Fourth line contains 4 space separated characters U or D denoting the direction of movement of particles. U denotes the upward direction and D denotes the downward direction.
4 Particles Codevita 9 Solution

Output

Print 2 space separated integers which denote the value

[4 * [Max (sum of area of triangle ABC and area of triangle ADC)]2] and [4 * [Min (sum of area of triangle ABC and area of triangle ADC)]2] respectively. If the above values are decimal value round them off to nearest integer.

Time Limit

1

Examples
Example 1
Input
10
5 5 5 5                                                                                                                                                                      1 1 1 1
U U D D
Output
80000 40000
Explanation
The movement per time unit is depicted as shown in the diagrams below.

4 Particles Codevita 9 Solution
4 Particles Codevita 9 Solution
4 Particles Codevita 9 Solution

Note: Within a state the area of sum of triangle ABC and triangle ADC is constant. The MAXAREA and MINAREA terms used in the diagrams above
are used to keep a track of maximum area and minimum area achieved until that point in time.

MAXAREA = [4 * [Max (sum of area of triangle ABC and area of triangleADC)]2]

MINAREA = [4 * [Min (sum of area of triangle ABC and area of triangleADC)]2

Example 2
Input
10
5 5 5 5
1 2 1 2
D U U D

Output
80000 40000
Explanation
The movement per time unit is depicted as shown in the diagrams below.

4 Particles Codevita 9 Solution
4 Particles Codevita 9 Solution
4 Particles Codevita 9 Solution

Note: Within a state the area of sum of triangle ABC and triangle ADC is constant. The MAXAREA and MINAREA terms used in the diagrams above
are used to keep a track of maximum area and minimum area achieved until that point in time.

MAXAREA = [4 * [Max (sum of area of triangle ABC and area of triangleADC)]2

MINAREA = [4 * [Min (sum of area of triangle ABC and area of triangleADC)]2]

Example 3
Input
10
5 5 5 5
1 1 1 1
U D U D
Output
120000 40000
Explanation
The movement per time unit is depicted as shown in the diagrams below.

4 Particles Codevita 9 Solution
4 Particles Codevita 9 Solution

Note: Within a state the area of sum of triangle ABC and triangle ADC is constant. The MAXAREA and MINAREA terms used in the diagrams above
are used to keep a track of maximum area and minimum area achieved until that point in time.

MAXAREA = [4 * [Max (sum of area of triangle ABC and area of triangleADC)]2

MINAREA = [4 * [Min (sum of area of triangle ABC and area of triangleADC)]2

SOLUTION

Program: 4 Particles Codevita 9 Solution in C++

#include <bits/stdc++.h>
#include <math.h>
#include <cmath>
using namespace std;
float dist(float x1, float x2, float y1, float y2, float z1, float z2)
{
    float d = 0;
    d = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0);
    return d;
}
float area(float side1, float side2, float side3)
{
    float s = (side1 + side2 + side3) / 2;
    float are = sqrt(s * (s - side1) * (s - side2) * (s - side3));
    return are;
}
int main()
{
    float h, a, b, c, d, va, vb, vc, vd;
    cin >> h >> a >> b >> c >> d >> va >> vb >> vc >> vd;
    char da, db, dc, dd;
    cin >> da >> db >> dc >> dd;
    if (da == 'D')
    {
        va = va * (-1);
    }
    if (db == 'D')
    {
        vb = vb * (-1);
    }
    if (dc == 'D')
    {
        vc = vc * (-1);
    }
    if (dd == 'D')
    {
        vd = vd * (-1);
    }
    float xa = 0, ya = h * (-1);
    float xb = h, yb = h * (-1);
    float xc = h, yc = 0;
    float xd = 0, yd = 0;
    float z[100][4];
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            z[i][j] = 0;
        }
    }
    z[0][0] = a;
    z[0][1] = b;
    z[0][2] = c;
    z[0][3] = d;
    for (int i = 1; i < 100; i++)
    {
        z[i][0] = z[i - 1][0] + va;
        z[i][1] = z[i - 1][1] + vb;
        z[i][2] = z[i - 1][2] + vc;
        z[i][3] = z[i - 1][3] + vd;
        if (z[i][0] > h)
        {
            z[i][0] = h;
        }
        if (z[i][0] < 0)
        {
            z[i][0] = 0;
        }
        if (z[i][1] > h)
        {
            z[i][1] = h;
        }
        if (z[i][1] < 0)
        {
            z[i][1] = 0;
        }
        if (z[i][2] > h)
        {
            z[i][2] = h;
        }
        if (z[i][2] < 0)
        {
            z[i][2] = 0;
        }
        if (z[i][3] > h)
        {
            z[i][3] = h;
        }
        if (z[i][3] < 0)
        {
            z[i][3] = 0;
        }
    }
    float ab[100];
    for (int i = 0; i < 100; i++)
    {
        ab[i] = dist(xa, xb, ya, yb, z[i][0], z[i][1]);
    }
    float bc[100];
    for (int i = 0; i < 100; i++)
    {
        bc[i] = dist(xb, xc, yb, yc, z[i][1], z[i][2]);
    }
    float ac[100];
    for (int i = 0; i < 100; i++)
    {
        ac[i] = dist(xa, xc, ya, yc, z[i][0], z[i][2]);
    }
    float ad[100];
    for (int i = 0; i < 100; i++)
    {
        ad[i] = dist(xa, xd, ya, yd, z[i][0], z[i][3]);
    }
    float bd[100];
    for (int i = 0; i < 100; i++)
    {
        bd[i] = dist(xb, xd, yb, yd, z[i][1], z[i][3]);
    }
    float cd[100];
    for (int i = 0; i < 100; i++)
    {
        cd[i] = dist(xc, xd, yc, yd, z[i][2], z[i][3]);
    }

    float abc[100];
    for (int i = 0; i < 100; i++)
    {
        abc[i] = area(ab[i], bc[i], ac[i]);
    }
    float adc[100];
    for (int i = 0; i < 100; i++)
    {
        adc[i] = area(ad[i], cd[i], ac[i]);
    }
    float abd[100];
    for (int i = 0; i < 100; i++)
    {
        abd[i] = area(ab[i], ad[i], bd[i]);
    }
    float bcd[100];
    for (int i = 0; i < 100; i++)
    {
        bcd[i] = area(bc[i], cd[i], bd[i]);
    }

    float maxabc = abc[0];
    for (int i = 0; i < 100; i++)
    {
        if (maxabc < abc[i])
            maxabc = abc[i];
    }
    float minabc = abc[0];
    for (int i = 0; i < 100; i++)
    {
        if (minabc > abc[i])
            minabc = abc[i];
    }
    float maxadc = adc[0];
    for (int i = 0; i < 100; i++)
    {
        if (maxadc < adc[i])
            maxadc = adc[i];
    }
    float minadc = adc[0];
    for (int i = 0; i < 100; i++)
    {
        if (minadc > adc[i])
            minadc = adc[i];
    }
    float ans1 = 4 * pow((maxabc + maxadc), 2);
    float ans2 = 4 * pow((minabc + minadc), 2);
    cout << round(ans1) << " " << round(ans2) << endl;
    return 0;
}

Codevita Season 9 All Questions Solution

1 thought on “4 Particles Codevita 9 Solution”

  1. import java.util.*;
    public class Cubical {
    static int[] initialpos=new int[4];
    static int[] currentpos=new int[4];
    static int[] speed=new int[4];
    static char[] direction=new char[4];
    static boolean [] reachedend=new boolean[4];static int height=0;
    static boolean timerrunning=true;static int timer=0;static double currentarea, max=0,min=0;
    public static void main(String args[]){
    for(int i=0;i<4;i++) reachedend[i]=false;//initialize the status as false.

    Scanner sc=new Scanner(System.in);
    height=sc.nextInt();
    for(int a=0;a<4;a++){
    initialpos[a]=sc.nextInt();
    }
    for(int a=0;a<4;a++){
    speed[a]=sc.nextInt();
    }
    sc.nextLine();int i=0;
    String directions=sc.nextLine();
    //tested input till here.
    //extract the chars.
    for(char ch:directions.toCharArray())
    {
    if(ch!=' ')
    {
    direction[i]=ch;i++;
    }
    }
    //all input tested and done.

    while(timerrunning){timer++;
    currentpos=getpos(timer);
    currentarea=getarea(currentpos);
    if(timer==1){
    max=currentarea;
    min=currentarea;

    }
    if(timer>1)
    {
    if(min>currentarea)min=currentarea;
    if(max=height)){
    currentpos[i]=initialpos[i]+speed[i]*timer;

    }else {
    currentpos[i]=height;reachedend[i]=true;
    }

    }
    if(direction[i]=='D'){
    if(!((initialpos[i]-speed[i]*timer)<=0)){
    currentpos[i]=initialpos[i]-speed[i]*timer;

    }else {
    currentpos[i]=0;reachedend[i]=true;
    }
    }
    }
    pos=currentpos;
    return pos;
    }
    //getpos function is tested verbally.

    public static double getarea(int pos[]){
    double area=0;
    double a,b,c,d,s1,s2,s3,s4,s5;
    a=pos[0];
    b=pos[1];
    c=pos[2];
    d=pos[3];
    s1=Math.sqrt(Math.pow(a-b, 2)+height*height);
    s2=Math.sqrt(Math.pow(b-c, 2)+height*height);
    s3=Math.sqrt(Math.pow(c-d, 2)+height*height);
    s4=Math.sqrt(Math.pow(d-a, 2)+height*height);
    s5=Math.sqrt(Math.pow(b-d, 2)+2*height*height);
    double p1=(s1+s4+s5)/2;double area1=Math.sqrt(p1*(p1-s1)*(p1-s4)*(p1-s5));
    double p2=(s2+s3+s5)/2; double area2=Math.sqrt(p2*(p2-s2)*(p2-s3)*(p2-s5));

    area=area1+area2;

    return area;
    }

    static void p(String s){

    System.out.println(s);

    }

    }

    Reply

Leave a Comment

fourteen − 3 =