Question of the Day - Concatenated Square - Newton School - 19/03/2023

 


Concatenated Square?


Problem Statement

Alexa found two positive integers, a and b. Determine whether the concatenation of a and b in this order is a 
square number.

Input
The input consists of two space separated integers a and b.

Constraints
1 ≤ a, b ≤ 100
a and b are integers.

Output
If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Approach: 

So in this problem what we can do - we have given the two integer numbers, first we have to do the concatenation of these numbers. After that we have to find the square root of concatenated number. If this number is a square of any number then we have to print Yes else we have to print  No. That means that number is not a square of any number.

Lets take one example.

We have given two integers a=1 and b=21, Now we have to do the concatenation of these numbers.

So concatenation of a and b is = 121, now we have to check if this 121 is a square of any number or Not. 121 is the square of 11 so we can print Yes

So to solve this problem will take two numbers a and b as inputs in string format. Now will do the concatenation of given number after that we have to find the square root of this number. So we find the square root of this concatenated number using Math.sqrt() function. If we get the number in integer format that means we can say that this number is a square root of a number.

Solution:-

Java Code:-

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc=new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();

        int ans = Integer.parseInt(a + b);

        int result  = (int) Math.sqrt(ans);

        if(result * result == ans){
            System.out.print("Yes");
        }
        else {
            System.out.print("No");
        }
    }
}


C++ Code:-

#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;

int main() {
    // Your code here
    string a,b;
    cin>>a>>b;

    int ans=stoi(a+b);
    int result=sqrt(ans);

    if(result*result==ans)
    cout<<"Yes"<<endl;
    else 
    cout<<"No"<<endl;
}

JavaScript Code:-

// Your code here

function findSquare(input){
    input = input.split(" ");
    var ans = input.join("");
    ans = Math.sqrt(n);
    console.log(Number.isInteger(ans)?"Yes":"No")
}

findSquare(require("fs").readFileSync("/dev/stdin", "utf8").trim());

Python Code:-

# Your code here

import math
a,b=input().split()
ans=int(a+b)
if int(math.sqrt(ans))**2 == ans:
  print("Yes")
else:
  print("No")


Thank you for taking time to see the article. I have provided solutions in C++, Java, JavaScript, and Python languages. If you are interested then you can try that as well.

If you like this article, Please like, share and follow our blog to see such posts.

No comments:

Powered by Blogger.