Showing posts with label maximum. Show all posts
Showing posts with label maximum. Show all posts

Java > Array-1 > maxTriple (CodingBat Solution)

Problem:

Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.

maxTriple({1, 2, 3}) → 3
maxTriple({1, 5, 3}) → 5
maxTriple({5, 2, 3}) → 5


Solution:

public int maxTriple(int[] nums) {
  int result = 0;
  int a = nums[0];
  int b= nums[((nums.length+1)/2) -1;
  int c = nums[nums.length -1];
  
  if (a>b && a>c)
  result = a;
    if (b>a && b>c)
  result = b;
    if (c>a && c>b)
  result = c;
  return result;
  }
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact