w3resource

C Exercises: Rearrange an array such that even index elements are smaller and odd index elements are greater than their next


104. Rearrange Array: Even Index Elements Smaller, Odd Index Elements Greater

Write a program in C to rearrange an array such that even index elements are smaller and odd index elements are greater than their next.

Expected Output:
The array given is:
6 4 2 1 8 3
The new array after rearranging:
4 6 1 8 2 3

The task is to write a C program that rearranges an array such that elements at even indices are smaller than their next element, and elements at odd indices are greater than their next element. The program should iterate through the array, swap elements as necessary to meet these conditions, and then display the rearranged array.

Sample Solution:

C Code:

#include<stdio.h>

// Function to rearrange the array elements based on specific conditions
void rearrange(int* arr1, int n) 
{ 
    int temp;

    // Loop through the array up to the second last element
    for (int i = 0; i < n - 1; i++) 
    { 
        // If 'i' is even and the current element is greater than the next element, swap them
        if (i % 2 == 0 && arr1[i] > arr1[i + 1]) 
        { 
            temp = arr1[i];
            arr1[i] = arr1[i + 1];
            arr1[i + 1] = temp;
        }
        // If 'i' is odd and the current element is less than the next element, swap them
        if (i % 2 != 0 && arr1[i] < arr1[i + 1]) 
        { 
            temp = arr1[i];
            arr1[i] = arr1[i + 1];
            arr1[i + 1] = temp;
        }
    } 
} 

// Function to display the array elements
void dispArray(int arr1[], int size) 
{ 
    for (int i = 0; i < size; i++) 
        printf("%d  ", arr1[i]); 
    printf("\n"); 
} 

// Main function
int main() 
{ 
    int arr1[] = { 6, 4, 2, 1, 8, 3 }; // Given array
    int n = sizeof(arr1) / sizeof(arr1[0]); // Calculate array size
    printf("The array given is: \n"); 
    dispArray(arr1, n); // Display the given array
    rearrange(arr1, n); // Rearrange the array based on specific conditions
    printf("The new array after rearranging: \n"); 
    dispArray(arr1, n); // Display the rearranged array
    return 0; 
}

Output:

The array given is: 
6  4  2  1  8  3  
The new array after rearranging: 
4  6  1  8  2  3  

Visual Presentation:

C Exercises: Rearrange an array such that even index elements are smaller and odd index elements are greater than their next

Flowchart:/p> Flowchart:  Rearrange an array such that even index elements are smaller and odd index elements are greater than their next

For more Practice: Solve these Related Problems:

  • Write a C program to rearrange an array so that elements at even indices are smaller than their immediate odd-index neighbors.
  • Write a C program to perform in-place rearrangement ensuring even index values are lower than adjacent odd index values.
  • Write a C program to rearrange an array by first sorting and then swapping adjacent pairs to meet the condition.
  • Write a C program to update the array so that every even-index element is the minimum of its adjacent pair and every odd-index element is the maximum.

C Programming Code Editor:



Previous: Write a program in C to update every array element with multiplication of previous and next numbers in array.
Next: Write a program in C to find minimum number of swaps required to gather all elements less than or equals to k.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.