wu :: forums
« wu :: forums - Array Problem (x-y=z for x,y in sorted array) »

Welcome, Guest. Please Login or Register.
Apr 23rd, 2024, 3:05am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: ThudnBlunder, Eigenray, towr, william wu, Icarus, SMQ, Grimbal)
   Array Problem (x-y=z for x,y in sorted array)
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Array Problem (x-y=z for x,y in sorted array)  (Read 4032 times)
witee
Newbie
*





   
Email

Posts: 48
Array Problem (x-y=z for x,y in sorted array)  
« on: Jul 13th, 2010, 6:35am »
Quote Quote Modify Modify

Given a sorted array and a constant z, find two numbers x and y, if exist, s.t. x-y = z
give the optimized solution?  Roll Eyes
« Last Edit: Jul 18th, 2010, 2:16pm by Grimbal » IP Logged
pex
Uberpuzzler
*****





   


Gender: male
Posts: 880
Re: Array Problem  
« Reply #1 on: Jul 13th, 2010, 6:50am »
Quote Quote Modify Modify

on Jul 13th, 2010, 6:35am, witee wrote:
Given a sorted array and a constant z, find two numbers x and y, if exist, s.t. x-y = z
give the optimized solution?  Roll Eyes

 
There is a trivial O(n) algorithm:
 
hidden:
- start with i=0, j=0
- at every iteration:
  - compute d = a[j]-a[i]
  - if d = z: done
  - if d > z: i++
  - if d < z: j++
- terminate when an index runs out of bounds
IP Logged
nakli
Junior Member
**






   
WWW Email

Gender: male
Posts: 62
Re: Array Problem  
« Reply #2 on: Jul 13th, 2010, 8:10am »
Quote Quote Modify Modify

This neatly finds one such pair(x,y).
 
How fast can we find all such existing pairs?
IP Logged

I was born naked on a bed with a lady.
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Array Problem  
« Reply #3 on: Jul 13th, 2010, 2:56pm »
Quote Quote Modify Modify

You can find the rest by continuing in the same way. Same time complexity.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7526
Re: Array Problem  
« Reply #4 on: Jul 15th, 2010, 3:02pm »
Quote Quote Modify Modify

Yep.  Whenever you find a solution, print it and advance both i and j to the next higher value in the array.
IP Logged
pex
Uberpuzzler
*****





   


Gender: male
Posts: 880
Re: Array Problem  
« Reply #5 on: Jul 17th, 2010, 3:32am »
Quote Quote Modify Modify

There are some issues with equal values though: suppose you are looking for a sum of 11 and the array is
 
... 4 4 ... 7 7 ...
 
This means that there are four (4,7) pairs. After finding the first such pair, advancing one (or both) of the pointers will make you miss at least one other pair. So a bit of bookkeeping is required.
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7526
Re: Array Problem  
« Reply #6 on: Jul 18th, 2010, 2:11pm »
Quote Quote Modify Modify

I understood the problem as following:
  find pairs (x,y) s.t. x and y exist in the array and x-y=z.
So duplicates don't count.
If duplicates did count, I would say in your case you need to return 4 pairs:
  (41,71), (41,72), (42,71), (42,72)
(looking for x+y=11 or x-y=-3)
That would make the output size O(n2) as worst case and no solution could be faster than that.
IP Logged
birbal
Full Member
***





   


Gender: male
Posts: 250
Re: Array Problem  
« Reply #7 on: Jul 24th, 2010, 7:51am »
Quote Quote Modify Modify

on Jul 18th, 2010, 2:11pm, Grimbal wrote:
I understood the problem as following:
  find pairs (x,y) s.t. x and y exist in the array and x-y=z.
So duplicates don't count.
If duplicates did count, I would say in your case you need to return 4 pairs:
  (41,71), (41,72), (42,71), (42,72)
(looking for x+y=11 or x-y=-3)
That would make the output size O(n2) as worst case and no solution could be faster than that.

Size of the output would be O(n2), we we need not iterate through each pair, we can count number of 4s ( 2 in this case) and number of 7s ( also 2 ) and can say ( 4 , 7 ) - 2*2 times.
IP Logged

The only thing we have to fear is fear itself!
qddaichy
Newbie
*





   


Posts: 1
Re: Array Problem (x-y=z for x,y in sorted array)  
« Reply #8 on: Jan 26th, 2014, 10:28pm »
Quote Quote Modify Modify

Just want to see the solution
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