简体   繁体   中英

Return 3 integers that have the same difference between each two from smallest to biggest in an array

So, I wanted to insert an array of numbers, and return 3 numbers in order biggest to smallest with the same difference between each two.

Example:

2 3 7 9 12. 

Return:

2 7 12 because 2+5=7, 7+5=12. 

The code below is my attempt. I made 3 for loops .

Loops:

  • The went through the entire array, picked a number. 遍历整个数组,选择了一个数字。
  • The : for the remaining numbers, picked a smaller number. :对于剩余的数字,选择一个较小的数字。 Calculate their difference.
  • The : finds a third number that is smaller and has that same difference vs. the second number. :找到较小的第三个数字,并且与第二个数字具有相同的差异。

So: (first number - second number) = (second number - third number)

public static void main(String[] args) {
    int n;
    int num1;
    int num2;
    int num3;
    int dif;

    Scanner scan = new Scanner(System.in);

    System.out.print("How many numbers do you want to choose from? ");
    n = scan.nextInt();

    int nums[] = new int[n];

    System.out.println("Please input the integers: ");

    for (int i=0; i<n ;i++){
        nums[i] = scan.nextInt();
    }

    System.out.println(" ");

    for (int i=0; i<n; i++){

        for (int j=i+1; j<n; j++){      //compare element i to the rest of the array

            if(nums[j]<= nums[i]){  //if a num at j is smaller than num at i,
                num3 = nums[i];     //then num3 is num at i
                num2 = nums[j];     //and num2 is num at j
                dif = num3 - num2;  //find the difference

                for(int k=i+j+1; k<n; k++){ 
                    if(num2 == (nums[k]+ dif)){ //if num2 is num at k + difference
                    num1 = nums[k];             //then num1 must be num at k
                    }   
                }
            }
        }
    }
    System.out.print(num3); //This is the effort printing them out
    System.out.print(num2); //But for some reason I couldn't
    System.out.print(num1); //even I initialized num3,2,1 outside of the for loop

    scan.close();   //closing the scanner object
}

This works with two fors, if you are looking for faster one.

public static void main(String[] args) throws Exception {
    List<Integer> integers = Arrays.asList(1, 3, 5, 9, 17);
    Map<String, Integer> differenceMap = new HashMap<>();

    for (int i = 0; i < integers.size(); i++) {
        int first = integers.get(i);

        for (int j = i + 1; j < integers.size(); j++) {
            int second = integers.get(j);

            int difference = second - first;
            int next = difference + second;
            if (integers.contains(next)) {
                differenceMap.put(first + " - " + second + " - " + next, difference);
            }
        }
    }

    differenceMap.keySet().forEach(System.out::println);

}

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