All submissions

Find Maximum Element Using Pointer Walk

Code

#include <stdio.h>

int find_max_element(int *ptr, int n) {
    // Your logic here
    //return 0;
    int temp = *(ptr + 0);
    for (int x = 0; x < n; x++)
    {
        for(int y = x + 1; y < n; y++)
        {
            //store the highest value
            if (temp >= *(ptr + y)) 
                temp = temp;
            else if (temp < *(ptr + y))
                temp = *(ptr + y);
        }
    }
    return temp;
}

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