-1

Given the set of unsorted integers, how to find every pair of integers which have minimum difference. There are 3 samples as described below:

a = random.sample (range(-200,200), 5)
b = random.sample (range(-1000, 1000), 25)
c = random.sample (range(-2000, 2000), 50)

The expected output should be something like:

List A = [-85, -154, -33, 192, -160]

Minimum pairs for list A:
(-160, -154)
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
jimo
  • 430
  • 2
  • 7
  • 19
  • 1
    Without doing your homework for you, I'd try sorting the array. – EOF Apr 05 '15 at 11:42
  • Hi, please add more details to the questions. In its present state it is hard to understand the requirement, can you please add an example to illustrate the requirement? – Aravind Apr 05 '15 at 11:49
  • @Aravind What is not clear? – jimo Apr 05 '15 at 11:58
  • Now, that the question is edited, I understand, so you are looking for the pair having the minimum difference. As EOF has suggested, sorting would help you. Sort the list and check the difference between consecutive elements. – Aravind Apr 05 '15 at 12:02
  • @Aravind I simply rolled the question edit back, I don't understand why OP removed the explanation. – Weather Vane Apr 05 '15 at 12:06
  • 1
    If the array values are not unique there may be several equal values, which would need several pairs to answer the question. Even if the values are unique, there may be several pairs with the same minimum difference. – Weather Vane Apr 05 '15 at 12:09

1 Answers1

0

Catch! :)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct Pair
{
    size_t first;
    size_t second;
};

struct Pair minimum_difference( const int a[], size_t n )
{
    struct Pair p = { 0 };

    if ( 1 < n )
    {
        p.first = 0;
        p.second = 1;

        for ( size_t i = 0; i < n - 1; i++ )
        {
            for ( size_t j = i + 1; j < n; j++ )
            {
//              printf( "%d %d %llu\n", a[i], a[j], 
//                      ( unsigned long long )abs( a[i] - a[j] ) );
                if ( ( unsigned long long )abs( a[i] - a[j] ) <
                     ( unsigned long long )abs( a[p.first] - a[p.second] ) )
                {
                    p.first  = i;
                    p.second = j;
                }                  
            }
        }
    }

    return p;
}

int main(void) 
{
    const size_t N = 5;
    const int UPPER_BOUND = 40 * N;

    int a[N];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < N; i++ )
    {
        a[i] = rand() % ( 2 * UPPER_BOUND ) - UPPER_BOUND;
    }

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    printf( "\n" );

    struct Pair p = minimum_difference( a, N );

    printf( "(%d, %d)\n", a[p.first], a[p.second] );

    return 0;
}

The program output might look loke

119 9 -193 21 -43 
(9, 21)

This prpgram finds only the first pair with the minimum difference. If there are several pairs with the minimum difference when you have to allocate dynamically an array of pairs. Of course the function will be changed.

The other approach is to use a sort-copy algorithm by using an additional array. And then traverse this array calculating the difference.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335