C Exercises: Get the indices of the two numbers of a given array of integers
C Programming Practice: Exercise-1 with Solution
Write a C programming to get the indices of the two numbers of a given array of integers, such that the sum of the two numbers equal to a specific target.
C Code:
#include <stdio.h>
#include <stdlib.h>
struct object {
int val;
int index;
};
static int compare(const void *a, const void *b)
{
return ((struct object *) a)->val - ((struct object *) b)->val;
}
static int * two_sum(int *nums, int nums_size, int target)
{
int i, j;
struct object *objes = malloc(nums_size * sizeof(*objes));
for (i = 0; i < nums_size; i++) {
objes[i].val = nums[i];
objes[i].index = i;
}
qsort(objes, nums_size, sizeof(*objes), compare);
int *results = malloc(2 * sizeof(int));
i = 0;
j = nums_size - 1;
while (i < j) {
int diff = target - objes[i].val;
if (diff > objes[j].val) {
while (++i < j && objes[i].val == objes[i - 1].val) {}
} else if (diff < objes[j].val) {
while (--j > i && objes[j].val == objes[j + 1].val) {}
} else {
results[0] = objes[i].index;
results[1] = objes[j].index;
return results;
}
}
return NULL;
}
int main(void)
{
int nums[] = { 4, 2, 1, 5 };
int ctr = sizeof(nums) / sizeof(*nums);
int target = 7;
int i;
printf("Original Array: ");
for(i=0; i<ctr; i++)
{
printf("%d ", nums[i]);
}
printf("\nTarget Value: %d", target);
int *indexes = two_sum(nums, ctr, target);
if (indexes != NULL) {
printf("\nIndices of the two numbers whose sum equal to target value: %d", target);
printf("\n%d %d\n", indexes[0], indexes[1]);
}
else
{
printf("Not found or matched\n");
}
return 0;
}
Sample Output:
Original Array: 4 2 1 5 Target Value: 7 Indices of the two numbers whose sum equal to target value: 7 1 3
Pictorial Presentation:
Flowchart:
C Programming Code Editor:
Contribute your code and comments through Disqus.
Previous: C programming Exercises Introduction
Next: Write a C program to find length of the longest substring of a given string without repeating characters.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework