How to use C to solve Two Sum correctly? [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
Two Sum Problem:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Code:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
int *a = malloc(sizeof(int) * 2);
for(int i = 0; i < numsSize; i++)
for(int j = i+1; j < numsSize; j++)
if (nums[i] + nums[j] == target)
a[0] = i;
a[1] = j;
return a;
/* Error: control reaches end of non-void function [-Werror=return-type] */
c interview-questions
closed as off-topic by 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno Apr 13 at 15:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno
add a comment |Â
up vote
-1
down vote
favorite
Two Sum Problem:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Code:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
int *a = malloc(sizeof(int) * 2);
for(int i = 0; i < numsSize; i++)
for(int j = i+1; j < numsSize; j++)
if (nums[i] + nums[j] == target)
a[0] = i;
a[1] = j;
return a;
/* Error: control reaches end of non-void function [-Werror=return-type] */
c interview-questions
closed as off-topic by 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno Apr 13 at 15:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno
Are you asking about the error? (your title suggest the same). The error here is quite straight forward: Ifnums
simply do not contain a combination that adds totarget
, then the method lacks a return statement. You could e.g. return-1
twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this.
â Flater
Apr 13 at 10:04
@Flater Thank you! Very useful comment.
â user167056
Apr 13 at 12:18
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Two Sum Problem:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Code:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
int *a = malloc(sizeof(int) * 2);
for(int i = 0; i < numsSize; i++)
for(int j = i+1; j < numsSize; j++)
if (nums[i] + nums[j] == target)
a[0] = i;
a[1] = j;
return a;
/* Error: control reaches end of non-void function [-Werror=return-type] */
c interview-questions
Two Sum Problem:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Code:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
int *a = malloc(sizeof(int) * 2);
for(int i = 0; i < numsSize; i++)
for(int j = i+1; j < numsSize; j++)
if (nums[i] + nums[j] == target)
a[0] = i;
a[1] = j;
return a;
/* Error: control reaches end of non-void function [-Werror=return-type] */
c interview-questions
edited Apr 13 at 9:43
asked Apr 13 at 7:59
user167056
43
43
closed as off-topic by 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno Apr 13 at 15:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno
closed as off-topic by 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno Apr 13 at 15:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Toby Speight, Stephen Rauch, Sam Onela, Dannnno
Are you asking about the error? (your title suggest the same). The error here is quite straight forward: Ifnums
simply do not contain a combination that adds totarget
, then the method lacks a return statement. You could e.g. return-1
twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this.
â Flater
Apr 13 at 10:04
@Flater Thank you! Very useful comment.
â user167056
Apr 13 at 12:18
add a comment |Â
Are you asking about the error? (your title suggest the same). The error here is quite straight forward: Ifnums
simply do not contain a combination that adds totarget
, then the method lacks a return statement. You could e.g. return-1
twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this.
â Flater
Apr 13 at 10:04
@Flater Thank you! Very useful comment.
â user167056
Apr 13 at 12:18
Are you asking about the error? (your title suggest the same). The error here is quite straight forward: If
nums
simply do not contain a combination that adds to target
, then the method lacks a return statement. You could e.g. return -1
twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this.â Flater
Apr 13 at 10:04
Are you asking about the error? (your title suggest the same). The error here is quite straight forward: If
nums
simply do not contain a combination that adds to target
, then the method lacks a return statement. You could e.g. return -1
twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this.â Flater
Apr 13 at 10:04
@Flater Thank you! Very useful comment.
â user167056
Apr 13 at 12:18
@Flater Thank you! Very useful comment.
â user167056
Apr 13 at 12:18
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I would have done this:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define INVALID (-1)
int* twoSum(int* nums, int numsSize, int target)
int *result = malloc(sizeof(int) * 2);
memset (result, INVALID, sizeof(int) * 2);
for(int left = 0; left < numsSize && result[0] == INVALID; left++)
for(int right = left+ 1; right < numsSize && result[0] == INVALID; right++)
if (nums[left] + nums[right] == target)
result[0] = left;
result[1] = right;
/* A return statement here instead of the overhead of the
second check in the for loop conditions may be a good
idea, it depends if the driving factor is performance
or maintainability */
return result;
Since this is an interview question and you are showing what a shining star you are you might want to consider the following points:
Its best to avoid multiple return points if you can, but there is
going to be a performance hit. By adding the comment I think I have
shown that I have identified that issue and, right or wrong, made a
decision and explained why.Setting the results as early as possible may be a waste of a few
processor cycles but it will help when the code goes wrong, you know
you are going to have random values sneaking out, imagine what would
happen ifresults[0] = 5;
Using single letter variables means you have to right more
comments/documents to explain your code. This is an interview
question, you don't want to submit a 20 page document detailing what
a, i, j mean and their range of values do you. (left and right are
not very descriptive names, but they are better than i and j).
All I did was add one line and polish the turd a bit, which is much easier when I'm not being interviewed, good luck !
Quite good. Thank you!
â user167056
Apr 13 at 12:19
1
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I would have done this:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define INVALID (-1)
int* twoSum(int* nums, int numsSize, int target)
int *result = malloc(sizeof(int) * 2);
memset (result, INVALID, sizeof(int) * 2);
for(int left = 0; left < numsSize && result[0] == INVALID; left++)
for(int right = left+ 1; right < numsSize && result[0] == INVALID; right++)
if (nums[left] + nums[right] == target)
result[0] = left;
result[1] = right;
/* A return statement here instead of the overhead of the
second check in the for loop conditions may be a good
idea, it depends if the driving factor is performance
or maintainability */
return result;
Since this is an interview question and you are showing what a shining star you are you might want to consider the following points:
Its best to avoid multiple return points if you can, but there is
going to be a performance hit. By adding the comment I think I have
shown that I have identified that issue and, right or wrong, made a
decision and explained why.Setting the results as early as possible may be a waste of a few
processor cycles but it will help when the code goes wrong, you know
you are going to have random values sneaking out, imagine what would
happen ifresults[0] = 5;
Using single letter variables means you have to right more
comments/documents to explain your code. This is an interview
question, you don't want to submit a 20 page document detailing what
a, i, j mean and their range of values do you. (left and right are
not very descriptive names, but they are better than i and j).
All I did was add one line and polish the turd a bit, which is much easier when I'm not being interviewed, good luck !
Quite good. Thank you!
â user167056
Apr 13 at 12:19
1
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
add a comment |Â
up vote
0
down vote
accepted
I would have done this:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define INVALID (-1)
int* twoSum(int* nums, int numsSize, int target)
int *result = malloc(sizeof(int) * 2);
memset (result, INVALID, sizeof(int) * 2);
for(int left = 0; left < numsSize && result[0] == INVALID; left++)
for(int right = left+ 1; right < numsSize && result[0] == INVALID; right++)
if (nums[left] + nums[right] == target)
result[0] = left;
result[1] = right;
/* A return statement here instead of the overhead of the
second check in the for loop conditions may be a good
idea, it depends if the driving factor is performance
or maintainability */
return result;
Since this is an interview question and you are showing what a shining star you are you might want to consider the following points:
Its best to avoid multiple return points if you can, but there is
going to be a performance hit. By adding the comment I think I have
shown that I have identified that issue and, right or wrong, made a
decision and explained why.Setting the results as early as possible may be a waste of a few
processor cycles but it will help when the code goes wrong, you know
you are going to have random values sneaking out, imagine what would
happen ifresults[0] = 5;
Using single letter variables means you have to right more
comments/documents to explain your code. This is an interview
question, you don't want to submit a 20 page document detailing what
a, i, j mean and their range of values do you. (left and right are
not very descriptive names, but they are better than i and j).
All I did was add one line and polish the turd a bit, which is much easier when I'm not being interviewed, good luck !
Quite good. Thank you!
â user167056
Apr 13 at 12:19
1
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I would have done this:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define INVALID (-1)
int* twoSum(int* nums, int numsSize, int target)
int *result = malloc(sizeof(int) * 2);
memset (result, INVALID, sizeof(int) * 2);
for(int left = 0; left < numsSize && result[0] == INVALID; left++)
for(int right = left+ 1; right < numsSize && result[0] == INVALID; right++)
if (nums[left] + nums[right] == target)
result[0] = left;
result[1] = right;
/* A return statement here instead of the overhead of the
second check in the for loop conditions may be a good
idea, it depends if the driving factor is performance
or maintainability */
return result;
Since this is an interview question and you are showing what a shining star you are you might want to consider the following points:
Its best to avoid multiple return points if you can, but there is
going to be a performance hit. By adding the comment I think I have
shown that I have identified that issue and, right or wrong, made a
decision and explained why.Setting the results as early as possible may be a waste of a few
processor cycles but it will help when the code goes wrong, you know
you are going to have random values sneaking out, imagine what would
happen ifresults[0] = 5;
Using single letter variables means you have to right more
comments/documents to explain your code. This is an interview
question, you don't want to submit a 20 page document detailing what
a, i, j mean and their range of values do you. (left and right are
not very descriptive names, but they are better than i and j).
All I did was add one line and polish the turd a bit, which is much easier when I'm not being interviewed, good luck !
I would have done this:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define INVALID (-1)
int* twoSum(int* nums, int numsSize, int target)
int *result = malloc(sizeof(int) * 2);
memset (result, INVALID, sizeof(int) * 2);
for(int left = 0; left < numsSize && result[0] == INVALID; left++)
for(int right = left+ 1; right < numsSize && result[0] == INVALID; right++)
if (nums[left] + nums[right] == target)
result[0] = left;
result[1] = right;
/* A return statement here instead of the overhead of the
second check in the for loop conditions may be a good
idea, it depends if the driving factor is performance
or maintainability */
return result;
Since this is an interview question and you are showing what a shining star you are you might want to consider the following points:
Its best to avoid multiple return points if you can, but there is
going to be a performance hit. By adding the comment I think I have
shown that I have identified that issue and, right or wrong, made a
decision and explained why.Setting the results as early as possible may be a waste of a few
processor cycles but it will help when the code goes wrong, you know
you are going to have random values sneaking out, imagine what would
happen ifresults[0] = 5;
Using single letter variables means you have to right more
comments/documents to explain your code. This is an interview
question, you don't want to submit a 20 page document detailing what
a, i, j mean and their range of values do you. (left and right are
not very descriptive names, but they are better than i and j).
All I did was add one line and polish the turd a bit, which is much easier when I'm not being interviewed, good luck !
edited Apr 13 at 14:22
answered Apr 13 at 12:03
Code Gorilla
1,08228
1,08228
Quite good. Thank you!
â user167056
Apr 13 at 12:19
1
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
add a comment |Â
Quite good. Thank you!
â user167056
Apr 13 at 12:19
1
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
Quite good. Thank you!
â user167056
Apr 13 at 12:19
Quite good. Thank you!
â user167056
Apr 13 at 12:19
1
1
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
@pacmaninbw - Thanks, copy and paste error, now fixed.
â Code Gorilla
Apr 13 at 14:22
add a comment |Â
Are you asking about the error? (your title suggest the same). The error here is quite straight forward: If
nums
simply do not contain a combination that adds totarget
, then the method lacks a return statement. You could e.g. return-1
twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this.â Flater
Apr 13 at 10:04
@Flater Thank you! Very useful comment.
â user167056
Apr 13 at 12:18