Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Find all unique triplets in a given array integers whose sum equal to zero

C Programming Practice: Exercise-8 with Solution

Write a C programming to find all unique triplets in a given array integers whose sum equal to zero.

C Code:

#include <stdio.h>
#include <stdlib.h>
static int compare_two_num(const void *a, const void *b)
{
    return *(int *) a - *(int *) b;
}
static void two_sum(int *nums, int low_pos, int high_pos, int target, int **results, int *ctr)
{
    while (low_pos < high_pos) {
        int diff = target - nums[low_pos];
        if (diff > nums[high_pos]) {
            while (++low_pos < high_pos && nums[low_pos] == nums[low_pos - 1]) {}
        } else if (diff < nums[high_pos]) {
            while (--high_pos > low_pos && nums[high_pos] == nums[high_pos + 1]) {}
        } else {
            results[*ctr] = malloc(3 * sizeof(int));
            results[*ctr][0] = -target;
            results[*ctr][1] = nums[low_pos];
            results[*ctr][2] = nums[high_pos];
            (*ctr)++;
            while (++low_pos < high_pos && nums[low_pos] == nums[low_pos - 1]) {}
            while (--high_pos > low_pos && nums[high_pos] == nums[high_pos + 1]) {}
        }
    }
}
static int** three_Sum(int* nums, int numsSize, int* returnSize)
{
    if (numsSize < 3) {
        return NULL;
    }
    qsort(nums, numsSize, sizeof(*nums), compare_two_num);
    *returnSize = 0;
    int i, j, capacity = 50000;
    int **results = malloc(capacity * sizeof(int *));
    for (i = 0; i < numsSize; i++) {
        if (i == 0 || i > 0 && nums[i] != nums[i - 1]) {
            two_sum(nums, i + 1, numsSize - 1, -nums[i], results, returnSize);
        }
    }
    return results;
}
int main(void)
{
    int i, ctr;
    int nums[] = {-2,0,0,1,1};
	ctr = sizeof(nums) / sizeof(*nums);
  printf("Original Array: ");
   for(i=0; i<ctr; i++)
      {
          printf("%d  ", nums[i]);
      }
    int **result = three_Sum(nums, sizeof(nums) / sizeof(*nums), &ctr);
    for (i = 0; i < ctr; i++) {
		printf("\nUnique triplets of the said array whose sum equal to zero: ");
        printf("%d %d %d\n", result[i][0], result[i][1], result[i][2]);
    }
    return 0;
}

Sample Output:

Original Array: -2  0  0  1  1  
Unique triplets of the said array whose sum equal to zero: -2 1 1

Flowchart: 1

C Programming Flowchart: Find all unique triplets in a given array integers  whose sum equal to zero.

Flowchart: 2

C Programming Flowchart: Find all unique triplets in a given array integers  whose sum equal to zero.

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C programming to convert a given roman number to an integer.
Next: Write a C program to find all unique quadruplets in a given array of integers whose sum equal to zero.

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