Bird
0
0
DSA Cprogramming~20 mins

Three Sum Problem All Unique Triplets in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Three Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Three Sum Unique Triplets Function
What is the output of the following C function when called with nums = [-1, 0, 1, 2, -1, -4] and numsSize = 6?
DSA C
/* Returns number of triplets found, stores triplets in result array */
int threeSum(int* nums, int numsSize, int*** result) {
    int capacity = 16, count = 0;
    *result = malloc(capacity * sizeof(int*));
    for (int i = 0; i < numsSize - 2; i++) {
        for (int j = i + 1; j < numsSize - 1; j++) {
            for (int k = j + 1; k < numsSize; k++) {
                if (nums[i] + nums[j] + nums[k] == 0) {
                    int triplet[3] = {nums[i], nums[j], nums[k]};
                    // Sort triplet
                    for (int a = 0; a < 2; a++) {
                        for (int b = a + 1; b < 3; b++) {
                            if (triplet[a] > triplet[b]) {
                                int temp = triplet[a];
                                triplet[a] = triplet[b];
                                triplet[b] = temp;
                            }
                        }
                    }
                    // Check for duplicates
                    int duplicate = 0;
                    for (int x = 0; x < count; x++) {
                        if ((*result)[x][0] == triplet[0] && (*result)[x][1] == triplet[1] && (*result)[x][2] == triplet[2]) {
                            duplicate = 1;
                            break;
                        }
                    }
                    if (!duplicate) {
                        if (count == capacity) {
                            capacity *= 2;
                            *result = realloc(*result, capacity * sizeof(int*));
                        }
                        (*result)[count] = malloc(3 * sizeof(int));
                        (*result)[count][0] = triplet[0];
                        (*result)[count][1] = triplet[1];
                        (*result)[count][2] = triplet[2];
                        count++;
                    }
                }
            }
        }
    }
    return count;
}

// Example call and print function omitted for brevity
A[[-1, 0, 1], [0, 1, -1]]
B[[0, 0, 0], [-1, 1, 0]]
C[[-4, 2, 2], [-1, -1, 2]]
D[[-1, -1, 2], [-1, 0, 1]]
Attempts:
2 left
💡 Hint
Think about all unique triplets that sum to zero and how duplicates are avoided.
🧠 Conceptual
intermediate
1:00remaining
Understanding the Time Complexity of Three Sum
What is the time complexity of the naive three sum approach that checks all triplets in an array of size n?
AO(n)
BO(n^3)
CO(n^2)
DO(n log n)
Attempts:
2 left
💡 Hint
Consider how many triplets are checked by three nested loops.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Three Sum Duplicate Check
What is the bug in the following duplicate check code snippet inside the three sum function? for (int x = 0; x < count; x++) { if ((*result)[x][0] == triplet[0] && (*result)[x][1] == triplet[1] && (*result)[x][2] == triplet[2]) { duplicate = 1; break; } } Options:
AIt does not sort the triplet before checking duplicates, so order mismatch causes misses.
BIt compares pointers instead of values, causing false negatives.
CIt only checks the first element of triplets, missing duplicates.
DIt uses an uninitialized variable 'duplicate' causing undefined behavior.
Attempts:
2 left
💡 Hint
Think about how triplets are stored and compared for duplicates.
Predict Output
advanced
2:00remaining
Output of Optimized Three Sum Using Sorting and Two Pointers
What is the output of the following code snippet when nums = [-2, 0, 1, 1, 2]? int nums[] = {-2, 0, 1, 1, 2}; int numsSize = 5; // Assume function threeSumOptimized returns unique triplets sorted internally // and prints them in format: [a, b, c] // Output shown below options
DSA C
void printTriplets(int** triplets, int count) {
    for (int i = 0; i < count; i++) {
        printf("[%d, %d, %d]\n", triplets[i][0], triplets[i][1], triplets[i][2]);
    }
}

// threeSumOptimized implementation omitted for brevity
A[[-2, 0, 1], [0, 1, 1]]
B[[-2, 1, 1], [0, 1, 2]]
C[[-2, 0, 2], [-2, 1, 1]]
D[[0, 0, 0], [-2, 2, 2]]
Attempts:
2 left
💡 Hint
Think about pairs that sum with a number to zero and unique triplets.
🚀 Application
expert
2:30remaining
Number of Unique Triplets Summing to Zero
Given the array nums = [3, -1, -7, 2, -2, 1, 5, -3], how many unique triplets exist that sum to zero?
A4
B6
C5
D3
Attempts:
2 left
💡 Hint
Try to find all sets of three numbers that add to zero without duplicates.