Confused about the getID function in editorial solution


  • 0
    D

    As per the comment

    // Get the ID of the bucket from element value x and bucket width w
    // In Java, `-3 / 5 = 0` and but we need `-3 / 5 = -1`.
    

    So test the getID, I ran the following on Java 8

    public class NegativeNumberDivision {
        public static void main(String[] args) {
            for (int i=-1; i > -10; i--) {
                System.out.println(String.format("%d/%d = %d", i, 3, (i + 1)/ (3 - 1)));
                // System.out.println(String.format("%d/%d = %d", i, 3, (i / 3)));
            }
            return;
        }
    }
    

    Both the println calls generate the following output

      -1/3 = 0
      -2/3 = 0
      -3/3 = -1
      -4/3 = -1
      -5/3 = -2
      -6/3 = -2
      -7/3 = -3
      -8/3 = -3
      -9/3 = -4
    

    Am I missing something?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.