TCS CodeVita Question – Total distance traveled by the Beetle

Total distance traveled by the Beetle

Here, in this page we will discuss the program for finding the total distance traveled by the beetle in different programming languages, this question is asked in TCS CodeVita.
TCS CodeVita Season 10 is the tenth-edition of the prestigious coding competition which is organized by TCS every year, in the search of world’s  best coder.

Total distance traveled by the Beetle

Problem Description

A 10cm x 10cm x 10cm solid cube rests on the ground. It has a beetle on it, as well as some sweet honey spots on the cube’s surface. The beetle begins at a point on the cube’s surface and moves in a clockwise direction along the cube’s surface to the honey spots.

  • If it goes from one point on the same face to another (say, X to Y), it goes in an arc of a circle that subtends an angle of 60 degrees at the circle’s center.
  • If it travels from one point to another on a different face, it takes the shortest path on the cube’s surface, except that it never travels along its bottom.

The beetle is a Cartesian geometry student who knows the coordinates (x, y, z) of all the points it needs to visit. Its coordinate origin is one of the cube’s corners on the ground, and the z-axis points up. As a result, z=0 is the bottom surface (on which it does not crawl), and z=10 is the top. The beetle keeps track of all distances traveled and rounded the distance to two decimal places when it arrives at the following location so that the final distance is a sum of the rounded distances from spot to spot.

Input

The first line returns an integer N, the total number of points visited by the beetle (including the starting point).

The second line contains 3N non-negative numbers, each with two decimal places. These are to be interpreted as the x, y, and z coordinates of the points the beetle must visit in the given order.

Output

One line containing a number representing the total distance traveled by the beetle to two decimal places. Even if the travel distance is an integer, the output should have two decimal places.

Constraints

None of the points visited by the beetle are on the bottom face (z=0) or on any of the cube’s edges (the lines where two faces meet)

2<=N<=10

Input

3

1 1 10 2 1 10 0 1 9

Output

4.05

One comment on “TCS CodeVita Question – Total distance traveled by the Beetle”


  • mevinodrao

    https://prepinsta.com/tcs-codevita/count-pairs-problem/
    c code for above question
    happy elements count
    #include
    int n,k,i,arr[10000],j,b,a;
    int main ()
    {
    printf (“Enter the size of array”);
    scanf (“%d”, &n);
    printf (“Enter the k value to check happiness”);
    scanf (“%d”, &k);
    printf (“Enter the %d values into array”, n);
    for (i=1;i<=n;i++)
    {
    scanf ("%d",&arr[i]);
    }
    for (i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    if(j==i)
    {
    j++;
    }
    if(b==0)
    {
    if((arr[i]-3)=arr[j])
    {
    a++;
    b=1;
    }
    }
    }
    }
    b=0;
    }
    printf(“total happy elements are %d”,a);
    return 0;
    }