2

i'm writing a function that retrieves the maximum of each row in a 2d array, and returns a 1d array, where each index is relative to the 2d array's column row index.

For example, if I have an 2d array:

{1,2,3}
{4,5,6}
{7,8,9}

it should return an array of

{3,6,9}

here is my code so far:

double[] rowMaxes(double[][] nums) {
    double [] count = new double [nums.length];
    for(int i = 0; i < count.length; i++){
        for(int x = 0; x < nums[0].length; x++){
            for(int n = 0; n < nums.length; n++){
                if(nums[x][n] > count[i]){
                    count[i] = nums[x][n];
                }
            }
        }
    }
    return count;
}
1
  • Please refer to my Java 8 one liner solution Commented Aug 19, 2017 at 2:08

6 Answers 6

1

There's no need for 3 nested loops. You only need two loops :

for(int i = 0; i < count.length; i++){
    for(int x = 0; x < nums[0].length; x++){
        if(nums[i][x] > count[i]){
            count[i] = nums[i][x];
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

the problem is that this doesn't work with negative numbers that are the max of the 2d array; only for positives
@RandyHuang If your array might contain negative numbers, you should initialize all the elements of the count array to Integer.MIN_VALUE
@RandyHuang it missing count[i] = nums[i][0]; in the first loop
0

You should find row and column length before going in loop. If you want to consider negative numbers then first define max as minimum negative value. You can use this

public static void main(String[] args) {
        double count[][] = {{1,2,3,8},{4,6,5,9},{0,8,9,1}};
        int r = count.length;
        int c= count[0].length;
        double out[] = new double[r];
        for(int i = 0; i < r; i++){
            double max = Integer.MIN_VALUE;
            for(int x = 0; x < c; x++){
                if(count[i][x] > max)
                    max = count[i][x];
            }
            out[i] = max;
        }
        for(int i=0;i<r;i++)
            System.out.println(out[i]);

    } 

Comments

0
public static int[] getMaxOfRow(int arr[][]){
    int grtr[]=new int[arr.length];
    grtr[0]=arr[0][0];
    for(int i=0;i<arr.length;i++){
        for(int j=0;j<arr[0].length;j++){
            if(arr[i][j]>grtr[i]){
                grtr[i]=arr[i][j];
            }
        }
    }
    return grtr;
}                                                                                                

1 Comment

welcome to stackflow, we strongly suggest you to read the guide (here)[stackoverflow.com/help/how-to-answer] about how to answer question.
0

Be aware that your code will not work if all values in a row are less than zero.
When you create a new array it will be filled with the default value - it is zero.
Because it you need to add in the first loop count[i] = nums[i][0].

Something like this

double[] rowMaxes(double[][] nums) {
    double[] count = new double[nums.length];

    for (int i = 0; i < nums.length; i++) {
        count[i] = nums[i][0];
        for (int j = 1; j < nums[i].length; j++) {
            if (count[i] < nums[i][j]) {
                count[i] = nums[i][j];
            }
        }
    }

    return count;
}

If you use Java 8, you can replace inner loop with stream and max method.

for (int i = 0; i < nums.length; i++) {
    count[i] = Arrays.stream(nums[i]).max().getAsDouble();
}

Comments

0

If you prefer to make it one line, this is the solution with any explicit looping:

Arrays.stream(nums).mapToInt((row) -> Arrays.stream(row).max().getAsInt()).max().getAsInt()

Comments

0
package com.vikas.array;

public class ArrayGreater_2ndApproach {

    public static void main(String[] args) {

        int d[][] = { { 1, 5, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        for (int i = 0; i < d.length; i++) {
            int max = 0;
            for (int j = 0; j < d.length; j++) {
                if(max<d[i][j]) {
                    max = d[i][j] ;

                }
            }
            
            System.out.println("Max number in the row : " + max);
        }

    }
}

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Please read the questions carefully. The question was about how to get the maximum per row. Also, add a comment of your solution to share your thoughts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.