简体   繁体   中英

Find smallest pair sum in array

This is what I've been trying to achieve:

Input: arr[] = {1, 7, 2, 9, 6}
The pair (1, 2) will have the minimum sum pair ie 1 + 2 = 3

Output: (1,2)=3

  • Because the while loop is iterating once, I am not able to achieve the intended result. Need some help !!
public class Find_Smallest_Pair_Sum_In_Array {

    public static void main(String[] args) {

        int[] arr = { 1, 7, 2, 9, 6 };
        StringBuilder strBuilder = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                strBuilder.append("(");
                strBuilder.append(arr[i]);
                strBuilder.append(",");
                strBuilder.append(arr[j]);
                strBuilder.append(")");
                strBuilder.append("=");
                strBuilder.append(arr[i] + arr[j]);
                strBuilder.append("\n");
            }
        }
           
        Scanner scan = new Scanner(strBuilder.toString()); 

        int first, next = 0; 
        int previous = 0;
        String newStr = "";
        
        while (scan.hasNextLine()) {
            String oneLine = scan.nextLine();
            int num = Integer.parseInt(oneLine.substring(oneLine.lastIndexOf('=') + 1, oneLine.length()));
                        
            first = num;            
            if(num > previous) {
                newStr = oneLine;
                System.out.println("if :::: " +newStr);

            } else if(num < previous) {
                newStr = oneLine;
                System.out.println("else if :::: " +newStr);

            }
            previous = first;
            
        }
        System.out.println(newStr);
    }
}

I think what you're basically is obtaining the smallest two numbers of an array I hope...

This is a nice trick, probably not the more algorithmically efficient way but it's quick and dirty:

Arrays.sort(arr); 
//This will sort your array.
if(arr.length>1){
System.out.println(arr[0]);
System.out.println(arr[1]);
}

Will be your smallest two values. and It will also handle situations where your length is lower than 2.

To find the smallest 2 numbers, try this:

int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
for (int i : arr) {
  if (i < min1) {
    min2 = min1;
    min1 = i;
  } else if (i < min2) min2 = i;
}

Issue fixed with this simple solution:

    public class Find_Smallest_Pair_Sum_In_Array {

       public static void main(String[] args) {

        int[] arr = { 1, 7, 2, 9, 6 };
        StringBuilder strBuilder = new StringBuilder();

        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                strBuilder.append("(");
                strBuilder.append(arr[i]);
                strBuilder.append(",");
                strBuilder.append(arr[j]);
                strBuilder.append(")");
                strBuilder.append("=");
                strBuilder.append(arr[i] + arr[j]);
                strBuilder.append("\n");
            }
        }

        Scanner scan = new Scanner(strBuilder.toString()); 
        String oneLine = "";
        int i = 0;
        while (scan.hasNextLine()) {
            oneLine = scan.nextLine();
            if(i == 0) {
                System.out.println(oneLine);
                break;
            } 
           i++; // This is not required though, but still. 
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM