Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

sum - An algorithm to determine a subset sequence in O(n)?

How can i aproach this problem by induction?

Suppose that you are given an algorithm as a black box you cannot see how it is designed it has the following properties: if you input any sequence of real numbers and an integer k, the algorithm will answer YES or NO indicating whether there is a subset of numbers whose sum is exactly k. Show how to use this black box to find the subset of a given sequence {X1, …., Xn} whose sum is k. You can use the black box O(n) times.

Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not entirely convinced that induction is necessary here.

Here's my two cents:

Suppose you have a sequence of numbers S, and integer k, and you already know that there exists a subset of numbers whose sum is k. Now, remove a number from your sequence (call it i), and use your black box on what remains (using the same k as before).

  • If the algorithm returns YES on the new sequence, what does that tell you about i and any subset of S whose sum is k?
  • If the algorithm returns NO on the new sequence, what does that tell you about i and any subset of S whose sum is k?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...