### algorithm doesnt work on some inputs need help!

Problem:

A number 'i' is a magic number if for each digit 'j' in the number, there are exactly j occurences of it. For example, 1 is a magic number since there is 1 occurence of digit 1.Similarly 212 is also a magic number since there is 1 occurence of digit 1 and 2 occurences of digit 2. On the other hand, 12 and 123 are not magic numbers since 2 and 3 only occur once.
Given a positive integer P, your task is to decide whether P is a magic number.

Input

The first line consists of an integer N (1 <= N <= 10), the number of test cases. The next N lines contains a positive integer P consisting of M digits (1 <= M <= 100).

Output

For each test case, output YES if the input is a magic number. Otherwise, output NO.

My solution

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` `````` 1 #include 2 3 using namespace std; 4 5 bool magicnumber(int *digit, int length) 6 { 7 int i, j, count = 0; 8 9 for (i = 0; i < length; i++) 10 { 11 for (j = 0; j < length; j++) 12 { 13 if (digit[i] == digit[j]) 14 count++; 15 } 16 17 if (digit[i] == count) 18 cout << "match!" << endl; 19 20 if (digit[i] != count) 21 { 22 cout << "no match!" << endl; 23 return 0; 24 } 25 26 count = 0; 27 } 28 29 30 return 1; 31 } 32 33 34 35 36 37 int main() 38 { 39 int input, i, j = 0,number,number1,length = 0; 40 int printer[10] = {0}; 41 int digit[100] = {0}; 42 43 cin >> input; 44 45 for (i = 0; i < input; i++) 46 { 47 cin >> number; 48 number1 = number; 49 50 for(j = 0; number!= 0; j++) 51 { 52 digit[j] = number%10; 53 number/=10; 54 length++; 55 } 56 57 if (number1 == 0) 58 printer[i] = 0; 59 60 //cout << number << " " << length << " " << digit[0]; 61 62 else 63 printer[i] = magicnumber(digit,length); 64 65 length = 0; 66 } 67 68 for (i = 0; i < input; i++) 69 { 70 if (printer[i] == 1) 71 cout << "YES" << endl; 72 else 73 cout << "NO" << endl; 74 } 75 76 return 0; 77 } 78``````

certain inputs like :
1
22333444455555666666

1
22333444455555

give me the following output for both cases

no match!
NO

Can anyone see whats wrong with the function magicnumber or the main function because both of the above inputs are magic numbers and i expected the output to be like 20 lines of matches for the first one and YES and for the second one 14 lines of matches and YES?
Last edited on
If int is 32 bits wide, the maximum number it can hold is 2^31-1 = 2,147,483,647.
You should read the number as a string if you want to allow inputs of arbitrary length.
its not that, array is of a fixed since the input will definitely not exceed 100 digits as mentioned in the question. its something else. plus the inputs that dont work are 20 digits and 14 digits respectively. must be a the algorithm
Last edited on
 @james16 (15) its not that, array is of a fixed since the input will definitely not exceed 100 digits as mentioned in the question. its something else. plus the inputs that dont work are 20 digits and 14 digits respectively. must be a the algorithm

As @Athar said you are ttrying to enter an acceptable value for a variable of type int. So the input stream ( I do not check) either truncates your input or issues an error.

You can output entered value that to be sure what value is being processed.

Last edited on
thanks alot athar and vlad from moscow. exceeded integer limit. i converted it to a string and it works fine now.
Last edited on
Topic archived. No new replies allowed.