Algorithm for finding a sum within an array? [closed] - sum

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Say you have some array with elements that you know are all positive, and smaller than a number N.
Can someone give me a general, high-level description of an algorithm to find out whether there is some subset within the array in which all the elements add up exactly to N?
It doesn't need to be particularly efficient; the set I'm working with is very small.

If efficiency is not important, an algorithm at a high-level is:
input: array A[1..K] of positive numbers, positive N
for each subset S of { 1..K }
sum = 0
for each i in S
sum = sum + A[i]
end
if (sum equals N) return true
end
return false

Pseudocode. Extremely inefficient.
if empty(numbers) return false
return hasSumSubset(numbers, N)
boolean hasSumSubset(numbers[], N):
p = pop(numbers)
if empty(numbers) return N == p
return hasSumSubset(numbers, N-p) or hasSumSubset(numbers, N)
If you actually implement this make sure numbers is copied (not passed in by ref) for the recursive calls; otherwise it won't work correctly.

Related

Sieve Of Sundaram [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm need someone to walk me through the Sieve of Sundaram (wiki). Lets say I have a list of Integers, (41,43,47,49,51,53,59) in an array.
Now the above mentioned wikipedia explanation mentions: *
An odd integer is excluded from the final list if and only if it is of
the form 2(i+j+2ij)+1
*. I am trying to understand how to achieve this in a program (preferably VBA). Such that the output will be (41,43,47,53,59).
Explanations sincerely appreciated.
Regards,
I discuss the Sieve of Sundaram at my blog. The algorithm is given by the following pseudocode:
function sundaram(n)
m := n // 2
sieve := makeArray(m+1, True)
for i from 1 to m // 4
for j from i to (m-i) // (2*i+1)
sieve[i+j+2*i*j] := False
ps := [2]
for i from 1 to m
if sieve[i]
append 2*i+1 to ps
return ps
As per the WP article, after removing some numbers from a range k=1...n, we get the results as m=2k+1. That means that all ms are odd.
Now, an odd composite must be a product of two odd numbers: 2k+1 = (2i+1)(2j+1) = 4ij+2i+2j+1 (if one of them were even, the product would be even too). But that means that k = 2ij + i + j. Hence the sieve's operation.
As nicely explained here.

How to test my solution (contest)? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I made a code for contest, I am getting 'WA' but I am sure that my code is correct . So before I complain admin, how could I be sure that my code is actually correct and not that problem test data is incorrect? Here's the question and this is my solution in c++ , so can anyone suggest methods of testing one's own solution or can anybody supply test input where my solution produces wrong answer?
Oh , god you should check properly , i found another input which gives wrong output ( i told you usually test inputs are correct and we are usually wrong :D) . For input "1 2 1 3 4 2" your code outputs "1 4 2" which is certainly wrong , it should be "2 4 2" .
You have several examples inside the question explanation you can use to test your code:
For instance, the alternating depth of (([]))[[[()]]] is 2, the
maximum number of symbols between a matched pair () is 6 and the
maximum number of symbols between a matched pair [] is 8.
And
Sample Input
14
1 1 3 4 2 2 3 3 3 1 2 4 4 4
Sample Output
2 6 8
You can also invent some yourself, count the depth and maximum number of symbols and use that as input.
Moreover, it is said in the question that
The time limit for this task is 1 second. The memory limit is 32MB.
Are you sure your code didn't take more than a second on one of the very big inputs, with N is close to 10^5?
If the tests don't pass, its highly probable that the problem is in your code and not in the automated checker.

I need to check the X & Y points of a CGPoint for an image, then show an alert [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm making a game where images fall from the top of the screen, and I need to detect when they reach/go past a certain x & y value. I tried doing:
if (asteroid.center.y == 296 && asteroid.center.x == 50) {
// etc...displayed alert
}
But for some reason, it won't work. Thanks in advance!
It depends on what type of object "asteroid" is. If it is an instance of UIView, you should be able to get the position from the UIView's frame property:
if (asteroid.frame.origin.y >= 296 && asteroid.frame.origin.x >= 50) {
// etc...displayed alert
}
frame is a CGRect reference. You can learn more about it here: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference/reference.html#//apple_ref/doc/c_ref/CGRect
EDIT:
If you wanted to check the center of the asteroid as oppose to the top/left, it would look like this:
if (asteroid.frame.origin.y + asteroid.frame.size.height / 2 >= 296 && asteroid.frame.origin.x + asteroid.frame.size.width / 2 >= 50) {
// etc...displayed alert
}

Hybrid Functional/Object Oriented languages like F#/Scala: Use types with methods or methods and types [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
So to add onto what I learned from this question I asked:
F# Records: Dangerous, only for limited use, or well used functionality?
This got me thinking about whether I should be using Types with methods or Records(?) AND methods. For example, this is a typical type I might make:
(* User Interaction Start *)
type UserInteraction public() =
member public x.CreatePassword(originalPassword) =
if String.IsNullOrEmpty originalPassword then
raise(new ArgumentException "CreatePassword: password is null or empty")
let hashUtility = new HashUtility();
hashUtility.CreateHash originalPassword
The problem I could see with this is there's a possibility that something within that UserInteraction type is mutable due that being allowed. Now with record types, as far as I understand, they are guaranteed to be immutable.
Would it be better design a module with methods that take in or return record types only or even go one step further with method chaining?
You may want to read When to Use Classes, Unions, Records, and Structures [MSDN] (bottom of the page). You appear to have some incorrect assumptions about records. For instance, they can have mutable fields just like classes. The important differences are inheritance, pattern matching, constructors, hidden members, equality, and copy-and-update expressions.
Just noticed one small error on that page: records can, in fact, implement interfaces.
Another possibly helpful section: Differences Between Records and Classes [MSDN] (bottom of page)
Note that you can create a member on an immutable type returning a new modified instance of the type which is a perfectly reasonable thing to do:
type Person =
{ Name : string; Age : int }
member x.GrowOlder n =
{ x with Age = x.Age + n }
let q = { Name = "David"; Age = 29 };;
q.GrowOlder 1
It is interesting to note that in the following C# code:
var x = 0;
var y = 1;
var z = x + y;
You wouldn't expect neither x nor y to be mutated by the use of the operator +, that is + returns a new value and does not mutate x nor y. You can generally consider designing everything with that in mind.

Objective-c random point which lies in given cgrect [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
my requirement is generating a random point in a given area, i.e i have an CGRect of some space and i need to generate a rendom point in this rect ..
how can i proceed in this scenario ??
You should generate 2 random values that lie in range of your rect.
Eg. a rect like this (100,50,300,200) will require you to get an x-value between 0 and 300 (your width) and then add 100 to it (your origin). Same will be required for y-value, get a random value between 0 and 200 (height) and add 50 (origin) to it.