william.watts
BAN USER
Questions (1)
Comments (2)
Reputation 50
- 0of 0 votes
AnswersGiven a List determine if contiguous elements of the List sum to an input number. For example: Array/List [6 5 3 2 1 7], and input number 8. The numbers 5 + 3 = 8. Or suppose an input number 10, the elements of the list 2 + 1 + 7 = 10.
- william.watts in United States| Report Duplicate | Flag | PURGE
Facebook Senior Software Development Engineer Algorithm
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
O(n^2) brute force solution:
int adjacent(int list[], int length, int number)
{
for(int i = 0; i < length; i++){
int helper = 0;
for(int j = 0; j < length; j++){
helper = helper + list[i+j];
if( helper == number) {
return 1;
}
}
}
return 0;
}
int main(void)
{
int list[] = {6, 3, 5, 3, 2, 22, 3, 4, 100, 6, 7, 10, 11};
int length = sizeof(list)/sizeof(*list);
printf("\n%d\n", adjacent(list,length, 31));
return 0;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
O(n) solution
- william.watts December 19, 2017