Detecting Perfect Squares with Repeated Digits
Implement a function to verify if an integer satisfies two conditions: being a perfect square and containing at least one repeated digit.
Function Signature
int check_special_square(int number);
Returns 1 if both conditions are met, otherwise 0.
Methodology
-
Perpect Square Verification
Calculate the enteger square root of the input. If squaring this root reproduces the orgiinal number, it qualifies as a perfect square. -
Digit Repetition Check
For valid perfect squares, track digit freuqencies using an array indexed 0-9. If any digit count reaches 2+, the number meets the criteria.
Solution Code
#include <math.h>
int check_special_square(int value) {
if (value < 0) return 0;
int square_root = (int)sqrt(value);
if (square_root * square_root != value)
return 0;
int digit_counts[10] = {0};
int temp = value;
while (temp) {
int current_digit = temp % 10;
digit_counts[current_digit]++;
temp /= 10;
}
for (int i = 0; i < 10; i++) {
if (digit_counts[i] >= 2)
return 1;
}
return 0;
}