c code to replace with next greatest element

  • cNGE
  • write a c code to replace with next greatest element.

    Write a program to replace every element in the dynamic array with the next greatest element present in the same array

    Code

    #include <stdio.h> #include <stdlib.h> #include <string.h> void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void printarray(int *arr, int n) { for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); } void nge(int *arr, int n) { int copy[n],k=0; for (int i = 0; i < n; i++) { int j; check: j = i + 1; while (j < n) { if (arr[j] > arr[i]) { copy[k]=arr[j]; k++; i++; goto check; } j++; } copy[k]=-1; k++; } printarray(copy,n); } int main() { int n, a, b; printf("enter the total elements of array\n"); scanf("%d", &n); int *arr = (int *)malloc(n * (sizeof(int))); for (int i; i < n; i++) { printf("enter the element %d :\n", i + 1); scanf("%d", &arr[i]); } printarray(arr, n); nge(arr, n); return 0; }