Largest Number - C Solution

1. Introduction

This blog post tackles an intriguing problem in C programming, which involves arranging a list of non-negative integers to form the largest possible number. This task is not just about sorting numbers, but about understanding how number combinations can be optimized to form the largest value, a problem that has implications in algorithms and combinatorics.

Problem

Given a list of non-negative integers 'nums', the goal is to arrange them in such a way that they form the largest number possible. The challenge lies in concatenating the numbers in the optimal order. Since the resulting number can be very large, it should be returned as a string.

2. Solution Steps

1. Convert the integers to strings for easy concatenation.

2. Define a custom comparator for sorting that compares concatenated string pairs.

3. Sort the array of string numbers using this comparator.

4. Concatenate the sorted string numbers to form the final result.

5. Handle edge cases like leading zeros.

3. Code Program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Comparator function for sorting
int compare(const void *a, const void *b) {
    char first[24], second[24];
    strcpy(first, *(const char**)a);
    strcat(first, *(const char**)b);
    strcpy(second, *(const char**)b);
    strcat(second, *(const char**)a);
    return strcmp(second, first); // Sort in descending order of concatenated strings
}

// Function to convert integers to strings
void intToString(int *nums, char **numsStr, int numsSize) {
    for (int i = 0; i < numsSize; i++) {
        numsStr[i] = malloc(12 * sizeof(char)); // Allocate memory for each number string
        sprintf(numsStr[i], "%d", nums[i]); // Convert integer to string
    }
}

// Main function to form the largest number
char *largestNumber(int *nums, int numsSize) {
    char **numsStr = malloc(numsSize * sizeof(char*)); // Array of number strings
    intToString(nums, numsStr, numsSize); // Convert integers to strings

    qsort(numsStr, numsSize, sizeof(char*), compare); // Sort the number strings

    // Check for leading zeros case
    if (strcmp(numsStr[0], "0") == 0) {
        free(numsStr);
        return "0";
    }

    char *result = malloc(numsSize * 12 * sizeof(char)); // Allocate memory for the result string
    result[0] = '\0'; // Initialize result as an empty string

    for (int i = 0; i < numsSize; i++) {
        strcat(result, numsStr[i]); // Concatenate strings in sorted order
        free(numsStr[i]); // Free each number string
    }

    free(numsStr); // Free the array of number strings
    return result; // Return the largest number as a string
}

int main() {
    int nums[] = {3, 30, 34, 5, 9}; // Example input
    char *result = largestNumber(nums, 5); // Call the function
    printf("Output: %s\n", result); // Print the result

    free(result); // Free the result string
    return 0;
}

Output:

"9534330"

Explanation:

The program first converts the integers in the array to strings. 

Then, it sorts these strings based on a custom comparator that checks which combination of two numbers would yield a larger number when concatenated. 

After sorting, the program concatenates these strings to form the final largest number. 

In the case of input [3, 30, 34, 5, 9], the sorted order is [9, 5, 34, 3, 30], resulting in the largest number "9534330". 

Special attention is given to edge cases, such as when the array consists of all zeros, to ensure the correct result is returned.


Comments