We need to check whether the occurrence of an individual digit in a number is same or not.For e.g. for 2244 (2 occur 2 times and 4 occur 2 times).Therefore, occurrence of both digits are same.
//This will return true if occurrence of individual digit in //a number is same else false bool stable(int no) { vector<int> v1; int k , count = 1; int arr[10]; //Initializing all arr[] -> 0 for(int k = 0 ; k < 10 ;k++) { arr[k] = 0; } while(no != 0) { k=no%10; arr[k]++; no=no/10; } for(int i = 0 ; i < 10 ; i++) { if(arr[i] != 0) { v1.push_back(arr[i]); //storing count of individual digits } } vector<int>::iterator it , it2; for(it = v1.begin()+1 ,it2 = v1.begin(); it != v1.end() ; it++,it2++) { if(*it == *it2) //if all the values are same return true else false { count++; } } if(count == v1.size()) return true; return false; } But this code doesn't work for no like 2222,1111,444. Also, would you suggest some good way to optimize the code?
2222, as 2222 is stable (2 is the only digit with occurrence 4), your code seems right.