Menu Close

Coins and Triangle in Java …FTC

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.
Isn’t 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.
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?

Input
The first line of input contains a single integer T denoting the number of test cases.
Followed by an integer N denoting the number of gold coins Chef has.

Output
For each test case, output a single line containing an integer corresponding to the maximum possible height of the triangle that Chef can get.

Constraints:
1 <= T <= 100
1 <= N <= 10 power 9

INPUT:
Enter the TestCases:  1
No: of Coin Chef has:  50000

OUTPUT:
Maximum possible height of the triangle that Chef can get:  315
import java.io.*;
import java.util.*;
class temp{
  static int arr[];
  static int t;
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the TestCases:  ");
    int test = sc.nextInt();

    arr = new int[test];
    for(t = 0; t < test; t++){
      System.out.print("No: of Coin Chef has:  ");
      arr[t] = sc.nextInt();
      try{
          solve();
        }
      catch(Exception e){
        e.printStackTrace(System.out);
      }
    }
  }

  static void solve(){
    int i,f=0;
    int n = 0;
    int N = arr[t];
    for ( i = 1; f <=N ; i++){
      f+=i;
    }

    System.out.print("Maximum possible height of the triangle that Chef can get: " + (i-2));
  }
}

INPUT_1:
Enter the TestCases:  4
No: of Coin Chef has:  1
OUTPUT:
Maximum possible height of the triangle that Chef can get:  1

No: of Coin Chef has:  4
OUTPUT:
Maximum possible height of the triangle that Chef can get:  2

No: of Coin Chef has:  5
OUTPUT:
Maximum possible height of the triangle that Chef can get:  2

No: of Coin Chef has:  8
OUTPUT:
Maximum possible height of the triangle that Chef can get:  3


INPUT_2:
Enter the TestCases:  6
No: of Coin Chef has:  3
OUTPUT:
Maximum possible height of the triangle that Chef can get:  2

No: of Coin Chef has:  5
OUTPUT:
Maximum possible height of the triangle that Chef can get:  2

No: of Coin Chef has:  12
OUTPUT:
Maximum possible height of the triangle that Chef can get:  4

No: of Coin Chef has:  18
OUTPUT:
Maximum possible height of the triangle that Chef can get:  5

No: of Coin Chef has:  34
OUTPUT:
Maximum possible height of the triangle that Chef can get:  7

No: of Coin Chef has:  1
OUTPUT:
Maximum possible height of the triangle that Chef can get:  1


INPUT_3:
Enter the TestCases:  3
No: of Coin Chef has:  100
OUTPUT:
Maximum possible height of the triangle that Chef can get:  13

No: of Coin Chef has:  500
OUTPUT:
Maximum possible height of the triangle that Chef can get:  31

No: of Coin Chef has:  1000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  44


INPUT_4:
Enter the TestCases:  2
No: of Coin Chef has:  10000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  140

No: of Coin Chef has:  100000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  446


INPUT_5:
Enter the TestCases:  1
No: of Coin Chef has:  50000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  315


INPUT_6:
Enter the TestCases:  3
No: of Coin Chef has:  35000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  264

No: of Coin Chef has:  2000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  62

No: of Coin Chef has:  20000
OUTPUT:
Maximum possible height of the triangle that Chef can get:  199


ILLUSTRATION

Executed using javac on linux terminal

Leave a Reply

Your email address will not be published. Required fields are marked *