Calculate the absolute minimum difference between any two numbers in a huge array of integers


specialized

I have a very long array (300 000+ unsorted integers) in a Java program and need to calculate the smallest absolute difference between any two numbers within the array and display the absolute difference and the corresponding pair of numbers as output. The entire calculation should happen quickly.

I have the following code, which usually works:

private static void calcMinAbsDiff(int[] inputArray)
{
    Arrays.sort(inputArray);

    int minimum = Math.abs(inputArray[1] - inputArray[0]);

    int firstElement = inputArray[0];

    int secondElement = inputArray[1];

    for (int i = 2; i < inputArray.length; i++)
    {
        if(Math.abs(inputArray[i] - inputArray[i-1]) < minimum)
        {
            minimum = Math.abs(inputArray[i] - inputArray[i-1]);

            firstElement = inputArray[i-1];

            secondElement = inputArray[i];
        }
    }

    System.out.println("Minimum Absolute Difference : "+minimum);

    System.out.println("Pair of Elements : ("+firstElement+", "+secondElement+")");
}

However, the output I receive is all 0s. I believe this is because the array is too long.

bl33p bl00p

If your dataset has two or more zeros and no negative integers, your output is expected. After sorting, both inputArray[0] and inputArray[1] are 0, and the difference is 0. No other pair of adjacent elements has an absolute difference less than 0, so minimum, firstElement, and second Element all end up with 0 at the end of the algorithm.

If you really don't have zeros in your dataset, or you do have negative integers, then you might have an initialization problem. Check this thread: Why does my simple array only print zeros in java?

If not, then the only other thing I can think of is that you had a problem with the previous range causing the data to be zeroed out.

I would try printing samples of the dataset at various points to see exactly where/when it zeros out.

If you're still having issues, please post more information about the dataset and the scope from which this function is called to help us understand what's going on. Let us know how you did it!

Related


Calculate difference between two numbers and get absolute value

distressed: I want to find the difference between two numbers in Go, the result should not be in "-". Please find my code below: dollarValue := 240000 - 480000 The result is "-240000". But my expected output is only "240000". Can anyone help how to calc

Minimum difference between the sum of two numbers in an array

sky_coder123 I am trying to solve this problem : A physics professor provides projects for students in his class. Students must form a team of two to work on the project. The professor leaves the students to decide the team. The number of students in a class w

Calculate the absolute difference between two angles

Daniel I have two angles a and b and I want to calculate the absolute difference between the two angles. example >> absDiffDeg(360,5) ans = 5 >> absDiffDeg(-5,5) ans = 10 >> absDiffDeg(5,-5) ans = 10 toss Normalize the difference, because mod(x,y) takes the s

Calculate difference between two numbers and get absolute value

distressed: I want to find the difference between two numbers in Go, the result should not be in "-". Please find my code below: dollarValue := 240000 - 480000 The result is "-240000". But my expected output is only "240000". Can anyone help how to calc

Calculate the minimum difference between two dictionaries

Jordan Ganson I am trying to calculate the difference between two dictionaries to return a specific value. I put in different values and they should return different results, but the result remains the same. diets = {"normal" : {'p': '32.50', 'c': '60', 'f': '

PHP calculate percentage difference between two numbers

Sanju I am trying to find the percentage difference of two numbers. The reality is that iam gets the total value of user data for this week and last week. Now I need to see the percent difference in performance between this week's data and last week's data. Be

Minimum difference between the sum of two numbers in an array

sky_coder123 I am trying to solve this problem : A physics professor provides projects for students in his class. Students must form a team of two to work on the project. The professor leaves the students to decide the team. The number of students in a class w

Calculate the absolute difference between two angles

Daniel I have two angles a and b and I want to calculate the absolute difference between the two angles. example >> absDiffDeg(360,5) ans = 5 >> absDiffDeg(-5,5) ans = 10 >> absDiffDeg(5,-5) ans = 10 toss Normalize the difference, because mod(x,y) takes the s

Calculate the minimum difference between two dictionaries

Jordan Ganson I am trying to calculate the difference between two dictionaries to return a specific value. I put in different values and they should return different results, but the result remains the same. diets = {"normal" : {'p': '32.50', 'c': '60', 'f': '

Unable to calculate percentage difference between two numbers

LyonsTheWay2Gold I am trying to display a percentage after subtracting a number. example: Job Cost = £165.00Worker Charges = £42.00Surplus =% What percentage of costs are left after workers take a pay cut? My code output shows 0 int number = 0, number1, result

Create an array of integers between two numbers in JavaScript

buzzing I am trying to create an array that starts with 0.05 and ends with 2.5. I want the value between min and max to grow in increments of 0.245. var min = 0.05; var max = 2.5; var increments = ((max - min) / 100); var arr = []; The final output should loo

How to find absolute difference between two numbers

User 1145643 What is the best way to find the absolute difference between two numbers in MYSQL so that the results can be sorted? The following works only if numberA is greater than numberB, but as you can see this is not always the case. Is there a good way t

Create an array of integers between two numbers in JavaScript

buzzing I am trying to create an array that starts with 0.05 and ends with 2.5. I want the value between min and max to grow in increments of 0.245. var min = 0.05; var max = 2.5; var increments = ((max - min) / 100); var arr = []; The final output should loo

Calculate difference between two numbers and get absolute value

distressed: I want to find the difference between two numbers in Go, the result should not be in "-". Please find my code below: dollarValue := 240000 - 480000 The result is "-240000". But my expected output is only "240000". Can anyone help how to calc

Minimum difference between the sum of two numbers in an array

sky_coder123 I am trying to solve this problem : A physics professor provides projects for students in his class. Students must form a team of two to work on the project. The professor leaves the students to decide the team. The number of students in a class w

Calculate the absolute difference between two angles

Daniel I have two angles a and b and I want to calculate the absolute difference between the two angles. example >> absDiffDeg(360,5) ans = 5 >> absDiffDeg(-5,5) ans = 10 >> absDiffDeg(5,-5) ans = 10 toss Normalize the difference, because mod(x,y) takes the s

PHP calculate percentage difference between two numbers

Sanju I am trying to find the percentage difference of two numbers. The reality is that iam gets the total value of user data for this week and last week. Now I need to see the percent difference in performance between this week's data and last week's data. Be

Unable to calculate percentage difference between two numbers

LyonsTheWay2Gold I am trying to display a percentage after subtracting a number. example: Job Cost = £165.00Worker Charges = £42.00Surplus =% What percentage of costs are left after workers take a pay cut? My code output shows 0 int number = 0, number1, result

Create an array of integers between two numbers in JavaScript

buzzing I am trying to create an array that starts with 0.05 and ends with 2.5. I want the value between min and max to grow in increments of 0.245. var min = 0.05; var max = 2.5; var increments = ((max - min) / 100); var arr = []; The final output should loo