-1

Sort multidimensional array based on the difference in the value, if the value is the same; sort on the first column.
Java Solution needed. If not code, I would like to know the approach or any kind of hint to solve this type of problem.

Constrains : No of rows can be any but fixed no of column ie 2.

Example: 
int arr[][] = new int[5][2]

    [0] [1]
[0]  0   6
[1]  0   7
[2]  4   5
[3]  2   3
[4]  0   1

Final Output:

    [0] [1]
[0]  0   1
[1]  2   3
[2]  4   5
[3]  0   6
[4]  0   7

Explanation:

difference between: arr[0][1] - arr[0][0]  -> 6-0 -> 6
difference between: arr[1][1] - arr[1][0]  -> 7-0 -> 7
difference between: arr[2][1] - arr[2][0]  -> 5-4 -> 1    — same length ie 1
difference between: arr[3][1] - arr[3][0]  -> 3-2 -> 1  —  same length ie 1
difference between: arr[4][1] - arr[4[0]  -> 1-0 -> 1   — same length ie 1

I want to sort on the difference, in cases where difference is same I want to sort those with same difference on column [0]

So in this case, 
The below 3 have same difference
difference between: arr[2][1] - arr[2][0]  -> 5-4 -> 1    — same difference ie 1
difference between: arr[3][1] - arr[3][0]  -> 3-2 -> 1  —  same difference ie 1
difference between: arr[4][1] - arr[4[0]  -> 1-0 -> 1   — same difference ie 1

Need to sort the above 3 based on there column[0] value
arr[2][1] - arr[2][0]  -> 5-4 -> 1    —  value here is 4 ie arr[2][0] 
arr[3][1] - arr[3][0]  -> 3-2 -> 1  — value here is 2 ie arr[3][0]
arr[4][1] - arr[4[0]  -> 1-0 -> 1   — value here is 1 ie arr[4][0]

So the the one with the least value in column[0] should be first,
In final output, 
arr[4][1] - arr[4[0]  -> 1-0 -> 1   ——> 1st
arr[3][1] - arr[3][0]  -> 3-2 -> 1  ——> 2nd
arr[4][1] - arr[4[0]  -> 1-0 -> 1   ——> 3rd 
arr[0][1] - arr[0][0]  -> 6-0 -> 6  ——> 4th
arr[1][1] - arr[1][0]  -> 7-0 -> 7  ——> 5th

Edit 1: Help needed in determining the Time complexity? Thanks for all the suggestion and code. Below is my version of working code: I would like to know the time complexity of my code? In short, what is the complexity of sorting a 2d array. 1d array --> O(n.logn) 2d --> ??

private static int solve(int pathLength, int[][] floristIntervals) {
        // TODO Auto-generated method stub
        System.out.println(Arrays.deepToString(floristIntervals));
        Arrays.sort(floristIntervals, new Comparator<int[]>(){

            @Override
            public int compare(int[] o1, int[] o2) {
                // TODO Auto-generated method stub

                /*System.out.println(o1[0]);
                System.out.println(o1[1]);
                System.out.println(o2[0]);
                System.out.println(o2[1]);*/
                if(o2[1]-o2[0] == o1[1]-o1[0]){
                    if(o2[0] > o1[0]){
                        return -1;
                    }
                    return 1;
                }
                if (o2[1]-o2[0] > o1[1]-o1[0])
                    return 1;
                else
                    return -1;
            }

        });
        System.out.println(Arrays.deepToString(floristIntervals));
sandy
  • 29
  • 5
  • So you enter one 2D array with random number of rows but only 2 columns, and then you sort them by "value" if "values" are same you sort them by the value in first column. Can you just clarify what you mean by "values" for the primary sort ? EDIT: Nevermind I got it, I'm working on solution with explanation. – whatamidoingwithmylife Dec 26 '17 at 00:14
  • 1
    http://idownvotedbecau.se/noattempt/, such as writing a [`Comparator`](https://docs.oracle.com/javase/9/docs/api/java/util/Comparator.html) that compares exactly like you described. – Andreas Dec 26 '17 at 00:33
  • @Andreas I am aware of Comparator solution but what about the second comparison? I mean after sorting as per difference; if they have equal difference I want to sort as per the column number/value, how can I do that? – sandy Dec 26 '17 at 00:41
  • @sandy By doing some [**research**](http://idownvotedbecau.se/noresearch/) on how to write a `Comparator` that [compares multiple criteria](https://stackoverflow.com/q/3704804/5221149), or by wrapping the `Comparator` in another `Comparator`, e.g. as done by [`thenComparing()`](https://docs.oracle.com/javase/9/docs/api/java/util/Comparator.html#thenComparing-java.util.Comparator-). – Andreas Dec 26 '17 at 00:43
  • @Andreas I have edited my question with my version of working code but I am not able to determine the time complexity. Can you please help me in it? – sandy Dec 26 '17 at 22:25
  • @GCP I have edited my question with my version of working code. Can you tell me the time complexity of your and my code. – sandy Dec 26 '17 at 22:27
  • @sandy I'm not an expert (~2 months experience), but I did some quick research and it turns out both sorting methods (Arrays.sort and Collections.sort) use merge sort, and it turns out the complexity for merge sort is: n log(n), however to be sure and not take what I've written for granted you should consult with someone who is more experienced – whatamidoingwithmylife Dec 26 '17 at 23:02

1 Answers1

0

OK, so here is a possible solution:

package com.company;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        // creating Scanner and declaring and initializing our 2D array
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of rows for your 2D array: ");
        int rows = in.nextInt();
        int[][] arr = new int[rows][2];

        // asking user for values to assign to our array
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print("Enter value for row " + i + " column " + j + ": ");
                arr[i][j] = in.nextInt();
            }
        }

        // create a Comparator that compares by result of reduction of 2nd and first column in each array, then if the
        // results are equal (0), we compare by first column
        Comparator<int[]> cmp = Comparator.<int[]>comparingInt(x -> x[1] - x[0]).thenComparing(Comparator.comparingInt(x -> x[0]));

        // we make a stream out of the 2D array, we sort inner elements (single dimensional arrays) by the Comparator we created and we print out results
        Arrays.stream(arr)
              .sorted(cmp)
              .forEach(x -> System.out.println(Arrays.toString(x)));

    }

}

Input array:

    [0] [1]

[0]  0   6

[1]  0   7

[2]  4   5

[3]  2   3

[4]  0   1

Output array:

[0, 1]
[2, 3]
[4, 5]
[0, 6]
[0, 7]

Ask if you have more questions.

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35