All submissions

Find Maximum Element Using Pointer Walk

Approach & Code

#include <stdio.h>
#include <limits.h>

/*
Plan:
- have a method to update the max as you go through the array
- in the find_max_element api
    - loop through the elements
    - update the max with the update max method
    - increment the pointer
- return the maximum

*/

// helper function to update the max
int update_max(int current_max, int *current_ptr)
{
    if(*current_ptr > current_max)
    {
        current_max = *current_ptr;
    }

    return current_max;
}

int find_max_element(int *ptr, int n) {
    // Your logic here
    if(n == 0 |ptr == NULL) return INT_MIN;

    int maximum = *ptr;

    for(int i = 0; i < n; ++i)
    {
        maximum = update_max(maximum, ptr);
        ptr++;
    }

    return maximum;
}

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