There is a company that has a very creative way of managing its accounts. Every time they want to write down a number, they shuffle its digits in the following way: they alternatively write one digit from the front of the number and one digit from the back, then the second digit from the front and the second from the back, and so on until the length of the shuffled number is the same as that of the original.

Write a function

that given a positive integer A, returns its shuffled representation .

for example given A=123456 the function should return 162534.

Given A=130 the function should return 103.

Assume that :

A is a integer within the range[0.. 100,000,000].

In your solution ,focus on correctness. The performance of your solution will not be the focus of the assessment.

C code

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

int solution(int A) {
    char s[20]; // Assuming maximum length of integer as 20 characters
    sprintf(s, "%d", A);

    char result[40]; // Assuming maximum length of result after conversion as 40 characters
    int i = 0, j = strlen(s) - 1;
    int k = 0;

    while (i <= j) {
        result[k++] = s[i++];
        if (i <= j)
            result[k++] = s[j--];
    }
    result[k] = '\0';

    return atoi(result);
}

int main() {
    int A = 130;
    printf("%d\n", solution(A)); // Output: 103
    return 0;
}

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *