Antony Ajay
Antony Ajay

Reputation: 83

Minimum difference in an array

I want to find the minimum difference among all elements of an array. I read through various other questions, but couldn't find the exact source of the error in the following code.

#include<iostream>
#include<stdio.h>
#include<cstdlib>

using namespace std;

void quicksort(long int *lp, long int *rp);

int main()
{
    int t,n;
    long int s[5000];

    cin>>t;
    while(t--){
    cin>>n;
    for(int i=0;i<n;i++) cin>>s[i];
    quicksort(&s[0],&s[n-1]);
    //cout<<"passes:"<<passes<<endl;
    //for(int i=0;i<n;i++) cout<<s[i]<<" ";
    long int min = abs(s[1]-s[0]);
    //cout<<endl<<min;
    for(int i=1;i<(n-1);i++){
        long int temp = abs(s[i]-s[i+1]);
        if (temp <= min) min = temp;
    }
    cout<<min;
    }
}

void quicksort(long int *lp,long int *rp){
    int arraysize= (rp-lp)+1;
    if(arraysize > 1){
       long int *pivot = (lp+(arraysize/2));
       long int swap=0;
      long int *orgl = lp;
      long int *orgr = rp;
      while(lp!=rp){
        while (*lp < *pivot) lp++;
        while (*rp > *pivot) rp--;
        if (lp == pivot) pivot=rp;
        else if (rp == pivot) pivot=lp;
        swap = *lp;
        *lp = *rp;
        *rp = swap;
        if((*lp == *pivot) || ( *rp == *pivot)) break;
    }
    quicksort(orgl,pivot-1);
    quicksort(pivot+1,orgr);
}

}

The problem statement is given in this link : http://www.codechef.com/problems/HORSES Can you please identify the error in my program ?

Upvotes: 1

Views: 1969

Answers (4)

#include <iostream> using namespace std;

int main() {
    int a[] = {1,5,2,3,6,9};
    int c=0;
    int n = sizeof(a)/sizeof(a[0]);
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            cout<<a[i]<<" - "<<a[j]<<" = "<<a[i]-a[j]<<endl;
            if(abs(a[i]-a[j]) == 2)
                c++;
        }
    }
    cout<<c<<endl;
    return 0; }

Upvotes: 0

Roman Dzhabarov
Roman Dzhabarov

Reputation: 521

You are using C++ so instead of using your custom quicksort which is not really guarantee O(n*logn) you better use sort from <algorithm>. This logic looks good:

    long int min = abs(s[1]-s[0]);
    //cout<<endl<<min;
    for(int i=1;i<(n-1);i++){
        long int temp = abs(s[i]-s[i+1]);
        if (temp <= min) min = temp;
    }

By the way: 
cout<<min; 
Add cout<<min << endl;

Upvotes: 3

jfs
jfs

Reputation: 414865

sort(s, s + n); // #include <algorithm> O(n*log n)

Otherwise sort/find minimum algorithm is correct. There are O(n) algorithms based on randomization, bucket sort.

#include <algorithm> // sort()
#include <iostream>

int main() {
  using namespace std;
  int ntests, n;
  const int maxn = 5000; // http://www.codechef.com/problems/HORSES
  int s[maxn];
  cin >> ntests; // read number of tests
  while (ntests--) {
    cin >> n; // read number of integers
    for (int i = 0; i < n; ++i) cin >> s[i]; // read input array

    sort(s, s + n); // sort O(n*log n)
    // find minimal difference O(n)
    int mindiff = s[1] - s[0]; // minn = 2
    for (int i = 2; i < n; ++i) {
      int diff = s[i] - s[i-1];
      if (diff < mindiff) mindiff = diff;
    }
    cout << mindiff << endl;
  }
}

Upvotes: 0

Daniel Fischer
Daniel Fischer

Reputation: 183978

The line

if((*lp == *pivot) || ( *rp == *pivot)) break;

is wrong. Consider

5 4 7 5 2 5
      ^
    pivot

Oops.

This line

long int temp = abs(s[i]-s[i+1]);

is unnecessarily complex. Since the array is (supposedly) sorted,

long int temp = s[i+1] - s[i];

does the same without calling abs.

Upvotes: 0

Related Questions