m.mirzamo
BAN USER
Questions (2)
Comments (2)
Reputation 330
- 1of 1 vote
AnswersGiven an array of positive integers (excluding zero) and a target number. Detect whether there is a set of consecutive elements in the array that add up to the target.
- m.mirzamo in United States
Example: a = {1, 3, 5, 7, 9}
target = 8
output = true ({3, 5})
or target = 15
output = true : {3, 5, 8}
but if target = 6, output would be false. since 1 and 5 are not next to each other.| Report Duplicate | Flag | PURGE
Facebook Intern Algorithm
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Time O(n), space O(1)
- m.mirzamo December 01, 2015C code:
void sort(int *a, int size)
{
int pivot = 0, runner = 0;
while (runner < size)
{
if (a[runner] < 0)
{
swap (a[runner], a[pivot]);
pivot++;
}
runner++;
}
}