Given an array of integers,update every element with largest of previous and next elements with the following exception.
a)First element - check largest of first and second.
b)Last element - check largest of last and second.
#include <stdio.h>
void modify(int arr[], int n)
{
if (n <= 1)
return;
int prev = arr[0];
arr[0] = arr[0] * arr[1];
for (int i=1; i<n-1; i++)
{
int curr = arr[i];
arr[i] = prev * arr[i+1];
prev = curr;
}
arr[n-1] = prev * arr[n-1];
}
int main() {
int arr[] = {2, 3, 4, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
modify(arr, n);
for (int i=0; i<n; i++)
printf("%d ",arr[i]);
return 0;
}
Comments
Leave a comment