Find the minimum of the absolute value of the difference in an array (pigeon nest principle)

Source: Internet
Author: User

Given an array of a[n], you want to find the smallest |a[i]-a[j]| in a group if only one element returns 0.

Seems to be Microsoft's face test, estimated that most people think of the first is the sort after the comparison bar, hehe, is an individual will do. The interviewer test you this question has the yarn meaning, this question we may use the drawer principle (also called the Pigeon Nest Principle) to put n elements in the n+1 bucket (only need O (n) time). Solve by the following procedure:

1 first find the largest and smallest elements in the array, and if they are equal, return 0 directly.

2 Determine the size of each bucket bucket_size= (maxe-mine)/(n-1)

3 Place each element in the corresponding bucket ID bucket_id= (A[i]-mine)/bucket_size;

4 The minimum value must be the adjacent element subtraction Ah, traverse the bucket, Ans=min (Ans,min (Bucket[i] The minimum value, Bucket[i+1].mine-bucket[i].maxe)) Of course, there is only one element in the bucket of the minimum value must not be calculated, So every time we can lose the bucket of less than 2 of the bucket, once we find that ans equals 0 can return immediately.

The code is as follows:

/************************************************************************* > File Name:xiaozhao.cpp > Autho R:acvcla > Qq:[email protected] > Mail: [email protected] > Created time:2014 December 27 Monday 22:34 13 sec *************************************************************************/#include <bits/stdc++.h >using namespace Std;const int inf=0x3f3f3f3f;struct info{int maxe,mine;vector<int>v;info (): Maxe (-inf), mine (INF) {v.clear ();}}; int Find_t_min (vector<int> in) {int sz=in.size (); Info bucket[sz+1];int maxe=*max_element (In.begin (), In.end ()); int mine=*min_element (In.begin (), In.end ()), if (maxe==mine) return 0;int bucket_size= (maxe-mine)/(sz-1); if (bucket_ size==0) return 0;for (int i=0; i<sz; i++) {int bucket_id= (in[i]-mine)/bucket_size;bucket[bucket_id].maxe=max ( Bucket[bucket_id].maxe,in[i]); Bucket[bucket_id].mine=min (Bucket[bucket_id].mine,in[i]); bucket[bucket_id]. V.push_back (In[i]);} int ans=inf;for (int i=0;i<sz;i++) {ans=min(Ans,bucket[i+1].mine-bucket[i].maxe), if (!ans) return ans;if (Bucket[i].v.size () >1) ans=min (Ans,find_t_min ( BUCKET[I].V));} if (Bucket[sz].v.size () >1) ans=min (Ans,find_t_min (BUCKET[SZ].V)); return ans;} int main (int argc, char const *argv[]) {int n;/***********************************ifstream fin ("In.ini"); *ofstream Fout ("Out.ini"); * #define COUT fout* #define CIN fin**********************************/while (cin>>n) {Vector<int > In;int x;while (n--) {cin>>x;in.push_back (x);} Cout<<find_t_min (in) <<endl;in.clear ();} return 0;}

Find the minimum of the absolute value of the difference in an array (pigeon nest principle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.