Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created September 5, 2019 14:17
Show Gist options
  • Save wszdwp/7e0a0cdcca48c979a2e5d7c7c2bfe76a to your computer and use it in GitHub Desktop.
Save wszdwp/7e0a0cdcca48c979a2e5d7c7c2bfe76a to your computer and use it in GitHub Desktop.
Some code tricks
int[] arr = {5, 3, 5, 7, 8, 9, 9, 10};
int[] res = new int[2];   //res[0] sum of even idx elems in arr, res[1] sum of odd idx elems
...
// normal way
if (i % 2 == 0) {
   res[0] += arr[i];
} else {
   res[1] += arr[i];
}

// tricky way
res[i % 2] += arr[i];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment