tcs1b

 #include <stdio.h>


// Function to find minimum swaps for beautiful array

int min_swaps_to_beautiful(int arr[], int n) {

    int swaps_asc = 0, swaps_desc = 0;


    for (int i = 0; i < n - 1; i++) {

        for (int j = 0; j < n - i - 1; j++) {

            if (arr[j] > arr[j + 1]) {

                // Swap for ascending order

                int temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

                swaps_asc++;

            }

        }

        if (arr[i] < arr[i + 1]) {

            // Swap for descending order

            int temp = arr[i];

            arr[i] = arr[i + 1];

            arr[i + 1] = temp;

            swaps_desc++;

        }

    }


    return (swaps_asc < swaps_desc) ? swaps_asc : swaps_desc;

}


int main() {

    int n;

    scanf("%d", &n);


    int arr[n];

    for (int i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }


    // Calculate minimum swaps for beautiful array

    int result = min_swaps_to_beautiful(arr, n);


    printf("%d\n", result);


    return 0;

}


Comments

Popular posts from this blog

tcs1dsu