All submissions

Find Minimum and Maximum in an Array

Code

#include <stdio.h>
#include <stdint.h>

void find_min_max(uint8_t *arr, uint8_t n, uint8_t *min, uint8_t *max) {
    // Your logic here
    int i,j;
   for(i=0;i<n;i++)
   {
    for(j=i+1;j<n;j++)
    {
        if(arr[i]>arr[j])
        {
            arr[i]=arr[i]^arr[j];
            arr[j]=arr[i]^arr[j];
            arr[i]=arr[i]^arr[j];
        }
    }

   }
   *min=arr[0];
   *max=arr[n-1];

}

int main() {
    uint8_t n;
    scanf("%hhu", &n);
    uint8_t arr[100];

    for (uint8_t i = 0; i < n; i++) {
        scanf("%hhu", &arr[i]);
    }

    uint8_t min_val, max_val;
    find_min_max(arr, n, &min_val, &max_val);

    printf("%hhu %hhu", min_val, max_val);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

5 10 20 5 30 15

Expected Output

5 30