C program HELP

There was a Lucky draw in a company, in which they had certain rules to select the winner. The 1st rule was that, every person is given a unique number. The 2nd rule was that, for deciding the winner they will have n numbers of draws of the cards. If the same card repeats more than half number of times, then that card owner will be declared as winner. There may exist a possibility that no one will win, in that case, display -1.

Input Format
Number of Cards in the array Array of Cards.

Constraints
card number>0

Output Format
Winner Card Number

C Code
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

int main() {
    int n;
    scanf("%d", &n);
    int a[n];
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    qsort(a, n, sizeof(int), compare);
    int winner = -1;
    for(int i = 0; i < n;) {
        int c = 1;
        int j = i + 1;
        while(j < n && a[j] == a[i]) {
            j++;
            c++;
        }
        if(c > n/2) {
            winner = a[i];
            break;
        }
        i = j;
    }
    if(winner == -1) {
        printf("No Winner");
    }
    else {
        printf("Winner is %d\n", winner);
    }
    return 0;
}
2 Likes

Thanks Bro, Appreciated.