Asked  7 Months ago    Answers:  5   Viewed   31 times

It's trivial to write a function to determine the min/max value in an array, such as:

 * @param chars
 * @return the max value in the array of chars
private static int maxValue(char[] chars) {
    int max = chars[0];
    for (int ktr = 0; ktr < chars.length; ktr++) {
        if (chars[ktr] > max) {
            max = chars[ktr];
    return max;

but isn't this already done somewhere?



Using Commons Lang (to convert) + Collections (to min/max)

import java.util.Arrays;
import java.util.Collections;

import org.apache.commons.lang.ArrayUtils;

public class MinMaxValue {

    public static void main(String[] args) {
        char[] a = {'3', '5', '1', '4', '2'};

        List b = Arrays.asList(ArrayUtils.toObject(a));


Note that Arrays.asList() wraps the underlying array, so it should not be too memory intensive and it should not perform a copy on the elements of the array.

Tuesday, June 1, 2021
answered 7 Months ago

It's printing out a number every time it finds one that is higher than the current max (which happens to occur three times in your case.) Move the print outside of the for loop and you should be good.

for (int counter = 1; counter < decMax.length; counter++)
     if (decMax[counter] > max)
      max = decMax[counter];

System.out.println("The highest maximum for the December is: " + max);
Wednesday, June 2, 2021
answered 7 Months ago

You can use filter to create a new array with elements that have an even index.

Just remember that the words in "odd" places are actually located at even indexes, since arrays in javascript are zero-indexed.

var s = 'Hello nice to meet you'

var evenWords = (s) => s.split(' ').filter((element, index) => index % 2 === 0).join(' ');

Friday, August 27, 2021
answered 3 Months ago

Here. 1. You use j<= animalNames.length;?

  1. You compare animalNames[j + 1]? -> error index out of array

  2. and you return in the first if condition return (animalNames[j]); -> wrong value

Ok, let me make clear. You find the longest string in an array. You loop over the array then you compare 2 near elements, and then return the bigger one. With your code, it will return the rabbit. Right?

May be you confuse about the process flow. There is a simple way.

  1. You assign a variable for the length of the first array element: elementLength = array[0].length; and a value to keep track of the index
  2. You loop over the array You check every element with this variable, if bigger then re-assign the element value and update the index.
  3. End of the loop. you have the biggest length and the index


int index = 0; 
int elementLength = array[0].length();
for(int i=1; i< array.length(); i++) {
    if(array[i].length() > elementLength) {
        index = i; elementLength = array[i].length();
return array[index];

that is it.

Wednesday, September 22, 2021
Andras Zoltan
answered 3 Months ago

Sort the segments by starting time.

Create a stack which will store the merged intervals.

Add the first element of the sorted array to the stack, then for each element in the array, compare it to the element at the top of the stack

If the start time is greater than the end time of the element at the top of the stack, add the interval to the stack.

If the start time is smaller than the end time of the element at the top of the stack, update the end time of the element at the top of the stack to match the end time of the new element.

When the whole array is processed the resulting stack should contain the merged intervals.

Implementation on java:

 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
public class Solution {
    public List<Interval> merge(List<Interval> intervals) {
        Deque<Interval> stack = new ArrayDeque<Interval>();

        Collections.sort(intervals, new Comparator<Interval>() {
                public int compare(Interval p1, Interval p2) {
                    return, p2.start);

        if (intervals.size() < 1) {
            return intervals;

        for (int j = 1; j < intervals.size(); j++) {
            Interval i = intervals.get(j);
            Interval top  = stack.peek();
            if (top.end < i. start) {
            else if (top.end < i.end) {
                top.end = i.end;
        return new ArrayList<Interval>(stack);

Thursday, September 30, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :