Find Maximum Element Using Pointer Walk

Code

#include <stdio.h>
#include <limits.h>
int find_max_element(int *ptr, int n) {
    // Your logic here
    int max_element = INT_MIN;
    for(int i = 0; i<n; i++){
        if(*ptr > max_element){
            max_element = *ptr;
        }
        ptr++;
    }
    return max_element;
}

int main() {
    int n;
    scanf("%d", &n);

    int arr[100];
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int result = find_max_element(arr, n);
    printf("%d", result);

    return 0;
}

Solving Approach

Use the Limits.h library function to use INT_MIN and find the maxiimum element.

 

 

Upvote
Downvote
Loading...

Input

5 10 25 5 30 15

Expected Output

30