text: Sum of Hamming difference of consecutive numbers from 0 to N | Set 2 | Java program to implement the above approach ; Function to calculate and return the hamming distance between all consecutive numbers from 0 to N ; Driver code
code: import java . util . * ; class GFG { static int TotalHammingDistance ( int n ) { int i = 1 , sum = 0 ; while ( n / i > 0 ) { sum = sum + n / i ; i = i * 2 ; } return sum ; } public static void main ( String [ ] args ) { int N = 9 ; System . out . println ( TotalHammingDistance ( N ) ) ; } }