My issue
why its show wrong answer for the hidden case
My code
#include <stdio.h>
void findNextSmallerElements(int arr[], int n, int result[]) {
int stack[n];
int top = -1; // Stack initialization (empty)
// Traverse the array from right to left
for (int i = n - 1; i >= 0; i--) {
// Pop elements from the stack that are not smaller
while (top != -1 && stack[top] >= arr[i]) {
top--;
}
// If stack is empty, no smaller element exists
result[i] = (top == -1) ? -1 : stack[top];
// Push the current element onto the stack
stack[++top] = arr[i];
}
}
int main() {
// Hardcoded input
int arr[] = {1, 3, 5, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int result[n];
// Function to find the next smaller elements
findNextSmallerElements(arr, n, result);
// Output the result
// printf("Next Smaller Elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
Learning course: Design and analysis of Algorithms
Problem Link: https://www.codechef.com/learn/course/abesit-daa/ABESITDA06/problems/STACK14