All submissions

Find Maximum Element Using Pointer Walk

Code

#include <stdio.h>

int find_max_element(int *ptr, int n) {
    // Your logic here
    int large=*ptr;               // Initialize the large with first value of array 
    for(int i=1;i<n;i++){         // iterate the loop till n
        *ptr++;                   // increment the pointer
        if(*ptr >= large)         // *ptr >= large condition checking
        {
           large = *ptr;          // storing the *ptr value into the large
        }   
    }
    return large;                 // return the large
}

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

int find_max_element(int *ptr, int n) {

    // Your logic here

    int large=*ptr;               // Initialize the large with first value of array 

    for(int i=0;i<n;i++){         // iterate the loop till n

        if(*ptr >= large)         // *ptr >= large condition checking

        {

           large = *ptr;          // storing the *ptr value into the large

        }

        *ptr++;                   // increment the pointer

    }

    return large;                 // return the large

}

 

 

Loading...

Input

5 10 25 5 30 15

Expected Output

30