Search code examples
javastringbuilder

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 i.e. 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);
    }
}

Solution

  • 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. 
            }
        }
    }