Fit Squares in Triangle | CodeChef Solution

Hello coders, today we are going to solve Fit Squares in Triangle CodeChef Solution whose Problem Code is TRISQ.

Fit Squares in Triangle

Task

What is the maximum number of squares of size 2×2 that can be fit in a right angled isosceles triangle of base B.
One side of the square must be parallel to the base of the isosceles triangle.
Base is the shortest side of the triangle

Input Format

First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.

Output Format

Output exactly T lines, each line containing the required answer.

Constraints

  • 1 ≤ T ≤ 103
  • 1 ≤ B ≤ 104

Sample Input

11
1
2
3
4
5
6
7
8
9
10
11

Sample Output

0
0
0
1
1
3
3
6
6
10
10

Solution – Fit Squares in Triangle | CodeChef Solution

C++

#include <iostream>
using namespace std;
int num(int n){
    if(n<4) return 0;
    
    return (n-2)/2 + num(n-2);
}
int main() {
	
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    
	    cout<<num(n)<<endl;
	}
	return 0;
}

Python

#Solution Provided by CodingBroz
for _ in range(int(input())):
    n = int(input())
    n = n - 2 
    n = n // 2
    print(int(n*(n+1)/2))

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
       int Test= sc.nextInt();
       while (Test-->0)
       {
           long base= sc.nextInt();
           base=base-2;
           base=Math.floorDiv(base,2);
           System.out.println(base*(base+1)/2);
       }
	}
}

Disclaimer: The above Problem (Fit Squares in Triangle) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.