Friday, May 19, 2017

[Java][Answer] Array-1 > biggerTwo()

Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum. In event of a tie, return a.

biggerTwo([1, 2], [3, 4]) → [3, 4]
biggerTwo([3, 4], [1, 2]) → [3, 4]
biggerTwo([1, 1], [1, 2]) → [1, 2]

Answer 1:

public int[] biggerTwo(int[] a, int[] b) {
  if(a[0]+a[1] < b[0]+b[1]) return b;
  return a;
}

Answer 2:

public int[] biggerTwo(int[] a, int[] b) {
  if (b[1] + b[0] > a[1] + a[0])
  return b;
  else
  return a;
}

Reference

http://www.javaproblems.com/2012/12/coding-bat-java-array-1-biggertwo.html

No comments :

Post a Comment