Advertisement
Guest User

SOalgo

a guest
Aug 2nd, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. I am getting a feeling that this will get loads of downvotes, as I dont speak maths(or CS lingo for that matter), but I am just letting out my thoughts on how to extend your approach
  2. I am assuming that
  3.  
  4. 1. Distinct set of integers are required (no repetition)
  5. 2. Whenever sum=x I store the integers that satisfied sum=x and move on, so
  6. that I get all the tuples of integers satisfying sum=x
  7.  
  8. ### Section A ###
  9. k=3 and x=sum
  10.  
  11. Sort the array in ascending order.
  12. Place first_pointer on the first integer and second_pointer on the second integer (i.e. next to first pointer).
  13. Put the third_pointer on the last integer.
  14. 1.Calculate sum=first_pointer + second_pointer + third_pointer.
  15. 2.if sum<x then second_pointer++ Repeat Step 1.
  16. 3.if sum>x then third_pointer-- Repeat Step 1.
  17. 4.Repeat Step 1 to 3 till second_pointer>=third_pointer i.e. the pointers meet.
  18. 5.first_pointer++; second_pointer=first_pointer+1; third_pointer=last integer i.e. shift the first_pointer by one step (on the right) and place second_pointer next to first pointer, return the third pointer to last integer.
  19. 6.Repeat Step 1 to 5, til on 5th step second_pointer=third_pointer.
  20. 7.If you reach till here, without finding any sum=x then there is no solution (I may be wrong here, but I dont see any other possibility.)
  21. <hr />
  22. ### Section B ###
  23. for k=4 and x=sum
  24.  
  25. Sort the array in ascending order.
  26. Place the first two pointers as in **Section A**.
  27. Put the third and fourth one on the last two integers.
  28. i.e. third pointer=second_last_integer and fourth_pointer=last_integer
  29. 1.Similar to **Section A Step 1** sum=pointer(1st+2nd+3rd+4th)
  30. 2.Similar to **Section A Step 2**
  31. 3.(_This is different_)if sum>x then fourth_pointer--
  32. 4.Similar to **Section A Step 4**
  33. 5.(_now on its totally different_) I will divide this as 5.a and 5.b for clarity
  34. 5.a This part is similar to **Section A** in the sense that you will shift the first and second pointer keeping them next to one another and repeating steps 1 to 4 of **Section B**
  35. 5.b If you did not find the sum yet fourth_pointer--; third_pointer=fourth_pointer-1 and repeat Steps 1 to 4 **and 5.a**
  36. 6. Repeat 5.b till second_pointer=third_pointer.
  37. If you have reached till here without any sum=x, then there is no solution //standard-disclaimer
  38. <hr />
  39. ### Section C ###
  40. (I am really falling short screen space, as I cant see preview of what I am writing. So I will keep it short and leave it upto your imagination)
  41. for any k and x=sum
  42.  
  43. Sort the array in ascending order.
  44. Place first int(k/2) pointers in the beginning.
  45. Place the rest k-int(k/2) pointers at the end.
  46. 1.Calculate sum=sigma(k).
  47. 2.if sum<x then pointer(int(k/2))++ Repeat from Step 1.
  48. 3.if sum>x then pointer(k-int(k/2))-- Repeat from Step 1.
  49. 4.Repeat from Step 1 till pointer(int(k/2))>=pointer(k-int(k/2)).
  50. 5.Now take the [int(k/2)-1,int(k/2),(k-int(k/2)),(k-int(k/2)+1)] pointers and proceed similar to **Section B**.
  51. 6.Repeat above by shifting int(k/2)-2 to the right, then by shifting (k-int(k/2)+2) to the left.
  52. 7.Extend Step 6 till you shift all pointers<int(k/2) so that they just overlap
  53.  
  54. ## THE END ##
  55. //phew !! wrote so much !!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement