Rajesh has been challenge by his friend to find the number which have properties like,
Here take no 12 such that
square of 12(1212)=144
and 21 which is reverse of 12 have square(2121)= 441 whose reverse is equal to 144
and now Rajesh is not getting any way how to solve the problem.So he makes a list of the number and need your help to tell whether a reverse number exist for that or not,
'T' test case and 'N' number.
050
010000
INPUT
The first line of input will be 'T' test case,follow by 'T' numbers
OUTPUT
If reverse number exist for that print that number or otherwise print No.
SAMPLE INPUT
2
12
11
SAMPLE OUTPUT
21
No
Explanation
You have to print the number if the reverse number exist otherwise print No
First number is 12 for this the reverse no is 21 whose square's reverse= square of 12
Second number is 11 for which no such number exist so print N0
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the problem. Rajesh is challenged to find numbers such that:
Take a number n. Compute its square, say s = n^2.
Now consider the reverse of n, call it rev_n. Compute its square, rev_n^2. Then reverse rev_n^2. If this reversed square equals the original square s, then rev_n is considered a "reverse number exist for that" (i.e., n has a reverse number that satisfies the property). Actually the sample: For n=12, rev=21, 21^2=441, reverse of 441 is 144 which equals 12^2=144. So indeed property holds. They output 21. So the task: given n, we need to find if there exists a number m (which should be the reverse of n? Or maybe any number? The problem statement: "Rajesh has been challenge by his friend to find the number which have properties like, Here take no 12 such that square of 12(1212)=144 and 21 which is reverse of 12 have square(2121)= 441 whose reverse is equal to 144 and now Rajesh is not getting any way how to solve the problem. So he makes a list of the number and need your help to tell whether a reverse number exist for that or not, 'T' test case and 'N' number."
Interpretation: For each given N, we need to determine whether there exists a number such that its square's reverse equals N's square. The sample shows that for N=12, the reverse number is 21 (which is the reverse of N). So it seems the candidate reverse number is specifically the reverse of N. But wait: Could there be other numbers? The problem says "whether a reverse number exist for that or not" meaning: given N, does the number formed by reversing N have the property that (rev(N))^2 reversed equals N^2? That seems to be the exact property described.
But let's verify with another example: N=11, rev=11, 11^2=121, reversed square of 11 is reverse(121)=121 which equals 121, so property holds? Actually if rev=11, then rev^2=121, reverse of that is 121, which equals original square 121. That would satisfy the condition as described: "21 which is reverse of 12 have square(21*21)= 441 whose reverse is equal to 144". That property would also hold for 11 because rev=11, rev^2=121, reverse of 121 is 121, equals 121. So why do they output "No"? Because maybe they require that the reverse number must be different from original? Or perhaps they require that the reverse number must be a number such that its square's reverse equals N's square, but not necessarily that it is the reverse of N? But then for 11, if we consider 11 itself, it would satisfy, but they output No. So maybe the condition is that the reverse number must be a different number? But the problem statement doesn't explicitly say "different". However, the example says "11 for which no such number exist so print No". That suggests that for 11, there is no number (maybe other than itself) that satisfies the property. But if we consider the reverse of 11, which is 11, it's the same number. Perhaps they don't consider it as a "reverse number" because it's not reversed? Or they might be requiring that the reverse of the number must be a number whose square's reverse is the original square, but that number must be the reverse of the original, and they want to output that reverse number if it's different? But then 11's reverse is 11, and they output "No". So maybe they require that the reverse number is not equal to the original? Possibly the phrase "reverse number exist for that" might mean there exists a number that when reversed, its square's reverse equals the original square? Hmm.
Let's parse the problem statement again: "Rajesh has been challenge by his friend to find the number which have properties like, Here take no 12 such that square of 12(1212)=144 and 21 which is reverse of 12 have square(2121)= 441 whose reverse is equal to 144 and now Rajesh is not getting any way how to solve the problem.So he makes a list of the number and need your help to tell whether a reverse number exist for that or not,". So they're essentially describing numbers like 12 that have a partner 21 with that property. So for each given N, we need to check if there exists a number M (presumably the reverse of N) such that reverse(M^2) = N^2. And output M if yes, else "No". For 11, the reverse is 11, but they say no such number exist. Perhaps they require that M != N? But the statement doesn't say "different". However, maybe they only consider numbers that when reversed produce a different number, and the property holds. But if N is a palindrome, the reverse is the same; but does it satisfy the condition? For N=11, 11^2=121, reverse(121)=121, condition holds. So technically the property holds, but they say no such number exist. That suggests that they might be looking for a different number, i.e., they want to output the reverse number if it is a valid partner (maybe distinct), but if the reverse is the same number, they consider that as not existing? Or maybe they consider that the "reverse number" should be the reverse of N, and they output that number if the condition holds; but for 11, they would output 11? But they output "No". So maybe they want to output the number if the condition holds and the reversed number is different from the original. But then for N=12, reverse is 21, which is different, so they output 21. For N=21, what would be the output? If input 21, reverse is 12, and condition: 12^2=144, rev(12^2?) Actually we need to check: For N=21, compute rev=12, 12^2=144, reverse of 144 is 441? That doesn't equal 21^2=441? Wait check: 21^2=441, reverse(12^2)= reverse(144)=441, so it holds. So 21 also has reverse number 12. So both directions work. So for a pair like (12,21), both have reverse partners. But if N is a palindrome like 11, the condition holds (since rev=11, rev^2=121, reverse(121)=121, equals 121) but they say "no such number". So perhaps the problem expects to output the reverse number only if it is not equal to N, otherwise "No". But the statement doesn't explicitly say that, but maybe it's implied by "reverse number exist for that" meaning a distinct reverse number. Could also be that they define "reverse number" as the number you get when you reverse digits, and they ask "whether a reverse number exist for that or not" meaning: does the reverse of that number (the digits reversed) satisfy the property? And if yes, print the reverse number; else print No. So for 11, the reverse is 11; it satisfies the property, so they would print 11. But they printed No. So that's not it.
Alternate possibility: Maybe they require that the reverse number's square's reverse exactly equals the original square, but that is a condition that is not automatically true for palindromes because reversing a square might not be the same as the square itself unless the square is also a palindrome. For 11, square 121 is palindrome, so condition holds. But maybe they require that the reverse number is the reverse of the original, but that the reversed square of that is the original square. That holds for 11. So why No? Could be that the problem expects to check if there exists any number such that its square's reverse equals N's square, and then output the smallest such number? Or specifically the number that is the reverse of N? However the sample only shows one case.
Let's think about possible misinterpretation: The problem might be: Given N, find if there exists a number M (not necessarily the reverse of N) such that when you reverse M, you get N? Or something like that? But they said "reverse number exist for that" with example 12 and 21. So it's clearly about the property of squares.
Maybe the condition is: N and its reverse R satisfy: (N^2) reversed equals R^2? Let's test: For N=12, N^2=144, reverse is 441, and R^2=441, so yes. For N=11, N^2=121, reverse is 121, and R^2=121, so that would also be true. So if condition is N^2 reverse equals R^2, then it holds for 11 as well. So they'd output 11, not No.
What if condition is: R^2 reversed equals N^2? That's symmetric: For N=12, R^2=441, reverse=144=N^2, holds. For N=11, R^2=121, reverse=121=N^2, holds. So again holds.
So why No? Maybe because they require that R is different from N, i.e., they want a nontrivial reverse. The problem says "reverse number exist for that" - could be interpreted as "whether there exists a number which is the reverse of the given number and also satisfies the square reverse property". For 11, the reverse is 11, so that number exists, but maybe they consider that the reverse number is not considered because it's the same? Or maybe they treat it as "reverse number" meaning a number that is the reverse of the given number but different? The example: "Here take no 12 such that square of 12(1212)=144 and 21 which is reverse of 12 have square(2121)= 441 whose reverse is equal to 144". They talk about 21 being the reverse of 12. For 11, the reverse is 11; but it's the same number, not a distinct "reverse number". However, they'd still say "reverse of 11 is 11". But maybe they consider that trivial and not interesting, so they output No.
Alternatively, perhaps the problem is to output the number (the reverse) if the condition holds AND the reverse number is an integer with no leading zeros? But 11's reverse is 11, fine.
Wait, let's check the sample input and output carefully:
SAMPLE INPUT
2
12
11
SAMPLE OUTPUT
21
No
Thus for 12 -> 21, for 11 -> No.
If we simply implement: For each N, compute R = reverse(N). If R != N and reverse(R^2) == N^2, then output R else "No". This would yield: For 12, R=21 (different), check: reverse(21^2)=reverse(441)=144, equals 144 -> true, output 21. For 11, R=11 (same), so condition R != N fails, so output No. That matches sample. So maybe that's the intended logic: they want a distinct reverse number (the reverse of N) that satisfies the property. But does the problem statement mention that the reverse number must be different? It does not explicitly, but the sample suggests it.
We should consider other test cases to see if this rule holds universally. What about N=10? Reverse is 01 which is 1. But typically, when reversing digits, leading zeros are dropped? Usually reverse of 10 is 1 (since 01 -> 1). Then check: N=10, N^2=100. R=1, R^2=1, reverse(1) is 1, which is not 100. So fails. Output No. But if we treat reverse with leading zeros? If we keep the same number of digits? For example, reverse of 10 as "01" is considered 1? Typically in such problems, reversal is done on the decimal representation without leading zeros; the reversed number is formed by writing digits in reverse order, and then interpreting that as a number (so leading zeros disappear). So R=1. So fails.
What about N=21? Already considered, R=12, different, condition holds. So if input 21, output would be 12.
What about N=121? Reverse is 121 (palindrome). So output No. Does condition hold? N^2=14641, R^2=14641, reverse of that is 14641, equal, but since R=N, output No. So fine.
What about N=13? Reverse=31, 13^2=169, 31^2=961, reverse(961)=169, holds! Indeed 13 and 31 are a known pair. So output 31.
N=122? Reverse=221, 122^2=14884, 221^2=48841, reverse(48841)=14884? 48841 reversed is 14884? Let's check: 48841 -> reverse digits: 14884, yes! So holds. Output 221. So this property is known: numbers such that n^2 reversed equals (reverse(n))^2. They are called reverse square numbers or something. There are many.
So it's plausible the problem is exactly that: for each N, if reverse(N) is different from N and reverse(reverse(N)^2) == N^2, print reverse(N), else print "No".
But check the condition: they said "and 21 which is reverse of 12 have square(21*21)= 441 whose reverse is equal to 144". That's exactly reverse(R^2) = N^2. So condition is reverse(R^2) == N^2. So we need to check that.
Thus algorithm: For each test case:
Read integer N (0<N<10000? Actually constraints: 010000, meaning N between 0 and 10000? Probably 0 < N < 10000, but N can be up to 10000 inclusive? Usually notation 010000 is ambiguous. Likely N is a positive integer less than 10000. Also T up to 50.
Compute rev = reverse(N). We'll define a function to reverse digits: convert to string, reverse, convert back to int. This drops leading zeros.
If rev == N: print "No"
Else: compute n_sq = N*N.
Compute rev_sq = rev*rev.
Compute rev_rev_sq = reverse(rev_sq).
If rev_rev_sq == n_sq: print rev
Else: print "No"
Check edge cases: N with trailing zeros? e.g., N=120. Reverse would be 21 (since 021 -> 21). Then check: 120^2=14400, rev=21, rev^2=441, reverse(441)=144, which is not 14400. So fails.
Now, could there be a scenario where reverse(N) is different and condition holds, but the reversed number might have leading zeros? For example, N=101? Reverse=101, palindrome. So not.
If N=10? Already fails.
Thus algorithm is straightforward.
But we need to confirm if the problem might require to output the reverse number only if it exists, even if it equals N? The sample says No for 11, which is palindrome, so likely they exclude equal.
Also, check if there might be cases where N is such that reverse(N) works but N itself might also be the reverse of something else? Not relevant.
Thus solution: for each N, compute rev = reverse(N). If rev != N and reverse(revrev) == NN, output rev, else "No".
Complexities: trivial.
Now, note: Constraints: N up to 10000, so squares up to 100 million, easily fit in 32-bit int (max 10000^2=100,000,000). Reverse of squares may be up to 8 digits? Actually max rev_sq when N=9999? rev of 9999 is 9999, rev_sq=9999^2=99,980,001; reverse could be up to 100,089,999? Anyway fits in 32-bit? 99,980,001 reversed is 100,089,999? Actually reversing digits: 99,980,001 reversed is 100,089,99? Wait digits: 99,980,001 is 8 digits? Actually 99,980,001 has 8 digits? 100,000,000 is 9 digits, 99,980,001 is 8? Let's compute: 9999^2 = 99,980,001, that's 8 digits. Reverse of that could be up to 100,089,999? That's 9 digits? Example: 99,980,001 reversed: digits: 1,0,0,0,8,9,9,9? Actually let's reverse the string: "99980001" reversed is "10008999". That's 8 digits? "10008999" is 8 digits? Actually it's 8 digits? 10008999 is 8 digits? 10,008,999 is 8 digits? 10 million is 8 digits? 10,008,999 is 8 digits (since 10 million is 8 digits? Wait: 10,000,000 has 8 digits, yes). So it's within 100 million, so safe for 32-bit int (max 2,147,483,647). So fine.
Implementation details: Need to handle up to 50 test cases.
Potential pitfalls: Some numbers might have squares that when reversed produce numbers with leading zeros? For example, N=12 gave 144, reverse 441. For numbers like N=10? rev_sq=1, reverse(1)=1. But we need to treat reverse of integer as integer without leading zeros. So reverse(100) would be 1? Typically reverse(100) as integer: digits "100" reversed "001" -> 1. That's fine.
Now, check if there's any case where rev_sq is like 400? Reverse(400) = 4? Actually digits "400" reversed "004" -> 4. That's fine.
Thus algorithm: Write a function rev_int(x) that returns the integer formed by reversing the decimal digits of x, dropping leading zeros.
Now, confirm with some known pairs: (12,21), (13,31), (102,201?) Let's test: 102^2=10404, rev=201, rev^2=40401, reverse(40401)=10404, holds. So output 201. (103,301)? 103^2=10609, 301^2=90601, reverse=10609, holds. So yes. (112,211)? 112^2=12544, 211^2=44521, reverse=12544, holds. So there are many.
Thus answer is straightforward.
Now, about output format: print each answer on separate line.
Edge Cases: N=0? Constraint says 0<N, so N positive.
Now, we need to output the number if exists, else "No". Note that "No" with capital N and small o, exactly as sample.
Thus solution.
But let's read problem statement again: "Here take no 12 such that
square of 12(1212)=144
and 21 which is reverse of 12 have square(2121)= 441 whose reverse is equal to 144
and now Rajesh is not getting any way how to solve the problem.So he makes a list of the number and need your help to tell whether a reverse number exist for that or not,
'T' test case and 'N' number.
050
010000
INPUT
The first line of input will be 'T' test case,follow by 'T' numbers
OUTPUT
If reverse number exist for that print that number or otherwise print No."
Actually they say "If reverse number exist for that print that number or otherwise print No." That could be interpreted as: if there exists any number (maybe not necessarily the reverse of N) that satisfies the property, then print that number; else print No. But they don't specify which number to print if multiple. Usually such problems print the reverse number (the partner). In the example, they print 21 for 12. So it's the reverse of N. So likely the intended partner is the reverse of N.
But why would they say "whether a reverse number exist for that or not"? Possibly they mean: does the reverse of the number (i.e., the number formed by reversing its digits) satisfy the property? If yes, print that reversed number; else print No. For 11, the reverse is 11, but they print No. That contradicts. Unless they have an additional condition that the reversed number must be different, maybe implied by "reverse number" meaning a different number. But they didn't state it.
Maybe the condition is that reverse of square of reverse of N should equal square of N, but also the reverse of N should be a number with the same number of digits? For 11, reverse is 11, which is the same, but maybe they consider it doesn't count as "reverse number" because it's not actually reversed? The phrase "reverse number" might mean "the number which is the reverse of the given number and is distinct". But it's ambiguous.
To be safe, we should analyze the problem deeper. There is known problem on CodeChef or similar: "Find Reverse Number" or "Reverse of a Square". Possibly the known problem is: Given a number, if the reverse of the number squared equals the reverse of the square of the number? Actually there is known property: Some numbers satisfy: (reverse of n)^2 = reverse (n^2). That's a different property. For 12: reverse(12)=21, 21^2=441, reverse(12^2)=reverse(144)=441, so that property holds: reverse(n)^2 == reverse(n^2). For 11: reverse(11)=11, reverse(121)=121, so 11^2=121, reverse(11^2)=121, and reverse(11)^2=121, so holds as well. So if the property is reverse(n)^2 == reverse(n^2), then 11 would satisfy. But sample says No for 11. So they aren't using that property. They are using reverse(reverse(n)^2) == n^2. That's different.
But check: For 12, reverse(21^2)=144, which is n^2. That's what they have. So property is reverse( (reverse(n))^2 ) == n^2.
Now, is it possible that for 11, reverse( (reverse(11))^2 ) = reverse(11^2) = reverse(121)=121, which equals 11^2=121. So it holds. So 11 should satisfy. Yet they say No. So maybe the condition includes that the reverse number must have the same number of digits? For 11, reverse(11) is 11, which is the same number, but maybe they consider that not a "reverse number" because it's not truly reversed? Or perhaps they consider that the reverse number should be distinct and also should be an integer without leading zeros, and maybe they also require that the reverse of the original number is not equal to original? It is common in such puzzles that they are looking for numbers where the square of the reverse is the reverse of the square, but often they consider only numbers where reverse is different. So they might be asking: Given a number, check if its reverse (as a distinct number) satisfies that property. If the number is a palindrome, then the reverse is the same, and while the property holds, it's trivial and they might ignore it.
But the problem statement doesn't mention "distinct", but the sample forces us to treat palindromes as No.
Given typical problem statements on platforms like HackerEarth, CodeChef, etc., sometimes they are poorly worded, and examples clarify. So we should go with sample.
Thus we implement as: For each N, compute rev = reverse(N). If rev != N and reverse(revrev) == NN, print rev; else print "No".
I'll also check with some other values to ensure there aren't any corner cases like rev having leading zeros. For N=1001? Reverse is 1001? Actually 1001 reversed is 1001 (palindrome). So No. N=1200? Reverse is 21? Actually 1200 reversed is "0021" -> 21. Check: N^2=1,440,000? 1200^2=1,440,000. rev=21, rev^2=441, reverse(441)=144, not equal. So No.
Another: N=1020? Reverse = 201? Actually 1020 reverse is "0201" -> 201. Then N^2=1,040,400? 1020^2=1,040,400. rev^2=40401, reverse=10404, not equal. So fails.
Thus it's fine.
Now, check if rev != N condition should be required even if condition holds? For N=1? 1 is palindrome, condition holds? 1^2=1, rev=1, rev^2=1, reverse(1)=1, equals 1. So if we didn't enforce rev != N, we would output 1. But what would sample expect for N=1? Probably No, because it's palindrome. But not given. We'll assume rev != N required.
Thus final solution.
But maybe there is another interpretation: They might want to output the number for which the property holds; that number could be the original number's reverse, but if that reverse is the same as original, they'd output the number itself? But they output No for 11. So they don't.
Thus solution stands.
Now, let's think about possibility of overflow? N up to 10000, squares up to 100 million, reversed squares up to maybe 9 digits (since max square 99,980,001 reversed is 100,089,999, which is ~100 million). All fit in 32-bit signed int (max 2.147e9). So fine.
Implementation in C++: read T, loop, read N, compute rev and squares, compare.
But there is also constraint: 050 means T between 0 and 50? Actually 050 is weird; likely 0 < T <= 50. So T up to 50.
Alright.
Now, I'll craft the explanation accordingly.
Before finalizing, I'd like to double-check if there's any nuance: The problem says "If reverse number exist for that print that number or otherwise print No." That "that number" could refer to the reverse number, not the original. So for 12, reverse number is 21, print 21. For 11, reverse number exists? It is 11, but maybe they don't consider it because it's not a "reverse number" (i.e., the number obtained by reversing digits) if it's the same? But technically it is still the number you get by reversing digits. However, they might think that "reverse number" should be something else? I'm not sure.
Alternatively, perhaps the condition is that the reverse number's square's reverse equals the original number's square, AND the reverse number must be a different integer. Many math puzzle problems about "reverse and square" often look for pairs like 12-21. Palindromes are considered trivial and not counted. So it's plausible.
Thus I'll implement with the rev != N check.
Let's also test with N=144? Not needed.
But there is also possibility: The "reverse number" could be any number (not necessarily the reverse of N) that satisfies the property. In that case, for N=11, we could try to find some M such that reverse(M^2) = 121. There might be many numbers M such that reverse(M^2)=121. For M=11, it works. So if we are allowed to output any M, we'd output 11. But they output No, so they don't accept that. So they want specifically the reverse of N.
Thus answer.
Now, let's think about input: "The first line of input will be 'T' test case,follow by 'T' numbers". So we read T then T lines each containing N.
We'll produce output each on separate line.
Edge: There could be whitespace.
Now, we must be careful: When reversing, we need to reverse the decimal digits without leading zeros. For N, we can do string reversal and then stoi (or atoi) which will drop leading zeros automatically. For the square reversal, we also need to convert to string, reverse, then convert back.
We'll define a function:
int rev(int x) {
int res = 0;
while (x > 0) {
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
But careful: This method will drop trailing zeros of x (which become leading zeros in reversed number, but are omitted because we don't add zeros). For example, rev(1200) will be: starting 0, then process digits: 0,0,2,1? Actually algorithm: res=0; x=1200. x%10=0 => res=0; x=120; x%10=0 => res=0; x=12; x%10=2 => res=2; x=1; x%10=1 => res=21; x=0; end. So rev=21. That's correct interpretation of reverse number as integer (leading zeros dropped). That's consistent.
Similarly, rev(100) gives 1. Good.
Now for rev_sq reversal, we can use the same function rev().
Thus algorithm.
One more nuance: Should we consider N up to 10000 inclusive? The constraint says "010000". That's ambiguous but likely 0 < N <= 10000 or N between 0 and 10000 exclusive? Possibly N less than 10000 (maybe up to 9999). We'll just handle up to 10000.
Now, test with known pair: N=122, rev=221, rev_sq=48841, rev(rev_sq)=14884, N^2=14884, matches. Output 221.
N=113? 113^2=12769, rev=311, 311^2=96721, rev=12769? 96721 reversed is 12769? Actually 96721 reversed: "12769", yes! So holds. Output 311.
Thus many.
Now, what about N=1? rev=1, rev_sq=1, rev_rev_sq=1, but rev==N, so output No. That would be correct if palindromes not counted.
Now, maybe they want to output No even if rev != N but condition fails. So all good.
Thus final code:
#include
using namespace std;
int rev_int(int x) {
int r = 0;
while (x) {
r = r * 10 + x % 10;
x /= 10;
}
return r;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
int R = rev_int(N);
if (R == N) {
cout << "No" << endl;
continue;
}
int N_sq = N * N;
int R_sq = R * R;
int rev_R_sq = rev_int(R_sq);
if (rev_R_sq == N_sq) {
cout << R << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
This should solve.
But we need to be careful: The rev_int function for x=0 would return 0, but x will never be 0 because N>0. However, could rev_R_sq be 0? If R_sq = 0? Not possible because R>0.
Thus fine.
Now, I'll produce final answer accordingly.
But I should also consider that some numbers might have squares that when reversed have leading zeros and rev_int would drop them. That's consistent with problem's notion of reversing numbers as integer values.
Thus answer is done.
Now, I'll write final response with explanation and code. ```cpp
#include
using namespace std;
// Function to reverse the decimal digits of an integer (leading zeros are dropped)
int rev_int(int x) {
int r = 0;
while (x > 0) {
r = r * 10 + x % 10;
x /= 10;
}
return r;
}
int main() {
int T;
cin >> T; // number of test cases
code
while (T--) {
int N;
cin >> N; // given number
int R = rev_int(N); // its digit reversal
// If the reversed number is the same as original, we consider it as "No"
if (R == N) {
cout << "No\n";
continue;
}
int N_sq = N * N; // N^2
int R_sq = R * R; // R^2
int rev_R_sq = rev_int(R_sq); // reverse of R^2
if (rev_R_sq == N_sq) {
cout << R << "\n"; // valid reverse number exists
} else {
cout << "No\n";
}
}
return 0;