easy
0 views

Enclose Number Between its Maximum and Minimum Digits

Enclose a given number between its smallest and largest digits.

Understand the Problem

Problem Statement

In the realm of digital data analytics and number theory, understanding the range and distribution of digits within a number is crucial. This concept finds its roots in various applications, from statistical data visualization to computational algorithms in modern banking systems.

Your programming challenge, inspired by real-world digital data processing scenarios, is to craft an algorithm that can dynamically represent the range of digits within an integer. This method is not just an academic exercise but resonates with practical scenarios like enhancing encryption techniques, optimizing storage in database systems, or even in rapid data analysis platforms dealing with vast streams of numbers.

For this computational task, you’ll accept an integer, N, as your input. Dive into its digits and enclose this number between its smallest and largest digits. This operation transforms the original number, giving a visual cue about its digit span, beneficial for real-time data analytics and digital encryption processes.

Constraints

  • 10 ≤ N ≤ 10^8
  • N is a positive integer
  • Digits are 0-9

Examples

Example 1
Input
4352
Output
243522
Explanation

From the analytical viewpoint, the minimum digit in 4352 is 2, and the maximum zooms to 5. Naturally, enclosing 4352 between these digits results in 243522, a number more informative for computational systems to decipher its digit spectrum.

Example 2
Input
21978
Output
9219789
Explanation

Digital algorithms can quickly discern that the smallest digit in 21978 is 1 and the pinnacle is 9. A calculated enclosure of 21978 between these figures crafts 9219789, a number enriched with contextual information about its original.

Solution

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

int main() {
    char N[10];
    scanf("%s", N);

    char max_digit = '0';
    char min_digit = '9';
    for(int i = 0; N[i]; i++) {
        if(N[i] > max_digit) max_digit = N[i];
        if(N[i] < min_digit) min_digit = N[i];
    }

    printf("%c%s%c", min_digit, N, max_digit);
    return 0;
}
Time:O(n) where n is the number of digits
Space:O(1) - only constant extra space used
Approach:

1. Read the input number as a string to handle each digit individually 2. Initialize max_digit to '0' and min_digit to '9' 3. Loop through each character in the string 4. Update max_digit if current character is greater 5. Update min_digit if current character is smaller 6. Print min_digit + original string + max_digit using printf

Visual Explanation

Loading diagram...
Enclose Number Between its Maximum and Minimum Digits | Letuscrack