112. Find Top 3 Largest Values in an Array

Back To All Submissions
Previous Submission
Next Submission

Code

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

void find_top_3(uint8_t *arr, uint8_t n) {
    // Your logic here
    for (int i=0;i<n;i++){
        for (int j=i;j<n-1;j++){
            if (arr[i]<arr[j+1]){
                int temp=arr[i];
                arr[i]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
    int i=0;
    while (i<3 && n!=0){

        printf("%hhu",arr[i]);
        i++;
        n--;
        printf(" ");
        
    }
}

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

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

    find_top_3(arr, n);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote