查找总和为15的最小数字对

时间:2019-05-12 05:50:57

标签: java arrays sorting numbers

我试图找到最小的数字对以达到15的总和。我正在为它们创建新的数组,并将该数组传递给添加该数组元素并生成true或false的方法。如果方法返回false,则数组大小将增加。

public class FindMinimum {

    static int arr[] = { 10, 3, 2, 13 };
    static int numArr[] = new int[30];
    static int arrLength = 2;
    static boolean status = false;
    static int number;

    public static void main(String args[]) {
        for (int i = 0; i < arrLength; i++) {
            numArr[i] = arr[i];
        }
        if (checkPair(numArr)) {
            System.out.println("Number found");
        } else {
            arrLength = arrLength + 1;
            System.out.println("Increasing array length by one");
        }
    }

    public static boolean checkPair(int x[]) {
        for (int i = 0; i < x.length; i++) {
            number = number + x[i];
        }
        if (number == 15) {
            status = true;
            for (int i : x) {
                System.out.println(i);
            }
        } else {
            status = false;
        }
        return status;
    }
}

预期结果是最小的加法对,即“ 13,2”

2 个答案:

答案 0 :(得分:0)

如果我正确理解,需要找到总是加到15的最小对。如果正确,下面的代码应该解决它。

public static void main(String args[]) {

        Arrays.sort(arr);
        for (int i=0,j=arr.length-1;i<arr.length && j>=0;) {
            if ((arr[i]+arr[j])<15) {
                /*System.out.println(arr[i]+"-"+arr[last-i]);
                break;*/
                i++;
            } else if ((arr[i]+arr[j])>15) {
                j--;
            } else {
                System.out.println(arr[i]+"-"+arr[j]);
                break;
            }
        }
    }

答案 1 :(得分:0)

import java.util.Scanner;

public class FindMinimumPair {

    static Scanner sc = new Scanner(System.in);
    static int userArr[];
    static int numArr[]; // New array to take number / pairs from main array to compare with else numbers
                            // in the main array
    static int arrLength = 1; // increase the array length of numArr if pair is more than 2 numbers
    static boolean status = false; // check method returns true or false
    static int sum;

    public static void main(String args[]) {
        System.out.println("Sum of pair should be ?");
        sum = sc.nextInt();
        System.out.println("Enter the lenght of an array");
        int userArrLength = sc.nextInt();
        userArr = new int[userArrLength];
        System.out.println("Enter array integers upto " + userArrLength);

        for (int i = 0; i < userArrLength; i++) {
            userArr[i] = sc.nextInt();
        }

        // Loop to read numbers from main array
        for (int i = 0; i < userArr.length; i++) {
            // Defines the length of new array
            numArr = new int[arrLength]; // initialize the new array
            // Loop to add numbers into new array
            for (int j = 0; j < arrLength; j++) {
                numArr[j] = userArr[j]; // add numbers into new array
            }
            if (check(numArr)) { // call check method and pass new array in it
                for (int a : numArr) { // if returns true then print that array (contains the pair)
                    System.out.print(a + " ");
                }
                System.out.print(userArr[numArr.length]); // print the last number which is the part of numArr
                System.out.println(" is equals to " + sum);
            } else {
                System.out.println("Numbers not found");
            }
            arrLength = arrLength + 1; // increase the array length if false
        }
    }

    public static boolean check(int number[]) {
        int x = 0;
        // Loop to make sum of all numbers of numArr (make it single number)
        for (int j = 0; j < number.length; j++) {
            x = x + number[j];
        }

        outer: for (int i = 0; i < number.length; i++) { // loop for elements in numArr array

            for (int j = 0; j < userArr.length; j++) { // loop for given array elements
                if (x + userArr[j] == sum) { // check each number of given array with the sum of numArr
                    status = true;
                    break outer; // breaks outer loop and returns true
                } else {
                    status = false;
                }
            }
        }
        return status;
    }
}