Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Data Structure -- Arrays -- Odd Number Of Elements
varalu
post Nov 13 2007, 11:02 AM
Post #1


Super Member
*********

Group: [HOSTED]
Posts: 305
Joined: 1-October 07
From: India
Member No.: 50,968



Given an array of elements with many numbers occurring even number of times and two numbers occurring odd number of times. Find out the two numbers that occur odd number of times.

example:

Elements in array -- 14433446

The expected result is 1 and 6


One solution
1. Find max of the array
2. Hash Function : element/max value
3. Repeat to all elements...
4. Find frequency... yo will get the 2 elements with odd frequency.

but this is not the optimal....
Do find more solutions to this and post it. Look for time and space complexity.


Another Solution
one more solution
1. Scan through the array
2. Insert the element into a BST if its already not there. If its already avl, delete it from BST.
3. Repeat for all the elements
4. Now, BST will have only the elements with odd occurrence.

Time complexity : O(n*log n)
Courtesy: Varun. (friend)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Limiting Returned Data To Last X Fields With Sql(2)
  2. Data Structure Questions(4)
  3. Data Structures -- Binary Tree -- Mirror Image(0)
  4. Data Structures -- Linked List(8)
  5. Data Structures -- Binary Tree -- Structurally Same(4)
  6. Data Structures -- Linked List -- Reverse(4)
  7. Data Structures -- String -- Palindrome(5)
  8. Data Structures -- Linked List -- Point Of Merging(2)
  9. Data Structures -- String -- Arrange Based On Repetition(1)
  10. Data Structures -- Expression Trees(0)
  11. Data Structure -- Trees -- Threaded Binary Tree(0)
  12. Data Structure -- Queue -- Implement Using Stack(1)
  13. Data Structure -- Permutations(1)


 



- Lo-Fi Version Time is now: 5th September 2008 - 06:17 AM