Given two numbers a and b, the task is to check whether the concatenation of a and b is a perfect cube or not.
Examples:
Input: a = 6, b = 4
Output: Yes
Concatenating 6 and 4 = 64. Since 64 (= 4 x 4 x 4) is a perfect cube, the output is Yes.
Input: a = 100, b = 1
Output: No
Concatenating 100 and 1 = 1001. Since 1001 is not a perfect cube, the output is No.
Approach: Since appending strings are comparatively easier than appending numbers, the simplest way to solve this problem is to convert the given integers into strings. Once the concatenated number has been obtained, it can be easily checked if it is a perfect cube or not.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectCube( int x)
{
long double cr = round(cbrt(x));
return (cr * cr * cr == x);
}
void checkCube( int a, int b)
{
string s1 = to_string(a);
string s2 = to_string(b);
int c = stoi(s1 + s2);
if (isPerfectCube(c)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int a = 6;
int b = 4;
checkCube(a, b);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean isPerfectCube( int x)
{
long cr = Math.round(Math.cbrt(x));
return (cr * cr * cr == x);
}
static void checkCube( int a, int b)
{
String s1 = Integer.toString(a);
String s2 = Integer.toString(b);
int c = Integer.parseInt(s1 + s2);
if (isPerfectCube(c)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
public static void main (String[] args)
{
int a = 6 ;
int b = 4 ;
checkCube(a, b);
}
}
|
Python 3
def isPerfectCube(x):
x = abs (x)
return int ( round (x * * ( 1. / 3 ))) * * 3 = = x
def checkCube(a, b):
s1 = str (a)
s2 = str (b)
c = int (s1 + s2)
if (isPerfectCube(c)):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
a = 6
b = 4
checkCube(a, b)
|
C#
using System;
class GFG {
static bool isPerfectCube( int x)
{
double cr = Math.Round(Math.Cbrt(x));
return (cr * cr * cr == x);
}
static void checkCube( int a, int b)
{
string s1 = Convert.ToString(a);
string s2 = Convert.ToString(b);
int c = Convert.ToInt32(s1 + s2);
if (isPerfectCube(c)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
public static void Main ()
{
int a = 6;
int b = 4;
checkCube(a, b);
}
}
|
Javascript
<script>
function isPerfectCube(x)
{
var cr = Math.round(Math.cbrt(x));
return (cr * cr * cr == x);
}
function checkCube(a , b)
{
s1 = a.toString();
s2 = b.toString();
var c = parseInt(s1 + s2);
if (isPerfectCube(c)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
var a = 6;
var b = 4;
checkCube(a, b);
</script>
|
Time Complexity: O(cbrt(concat(a,b)))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Dec, 2022
Like Article
Save Article