Thursday, September 13, 2018

TRIANGLE HEIGHT

  • Problem Description

    Chef belongs to a very rich family which owns many gold mines. Today, he brought N gold coins and decided to form a triangle using these coins. Isnt it strange?

    Chef has a unusual way of forming a triangle using gold coins, which is described as follows:

    He puts 1 coin in the 1st row.
    then puts 2 coins in the 2nd row.
    then puts 3 coins in the 3rd row.
    and so on as shown in the given figure.
    Chef is interested in forming a triangle with maximum possible height using at most N coins. Can you tell him the maximum possible height of the triangle?
  • CODING ARENA::
  • #include<stdio.h>
    int main(){
    int T,i,j;
    scanf("%d",&T);
    long int n[T];
    for( i=0;i<T;i++)
    {scanf("%li",&n[i]);
    }
    for( i=0;i<T;i++)
     {j=1;
      while(n[i]>=0)
      {n[i]=n[i]-j;
       j++;
      }
      printf("%d\n",j-2);
     }
     return 0;
    }  
  • Test Case 1

    Input (stdin)
    3
    
    3
    
    5
    
    7
    
    
    Expected Output
    2
    
    2
    
    3
  • Test Case 2

    Input (stdin)
    2
    
    6
    
    3
    
    
    Expected Output
    3
    
    2

No comments:

Post a Comment