Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:05:22+00:00 2026-06-11T09:05:22+00:00

There is an array which can contain, say, upto 1000 elements. The range of

  • 0

There is an array which can contain, say, upto 1000 elements. The range of numbers it can spawn is say 1 to 10^10. Now I have to find the minimal absolute difference between two numbers in the array. I have thought of two algorithms:

For the first one, I have defined a binarysearch function which finds the position of a to-be-inserted number in a sorted array. Now I start the sorted array with only the first number of the given array and begin iterating on the given array from the second element onwards. For each number, I find its position in the sorted array. If the number at that position is this number, then the difference is 0, it is the lowest possible one, so I exit the loop. Else, I insert the number in the sorted array at that point, then check the difference between that number and the previous and next numbers in that array. Then I store the minimum of this result and the previous result, and continue in this fashion.

Second: I sort the array using quicksort. (The range is too large, so I think radix sort won’t be that efficient). Then I iterate over it, breaking out with an answer of 0 if two consecutive numbers are equal, else storing the minimum of the difference between that number and the previous number and the previous result.

Which one will be more efficient?

Is there any better algo?

Stackoverflow has a number of posts in this regard, but they didn’t help much. Here’s my code in Perl:

sub position {
    my @list   = @{$_[0]};
    my $target = $_[1];

    my ($low,$high) = (0, (scalar @list)-1);

    while ($low <= $high) {
        $mid = int(($high + $low)/2);

        if ( $list[$mid] == $target ) {

            return $mid;
        }
        elsif ( $target < $list[$mid] ) {

            $high = $mid - 1; 
        }
        else {

            $low = $mid + 1;
        }
    }
    $low;
}
sub max { $_[0] > $_[1] ? $_[0] : $_[1]; }
sub min { $_[0] > $_[1] ? $_[1] : $_[0]; }

$ans        = 10_000_000_000;
@numbers    = (234, 56, 1, 34...123); #given array
($max,$min) = @num[0, 0];
@sorted     = ($numbers[0]);

for ( @num[1 .. $#num] ) {
    $pos = position(\@sorted, $_);

    if ( $sorted[$pos] == $_ ) { 

        $ans = 0;
        last;
    }
    splice @sorted, $pos, 0, $_;

    if ( $#sorted == $pos ) { 

        $ans = min($_-$sorted[-2], $ans);
    }
    elsif ( 0 == $pos ) {

        $ans = min($sorted[1]-$_, $ans);
    }
    else { 

        $ans = min(min(abs($sorted[$pos-1]-$_), abs($sorted[$pos+1]-$_)), $ans);
    }
    $max = max($_, $max);
    $min = min($_, $min);
}
print "$ans\n";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:05:24+00:00Added an answer on June 11, 2026 at 9:05 am

    You have up to 5k elements.

    Note that a sandy bridge processor has 32KB L1-Cache, assuming 4 bytes integer – it means it can contain 8192 integers.

    I’d try to avoid as much as possible creating additional data (except counters and such), and do everything in place using the same array. This will make the number of cache-misses very small, and will probably outpeform any algorithm.

    Thus, an in-place quicksort and than iterating over the elements in the array will probably be better then any other solution, both for being cache-efficient, while still keeping decent asymptotical complexity of O(nlogn).

    Note – Although this solution will probably be more efficient (at least theoretically), the scale is still very small – and unless you are going to do this oporation a lot of times – it just doesn’t worth your time over-optimizing it.


    General tip: when talking about small scale problems (and up to 5000 elements fits this criteria) the big-O notation is usually not enough. The cache performance is usually the dominant factor in these problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, Say you have an array of strings, which contain only characters '0'
I have an images array in which there are number of images. Not fixed,
i wrote an array wrapper class PersonArray which can contain objects of a certain
I have a 2D array which just contains boolean values showing if there is
I am making a page in which there will be array of images. I
Is there any built-in function in Python3/Numpy which filters an array and returns indices
i am using visual studio 2010. i have a three dimensional structure array which,
I have an array which contains sets of three similar named items; however, sometimes
I have an array called: $fragment = array($fragment); Which has the following three values:
Let's say I have an array that looks like this: $array[0] = 'House'; $array[1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.