[Solved] Large Square solution codechef

Large Square solution codechef


Problem Statement :

You are given NN identical squares, each with side length AA. All the squares have their sides parallel to the xaxisx−axis and yaxisy−axis. That is, the squares are not tilted. You have to take several (possibly, zero or all) squares and rearrange them to obtain a mega square. The mega square can’t have any gap in the enclosed region or have overlapping squares. Also, you cannot rotate any square. Output the side length of the largest mega square that you can obtain.

Input Format

  • The first line of input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first and only line of each test case contains two space-separated integers N,AN,A.

Output Format

For each test case, print a single line containing one integer – the side length of the largest square you can obtain. Constraints

  • 1T1001≤T≤100
  • 1N1051≤N≤105
  • 1A1041≤A≤104

Sample Input 1 

5
3 2
5 3
16 18
11 8
8 6

Sample Output 1 

2
6
72
24
12

Explanation

Test case 11: You can choose just one square to form the mega square. So the side length will be 22.

Test case 22: You can choose 44 squares to form a mega square with side-length 2A2⋅A =6=6.


Solution 


Python

import math
for _ in range(int(input())):
n, l = map(int, input().split())
print(math.floor(math.sqrt(n))*l)


C++

#include<bits/stdc++.h>
using namespace std;
int main() {

int t;
cin>>t;

while(t–)
{
int n, m;

cin>>n>>m;

int ls = (int)(sqrt(n));
int sol = ls*m;

cout<<sol<<endl;
}
return 0;
}

 


Java

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
while(true)
{
try
{ int t= Integer.parseInt(br.readLine());
String[] input = new String[2];
int n,i,j;

for ( i = 0; i < t; i++)
{
input = br.readLine().split(” “);
n= Integer.parseInt(input[0]);
int s;
int a= Integer.parseInt(input[1]);

for(j=0;j<n;j++)
{ if(n<(j*j))
break;
}

j–;if(j==0)j++;
s=a*j;
out.println(s);
} out.flush();
}

catch(Exception e)
{
return;
}
out.close();

}
}}


C

#include<stdio.h>

int main()
{
int t;
scanf(“%d”,&t);
for(int i=1;i<=t;i++)
{
int n;
int x,rr;
scanf(“%d %d”,&n,&x);
rr=floor(sqrt(n));
printf(“%d\n”,(x*rr));

}
}


C#

#region usings
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections;
using static System.Math;
using static System.Array;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Web;

#endregion
namespace CodeChef
{
public class Solution
{
private static readonly Scanner sc = new Scanner();
private const long MOD = 1000000007;
static void Main()
{
int test = sc.ReadInt();
while (test– > 0)
{
long[] ab = sc.ReadLongArr();
Solve(ab);
}
sc.Flush();
sc.Close();
}

private static void Solve(long[] ab)
{
long a = ab[0], b = ab[1];
double root = Floor(Sqrt(a));
sc.WriteLine(root * b);
}
}
public class Scanner
{

#if (!DEBUG)
public static StreamReader streamReader = new StreamReader(Console.OpenStandardInput());
public static StreamWriter streamWriter = new StreamWriter(Console.OpenStandardOutput());
#else
public static StreamReader streamReader = new StreamReader(@”C:\Users\869963\source\repos\Competitive\Codechef\TextFile1.txt”);
public static StreamWriter streamWriter = new StreamWriter(@”C:\Users\869963\source\repos\Competitive\Codechef\TextFile2.txt”);
#endif
#region Input
public int ReadInt()
{
return Convert.ToInt32(streamReader.ReadLine());
}
public string ReadLine()
{
return streamReader.ReadLine();
}
public long ReadLong()
{
return Convert.ToInt64(streamReader.ReadLine());
}
public double ReadDouble()
{
return Convert.ToDouble(streamReader.ReadLine());
}
public int[] ReadIntArr()
{
return ConvertAll(streamReader.ReadLine().TrimEnd().Split(‘ ‘), t => Convert.ToInt32(t));
}
public float[] ReadFloatArr()
{
return ConvertAll(streamReader.ReadLine().TrimEnd().Split(‘ ‘), t => float.Parse(t));
}
public long[] ReadLongArr()
{
return ConvertAll(streamReader.ReadLine().TrimEnd().Split(‘ ‘), t => Convert.ToInt64(t));
}
public char[] ReadCharArr()
{
return streamReader.ReadLine().ToCharArray();
}
public int[][] ReadInt2DArr(long n)
{
int[][] res = new int[n][];
for (int i = 0; i < n; i++)
{
res[i] = ReadIntArr();
}
return res;
}
#endregion
#region Output
public void Write<T>(T t)
{
streamWriter.Write(t);
}
public void WriteLine<T>(T result)
{
streamWriter.WriteLine(result);
}
public void Write2DMatrix<T>(T[,] sum, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Write<string>($”{sum[i, j]}”);
}
WriteLine<string>(“”);
}
}
public void YESNO(bool condition)
{
WriteLine(!condition ? “YES” : “NO”);
}
public void YesNo(bool condition)
{
WriteLine(condition ? “Yes” : “No”);
}
public void yesno(bool condition)
{
WriteLine(condition ? “yes” : “no”);
}
public void Flush()
{
streamWriter.Flush();
}
public void Close()
{
streamReader.Close();
streamWriter.Close();
}
internal void WriteArray(long[] secondHalf)
{
for (int i = 0; i < secondHalf.Length; i++)
{
streamWriter.Write(secondHalf[i] + ” “);
}
WriteLine(“”);
}
#endregion
}
}


Also read : Black cells in a chessboard solution codechef

Large Square solution codechef

 

Leave a Comment

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