algorithm - Sum of a number with any k elements in Array -
Design an algorithm, that a set of n integers and other integer x determines that there exists Is or not Kashmir (n> k> 2) The element in S. whose sum is exactly x. Please give your algorithm its time to run
I am preparing for an interview, and I have come into this algorithm. I have solved the problems where the Kashmir problem has been specified. Like 2 or 3 But I can not find any answer where I can solve for any Kashmir. I tried to resolve it using dynamic programming but did not get the result. Nobody can help me on this.
you int
array CTT
size X
, then go through the set, and mark the reachable digits -1
are all set in element except for zero, which is set to zero.
Repeat this process for each element
i
on cnt
for every element of s
Non-negative, check the element cnt. [I + si]
(if it's within range of array). If so, set it to cnt [si + i] = max (cnt [i] +1, cnt [si + i])
.
Once you all go through the S
element, check cnt [x]
. If it is set to two or more, then a combination of two or more elements is present by adding s
to s
above.
This algorithm is proxy-polynomial, with time the o (x * | S |)
Comments
Post a Comment