All submissions

Find Maximum Element Using Pointer Walk

 

#include <stdio.h>

int find_max_element(int *ptr, int n) {
    // Your logic here
    int max = *ptr;           // Initialize max with the first element
    for (int i = 1; i < n; i++) 
    {
        ptr++;                // Move to the next element
        if (*ptr > max) 
            max = *ptr;       // Update max element if current  element is greater
    }    
    return max;
}

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;
}

 

 

 

 

Loading...

Input

5 10 25 5 30 15

Expected Output

30