Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

To find the maximum, what's the standard pattern?

<code>int max = ?; for (int i = 0; i < arr.length; i++) if (arr[i] > max) max = arr[i]; </code></pre>

  • A

    max = -1;

  • B

    max = arr[0]; (start from i=1)

    check_circle
  • C

    max = 0;

  • D

    max = Integer.MAX_VALUE;

Explanation

Initialize to first element; then iterate from i=1 (or all and skip first compare).

Want 10 more like this — adaptive to your weak spots?

Related questions