Pages

Wednesday, July 5, 2017

Techgig - Flipping Bit


public class CandidateCode {
  public static int nochange_bits(String binary, int j, int k) {
    //invalid case
    if (j <= 0 || k <= 0) 
      return -1;

     //by default, the bits before the smaller index (J or K) will be always the same.
    int result = j < k ? j : k
    
    for (int i = j < k ? j : k; i < binary.length(); i++) {

      //means it will change back to original, just add to the result
      if ((i % j == 0 &&  i % k == 0) || (i % j != 0 &&  i % k != 0)){ 
        result++;
      }
    }
    return result;

  }
}

No comments :