wu :: forums
« wu :: forums - Two numbers with minimum difference »

Welcome, Guest. Please Login or Register.
May 16th, 2024, 9:09am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Icarus, SMQ, towr, Eigenray, Grimbal, william wu, ThudnBlunder)
   Two numbers with minimum difference
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Two numbers with minimum difference  (Read 5794 times)
howard roark
Full Member
***





   


Posts: 241
Two numbers with minimum difference  
« on: Oct 14th, 2008, 10:14pm »
Quote Quote Modify Modify

Algorithm to find the two numbers whose difference is minimum among the set of numbers.
 
For example the sequence is 5, 13, 7, 0, 10, 20, 1, 15, 4, 19
 
The algorithm should return min diff = 20-19 = 1.
 
Constraint - Time Complexity O(N)
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Two numbers with minimum difference  
« Reply #1 on: Oct 15th, 2008, 1:13am »
Quote Quote Modify Modify

http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs; action=search
 
http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs; action=display;num=1182184382;
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
howard roark
Full Member
***





   


Posts: 241
Re: Two numbers with minimum difference  
« Reply #2 on: Oct 15th, 2008, 6:19pm »
Quote Quote Modify Modify

Thanks towr...... but the post you have given doesnt solve the problem........ Do you think there is any solution for this question
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Two numbers with minimum difference  
« Reply #3 on: Oct 16th, 2008, 12:59am »
Quote Quote Modify Modify

on Oct 15th, 2008, 6:19pm, hoogle wrote:
Thanks towr...... but the post you have given doesnt solve the problem........ Do you think there is any solution for this question

?
 
on Jun 18th, 2007, 10:21am, andyv wrote:
sort the array with radix sort (linear time). Then compare the differences of adjacent elements

Seems like a solution.
And it's not like I have a better one.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Hippo
Uberpuzzler
*****





   


Gender: male
Posts: 919
Re: Two numbers with minimum difference  
« Reply #4 on: Oct 16th, 2008, 6:37am »
Quote Quote Modify Modify

I expected editting the original thread is prefered way ... so I will continue in doing that
IP Logged
prof
Newbie
*





   


Posts: 1
Re: Two numbers with minimum difference  
« Reply #5 on: Oct 14th, 2011, 12:32am »
Quote Quote Modify Modify

-> Form a binary search tree which requires O(logn ).
-> Traverse BST in inorder fashion .
-> We will have sorted array.
-> Then track the minimum number variable from whole sorted array sequence.
 
-> That substraction number difference and its two corresponding numbers are the answer of the given thing.
IP Logged
RajivG
Junior Member
**





   
WWW

Gender: male
Posts: 72
Re: Two numbers with minimum difference  
« Reply #6 on: Oct 14th, 2011, 4:14am »
Quote Quote Modify Modify

on Oct 14th, 2011, 12:32am, prof wrote:

 
-> Form a binary search tree which requires O(logn ).

 
are you sure Smiley check once again
IP Logged
mmgc
Newbie
*





   


Gender: male
Posts: 47
Re: Two numbers with minimum difference  
« Reply #7 on: Oct 21st, 2011, 11:34pm »
Quote Quote Modify Modify

If space is not a big constraint & max - min  < c * N where c is not a big number then allocate an array of size max - min. Traverse the original array and keep updating the new array for e.g. if we have 5 in original array then put newArray[5 - min] = 1.  
 
Now traverse the new array while tracking the difference b/w subscripts with element 1.  
IP Logged
computing
Newbie
*





   


Gender: male
Posts: 2
Re: Two numbers with minimum difference  
« Reply #8 on: Oct 22nd, 2011, 11:54am »
Quote Quote Modify Modify

@mmgc, good technique. Yet read the question again. There is no guarantee that the difference max - min will be less than N, size of original array.
 
Perhaps we could optimize your solution? While updating the auxiliary array, check for adjacent locations (on both the sides) and update a global element for least minimum. Hope it works... Smiley
IP Logged
jianqiang
Newbie
*





   


Posts: 4
Re: Two numbers with minimum difference  
« Reply #9 on: Oct 23rd, 2011, 7:17pm »
Quote Quote Modify Modify

http://stackoverflow.com/questions/1669922/is-it-possible-to-find-two-nu mbers-whose-difference-is-minimum-in-on-time
 
I think answer posted by "sdcvvc" is a good answer to this thread.
IP Logged
MrLambert
Newbie
*





   


Posts: 13
Re: Two numbers with minimum difference  
« Reply #10 on: Oct 31st, 2011, 9:51am »
Quote Quote Modify Modify

"sdcvvc" wrote about smallest and largest numbers.
I dont see this happening here, but with small change to his solution, this is solved.
IP Logged
jianqiang
Newbie
*





   


Posts: 4
Re: Two numbers with minimum difference  
« Reply #11 on: Nov 3rd, 2011, 2:16am »
Quote Quote Modify Modify

If you're looking for nonnegative difference, then this is of course at least as hard as checking if the array has two same elements. This is called element uniqueness problem and without any additional assumptions (like limiting size of integers, allowing other operations than comparison) requires >= n log n time. It is the 1-dimensional case of finding the closest pair of points.
 
---posted by sdcvvc, in http://stackoverflow.com/questions/1669922/is-it-possible-to-find-two-nu mbers-whose-difference-is-minimum-in-on-time
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board