0% found this document useful (0 votes)
7 views4 pages

Array

Uploaded by

Impana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Array

Uploaded by

Impana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12)You have an array A of integers of size N, an array B ( initially empty) and a stack (initially empty) .

You are allowed to do the following operations:

● Take the first element of array A and push it into S and remove it from A

● Take the top element from stack S, append it to the end of array B and replace it from S. You have
to tell if it possible to move all the elements of array A to array B using above operations such that
finally the array B is sorted in ascending order.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_SIZE 1000

// Stack structure
typedef struct {
int items [MAX_SIZE];
int top;
} Stack;

// Function to initialize the stack


void init Stack(Stack* s) {
s->top = -1;
}

// Function to push an element onto the stack


bool push (Stack* s, int item) {
if (s->top >= MAX_SIZE - 1) return false; // Stack overflow
s->items[++(s->top)] = item;
return true;
}

// Function to pop an element from the stack


int pop (Stack* s) {
if (s->top == -1) return -1; // Stack underflow
return s->items[(s->top)--];
}

// Function to check if B can be sorted


bool can Sort (int A[], int N) {
Stack s;
Init Stack(&s);
int nextExpected = 1; // Next expected number in sorted B

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


// Push elements from A to stack
push(&s, A[i]);

// While stack is not empty and the top of the stack is the next
expected
while (s.top != -1 && s.items[s.top] == nextExpected) {
pop(&s);
nextExpected++;
}
}

// After processing all elements in A, check the remaining elements in


the stack
while (s.top != -1) {
if (s.items[s.top] != nextExpected) {
return false; // If the top of the stack is not the expected
number
}
pop(&s);
nextExpected++;
}

return true; // All elements can be sorted into B


}

int main() {
int N;
printf("Enter the number of elements in array A: ");
scanf("%d", &N);

int A[MAX_SIZE];
printf("Enter the elements of array A: ");
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}

if (canSort(A, N)) {
printf("Yes, it is possible to sort array B.\n");
} else {
printf("No, it is not possible to sort array B.\n");
}

return 0;
}

ALGORITHM:

• Data Structures:
• A stack is used to temporarily hold elements as they are transferred from array A to
array B.
• An array A contains the input integers, and an array B is implied to be sorted but not
explicitly maintained in the code.

• Initialization:

• A stack is initialized with a top pointer set to -1, indicating it is empty.


• nextExpected is initialized to 1, which represents the next expected number to be
sorted into B.

• Processing:

• Loop through each element in array A:


o Push the current element from A onto the stack.
o While the stack is not empty and the top element equals nextExpected, pop
the element from the stack and increment nextExpected.
• After processing all elements in A, check if the remaining elements in the stack can be
popped in order of nextExpected.

• Return Result:

• If all elements can be popped in the expected order, return true (indicating B can be
sorted). Otherwise, return false.

TRACING:

1. Initialization:
o The program reads N = 5.
o It initializes array A with the values [1, 2, 3, 4, 5].
o A stack s is initialized, and nextExpected is set to 1.
2. Processing Elements:
o Loop through each element in A:

Iteration 1: Processing A[0] = 1

• Push 1 onto the stack.


o Stack: [1]
• Check the stack:
o The top of the stack (1) matches nextExpected (1), so pop 1.
o Stack after pop: []
o Increment nextExpected to 2.

Iteration 2: Processing A[1] = 2

• Push 2 onto the stack.


o Stack: [2]
• Check the stack:
o The top of the stack (2) matches nextExpected (2), so pop 2.
o Stack after pop: []
o Increment nextExpected to 3.

Iteration 3: Processing A[2] = 3

• Push 3 onto the stack.


o Stack: [3]
• Check the stack:
o The top of the stack (3) matches nextExpected (3), so pop 3.
o Stack after pop: []
o Increment nextExpected to 4.

Iteration 4: Processing A[3] = 4

• Push 4 onto the stack.


o Stack: [4]
• Check the stack:
o The top of the stack (4) matches nextExpected (4), so pop 4.
o Stack after pop: []
o Increment nextExpected to 5.

Iteration 5: Processing A[4] = 5

• Push 5 onto the stack.


o Stack: [5]
• Check the stack:
o The top of the stack (5) matches nextExpected (5), so pop 5.
o Stack after pop: []
o Increment nextExpected to 6.

3. Final Check:

• After processing all elements in A, the stack is empty.


• Now check the remaining elements in the stack (none in this case):
o Since the stack is empty, we confirm that all elements were processed in the
expected order.

You might also like