wu :: forums
« wu :: forums - Maximum sum of 2 array elements »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 6:35am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: SMQ, towr, Grimbal, Eigenray, ThudnBlunder, william wu, Icarus)
   Maximum sum of 2 array elements
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Maximum sum of 2 array elements  (Read 825 times)
mad
Junior Member
**





   


Posts: 118
Maximum sum of 2 array elements  
« on: Feb 6th, 2008, 10:14pm »
Quote Quote Modify Modify

Q.1)S contains the set of positive integer.Find the largest number c such that c=a+b where a,b,c are distict numbers in the set.
 
Q.2)write the program for displaying the ten most frequent words in a file such that your program should be efficient in all complexity measures.
 
Sorry if this has been posted before. Please provide the link in that case.
« Last Edit: Feb 6th, 2008, 10:20pm by mad » IP Logged
d33p4k
Newbie
*





   


Posts: 12
Re: Maximum sum of 2 array elements  
« Reply #1 on: Feb 11th, 2008, 10:30am »
Quote Quote Modify Modify

1.
Brute force approach will take O(n3) time.
But it can also be solved in O(n2) time and O(n) space:
First, sort the list and make a copy of it. Then, starting with the first element( call it 'a'), subtract the element value from each of the remaining elements of the second list. Now, there are two sorted list. And greatest common element( call it b) between them can be found in O(n) time. This element when added with 'a' gives the greatest common element (call it c) for  that 'a'.  Repeat this process for all elements in the list and the greatest among all c's is the answer.
If preserving the order of the elements is not required, then it can solved in O(n2) time and in space.
Sort the elements. Now, start with the last element( call it ak), find the first two consecutive number ai and aj in the list, such that ai + aj  >= ak, and j= i +1.
a1....ai-1 ai...aj aj+1...ak..an
 
if ai+ aj = ak, then   return ak .
else
 if ai-1+ aj < ak
  j++;
 if aj+1+ ai > ak
  i--;
It can be easily proved that atleast one of the above two condition is always true. Thus, at each step atleast one element is discarded. Total time it would take is O(k) time.
In order to get the final answer this has to be done for all k's from k=n to 1. So, overall complexity becomes O(n2).
 
 

 
2.
 
This can be solved in O(n) time and O(n) space.
hidden:
Make a suffix tree out of the file and find the 10 deepest forks in it.
IP Logged
johny_cage
Full Member
***





   


Gender: male
Posts: 155
Re: Maximum sum of 2 array elements  
« Reply #2 on: Feb 11th, 2008, 11:33pm »
Quote Quote Modify Modify

@47
 
Why it is in O(n2)?
It can be done in O(n log n), as sorting takes that much time. And after it, do O(n) searching with two pointer technique.
 
Any doubt, welcome....
IP Logged
d33p4k
Newbie
*





   


Posts: 12
Re: Maximum sum of 2 array elements  
« Reply #3 on: Feb 12th, 2008, 12:21am »
Quote Quote Modify Modify

on Feb 11th, 2008, 11:33pm, johny_cage wrote:
@47
 
Why it is in O(n2)?
It can be done in O(n log n), as sorting takes that much time. And after it, do O(n) searching with two pointer technique.
 
Any doubt, welcome....

 
The second step needs to be performed for each element present in the list. That makes it O(n2) time algo..
IP Logged
johny_cage
Full Member
***





   


Gender: male
Posts: 155
Re: Maximum sum of 2 array elements  
« Reply #4 on: Feb 12th, 2008, 1:04am »
Quote Quote Modify Modify

Ohhhhh!!!!!  Shocked
 
i just take the question otherwise.....
 
yups u r right.... Roll Eyes
IP Logged
kvs007
Newbie
*





   


Gender: male
Posts: 18
Re: Maximum sum of 2 array elements  
« Reply #5 on: Mar 7th, 2008, 11:41pm »
Quote Quote Modify Modify

To get the top ten words in the file you can implement a hashmap with the keys as hashcode of words and value their count. then get the top ten counts from the hashmap. It will take O(n)
IP Logged
mad
Junior Member
**





   


Posts: 118
Re: Maximum sum of 2 array elements  
« Reply #6 on: Mar 8th, 2008, 4:39pm »
Quote Quote Modify Modify

q)Given an array of non negative integers and an integer k, you are required to device an O(n) time algorithm to find out if there exists at least one pair of integers, say i and j, in the given array such that i+j equals k. That is, you are required to return a boolean value.The array is not sorted.
IP Logged
m_aakash
Junior Member
**





   


Posts: 96
Re: Maximum sum of 2 array elements  
« Reply #7 on: Mar 8th, 2008, 4:45pm »
Quote Quote Modify Modify

on Mar 8th, 2008, 4:39pm, mad wrote:
q)Given an array of non negative integers and an integer k, you are required to device an O(n) time algorithm to find out if there exists at least one pair of integers, say i and j, in the given array such that i+j equals k. That is, you are required to return a boolean value.The array is not sorted.

 
Using hashtable we can get O(n) expected time. Is it possible to get O(n) worst case time if elements are arbitrary?
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