Computer Science, asked by SahejChadha, 5 months ago

Q.4
A class Rearrange has been defined to insert an element and to delete an element
from an array. Some of the members of the class are given below:
Class name : Rearrange
Data members
all : integer type array
n: size of array (integer)
pos1: position of insertion (integer)
pos2: position of deletion
item: item to be inserted (integer)
Member functions
void enter() : to enter size, array elements and to display the entered elements.
void insert(): to accept element (item) to be inserted, position of insertion and
insert the element (item) at the position of insertion.
void disp1(): to display the array after item is inserted.
void disp2(): to display the array after item is deleted.
void remov(): to accept the position of deletion and delete element(item) at the
position of deletion.
Specify the class Rearrange giving details of the functions void enter(), void
insert(), void displo, void disp2() and void remov(). The main function need not
be written.

Answers

Answered by anjumalik4128
2

Answer:

Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max, fourth second min and so on.

Examples:

Input: arr[] = {1, 2, 3, 4, 5, 6, 7}

Output: arr[] = {7, 1, 6, 2, 5, 3, 4}

Input: arr[] = {1, 2, 3, 4, 5, 6}

Output: arr[] = {6, 1, 5, 2, 4, 3}

Expected time complexity: O(n).

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

The idea is to use an auxiliary array. We maintain two pointers one to leftmost or smallest element and other to rightmost or largest element. We more both pointers toward each other and alternatively copy elements at these pointers to an auxiliary array. Finally, we copy auxiliary array back to the original array.

Below image is a dry run of the above approach:

Similar questions