Search code examples
javaarrayssortingtreemap

How to get output from a treemap sequentially without depend upon the key value in java


I have a programming problem. I have two arrays. nums1={1,7,11} and nums2={2,4,6}. I have to find out the smallest sum taking 1 number from each array upto kth time. In my example k=3. So smallest sums are {1+2=3},{1+4=5},{1+6=7}. Arrays are always in sorted order.

I have used Treemap to solve this problem.

Here we go:

public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
     Map<Integer,int[]> value=new TreeMap<>();
     int[]arr_hold=new int[2];
     int []solve_arr=new int[k];
     List<int[]> solve = new ArrayList<>();
     ArrayList<Integer>add_sum= new ArrayList<>();
     int sum=0;
     for(int i=0;i<nums1.length;i++){
         for(int j=0;j<nums2.length;j++){
             sum=nums1[i]+nums2[j];
             arr_hold[0]=nums1[i];
             arr_hold[1]=nums2[j];
             value.put(sum,arr_hold);
         }
     }


     return solve;
 }

Problems: 1.When I print System.out.println(value); Output:

{3=[I@75412c2f, 5=[I@75412c2f, 7=[I@75412c2f, 9=[I@75412c2f, 11=[I@75412c2f, 13=[I@75412c2f, 15=[I@75412c2f, 17=[I@75412c2f}

Now why the values of array arr_hold not properly displayed?

As I am taking Treemap it already show me the output in a sorted format. So if I taking 1st,2nd,3rd value from tree map my problem will be solved. But the constraint here is

 value.get() method

searching element depend upon the key value. So if I am taking a loop to fetch the 3 smallest value from treemap it give me null output as keys mismatched.

 for(int k1=0;k1<k;k1++){
         System.out.println(value.get(k1));
     }

Output: null

     null

     null 

How could I solve my problem in my way?


Solution

  • What about this:

    import java.util.ArrayList;
    import java.util.List;
    
    public class KSmallestPairs {
        public static void main(String[] args) {
            System.out.println(kSmallestPairs(new int[]{1,7,11}, new int[]{2,4,6}, 3));
        }
    
        public static List<SumPair> kSmallestPairs(int[] leftInts, int[] rightInts, int k) {
            if (k < 1) {
                throw new IllegalArgumentException("k (=" + k + ") must higher than 0!");
            } else if (leftInts.length * rightInts.length < k) {
                throw new IllegalArgumentException("k (=" + k
                        + ") cannot be higher than the length of the cartesian product (="
                        + leftInts.length * rightInts.length + ")");
            }
    
            final List<SumPair> sumPairs = new ArrayList<>();
            int minLeftIndex = 0;
            int minRightIndex = 0;
            for (int leftIndex = 0, rightIndex = 0;
                 leftIndex < leftInts.length
                         && rightIndex < rightInts.length
                         && sumPairs.size() < k; ) {            
                final int leftInt = leftInts[leftIndex];
                final int rightInt = rightInts[rightIndex];
                sumPairs.add(new SumPair(leftInt, rightInt));
    
                if(leftIndex + 1 < leftInts.length && rightIndex + 1 < rightInts.length) {
                    final int nextLeftInt = leftInts[leftIndex + 1];
                    final int nextRightInt = rightInts[rightIndex + 1];
                    final int sumOfLeftIntAndNextRightInt = leftInt + nextRightInt;
                    final int sumOfNextLeftIntAndRightInt = nextLeftInt + rightInt;
                    if(sumOfLeftIntAndNextRightInt < sumOfNextLeftIntAndRightInt) {
                        rightIndex++;
                    } else {
                        leftIndex++;
                    }
                } else if(leftIndex + 1 < leftInts.length) {
                    leftIndex++;
                    rightIndex = minRightIndex;
                    minLeftIndex++;
                } else if(rightIndex + 1 < rightInts.length) {
                    leftIndex = minLeftIndex;
                    rightIndex++;
                    minRightIndex++;
                }
            }
            return sumPairs;
        }
    
        static class SumPair {
            private final int leftInt;
            private final int rightInt;
    
            public SumPair(int leftInt, int rightInt) {
                this.leftInt = leftInt;
                this.rightInt = rightInt;
            }
    
            public int getLeftInt() {
                return leftInt;
            }
    
            public int getRightInt() {
                return rightInt;
            }
    
            public int getSum() {
                return leftInt + rightInt;
            }
    
            @Override
            public String toString() {
                return leftInt + "+" + rightInt + "="+ getSum();
            }
        }
    }
    

    Output:

    [1+2=3, 1+4=5, 1+6=7, 7+2=9]
    

    It eagerly finds the k smallest sum pairs.