All submissions

Find Maximum Element Using Pointer Walk

Code

#include <stdio.h>
//#include <math.h>

// #define MIN_VALUE (pow(2, sizeof(n)*8)/2-1)

int find_max_element(int *ptr, int n) {
    // int max_value = -MIN_VALUE;
    int max_value = *ptr;

    for (int i=1; i<n; i++) {
        if (*(ptr+i) > max_value) {
            max_value = *(ptr+i);
        }
    }

    return max_value;
}

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

 

 

 

Loading...

Input

5 10 25 5 30 15

Expected Output

30