Digitally balanced numbers | Java implementation to check if a number is a digitally balanced number ; Function to check if the digits in the number is the same number of digits ; Loop to iterate over the digits of the number N ; Loop to iterate over the map ; Driver Code ; function to check
import java . util . * ; class GFG { static boolean checkSame ( int n , int b ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; while ( n != 0 ) { int r = n % b ; n = n / b ; if ( m . containsKey ( r ) ) { m . put ( r , m . get ( r ) + 1 ) ; } else { m . put ( r , 1 ) ; } } int last = - 1 ; for ( Map . Entry < Integer , Integer > i : m . entrySet ( ) ) { if ( last != - 1 && i . getValue ( ) != last ) { return false ; } else { last = i . getValue ( ) ; } } return true ; } public static void main ( String [ ] args ) { int n = 9 ; int base = 2 ; if ( checkSame ( n , base ) ) System . out . print ( " Yes " ) ; else System . out . print ( " NO " ) ; } }