Monday, August 20, 2018

WALK IN LIGHT

  • Problem Description

    There are N+1 light Bulb . Lights Bulb are placed at (0, 0), (1, 0), (2, 0) ... (N, 0). Initially all the light Bulb are on. You want to turn off all of them one after one. You want to follow a special pattern in turning off the lights.
    You will start at (0, 0). First, you walk to the right most light that is on, turn it off. Then you walk to the left most light that is on, turn it off. Then again to the right most light that is on and so on. You will stop after turning off all lights. You want to know how much distance you walked in the process. Note that distance between (a,0)and (b,0) is |a-b|.
    Input
    The first line of the input contains an integer T denoting the number of test cases. Each test case has a single integer N on separate line.
    Output
    For each test case, output the distance you walked.
    Constraints

    1 <=T <=10^5
    1<=N <=10^5
    Sample test case:
    You are initially at (0, 0)
    Right most on-light is (2, 0). Distance = 2.
    Now you are at (2, 0).
    Left most on-light is (0, 0). Distance = 2.
    Now you are at (0, 0)
    Right most on-light is (1, 0). Distance = 1.
    Now you are at (1, 0) and all lights Bulbare turned off.
    Total distance walked = 5.
  • CODING ARENA::
  • #include <stdio.h>
    int main()
    {
    int t;
      scanf("%d",&t);
      while(t--)
        {
          int b=0;
          scanf("%d",&b);
          int sum=((b*b)+(3*b))/2;
          printf("distance walked:%d\n",sum);
        }
    return 0;
    }
  • Test Case 1

    Input (stdin)
    2
    
    1
    
    4
    
    
    
    
    Expected Output
    distance walked:2
    
    distance walked:14
  • Test Case 2

    Input (stdin)
    1
    
    4
    
    
    Expected Output
    distance walked:14

No comments:

Post a Comment