-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mystery_number.c
66 lines (58 loc) · 1.34 KB
/
mystery_number.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// C program to Check if a number is a Mystery Number or Not.
/*
A mystery number is that number which can be expressed as a sum of two
numbers and those two numbers must be the reverse of each other
*/
#include <stdio.h>
#include <stdbool.h>
// Helper function to reverse a number.
int reverse(int n)
{
int rev = 0;
for (; n > 0; n /= 10)
{
rev = rev * 10;
rev = rev + (n % 10);
}
return rev;
}
bool check_mystery_num(int n)
{
/* Let us iterate from 1 upto n/2, to check if there is any pair, such that thier
sum equals the given number and check if the pair are reverse of each other */
for (int i = 1; i <= n / 2; i++)
{
if (reverse(i) == n - i)
{
return true;
}
}
return false;
}
int main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);
bool res = check_mystery_num(n);
if (res)
{
printf("The given number %d is a Mystery Number.", n);
}
else
{
printf("The given number %d is not a Mystery Number.", n);
}
return 0;
}
/*
Time Complexity: O(n*log(n)), where 'n' is the given number
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE 1
Enter the number: 43234
The given number 43234 is a Mystery Number.
SAMPLE 2
Enter the number: 1234
The given number 1234 is not a Mystery Number.
*/