title: Count Total Set Bits In All Numbers From 1 To N
signature: def countTotalSetBitsInAllNumbersFrom1ToN(n: Int) = {
test_code: import scala.collection.mutable.HashMap
  import scala.collection.immutable.HashSet
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  
  // TESTING CODE 
  // ============================================
  def validateSolution(actual: Int , expected: Int): Boolean = {
      actual.toString == expected.toString
  }
  
  def driver(n: Int, expected: Int): String = {
      try {
          if (validateSolution(PLACEHOLDER_FN_NAME(n),expected)) {
              "PASSED"
          } else{
              "FAILED"
          }
      }catch{
          case e: Exception => {e.getClass.getName}
      }
  
  }
  object QuestionEvaluator extends App {
  
      println(s"TEST-0...${driver(
        90, 
        279    
      )}")   
  
  
      println(s"TEST-1...${driver(
        56, 
        159    
      )}")   
  
  
      println(s"TEST-2...${driver(
        43, 
        112    
      )}")   
  
  
      println(s"TEST-3...${driver(
        31, 
        80    
      )}")   
  
  
      println(s"TEST-4...${driver(
        77, 
        231    
      )}")   
  
  
      println(s"TEST-5...${driver(
        35, 
        88    
      )}")   
  
  
      println(s"TEST-6...${driver(
        43, 
        112    
      )}")   
  
  
      println(s"TEST-7...${driver(
        66, 
        197    
      )}")   
  
  
      println(s"TEST-8...${driver(
        15, 
        32    
      )}")   
  
  
      println(s"TEST-9...${driver(
        95, 
        304    
      )}")   
  
  }
  
source_py: def count_total_set_bits_in_all_numbers_from_1_to_n(n):
      i = 0
      ans = 0
      while ((1 << i) <= n):
          k = 0
          change = (1 << i)
          for j in range(0, (n + 1)):
              ans += k
              if (change == 1):
                  k = (not k)
                  change = (1 << i)
              else:
                  change -= 1
          i += 1
      return ans
source_cpp: int count_total_set_bits_in_all_numbers_from_1_to_n(int n) {
    int i = 0;
    int ans = 0;
    while ( ( 1 << i ) <= n ) {
      bool k = 0;
      int change = 1 << i;
      for ( int j = 0;
      j <= n;
      j ++ ) {
        ans += k;
        if ( change == 1 ) {
          k = ! k;
          change = 1 << i;
        }
        else {
          change --;
        }
      }
      i ++;
    }
    return ans;
  }