Q:

C program to check a given number appears more than N/2 times in a sorted array of N integers

0

C program to check a given number appears more than N/2 times in a sorted array of N integers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, we will create an array of integers then check that the given number appears more than N/2 in the array. Here N is used to represent the size of an array.

Program:

The source code to check a given number appears more than N/2 times in a sorted array of N integers is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to check a given number appears more than N/2 times
// in a sorted array of N integers

#include <stdio.h>

int main()
{

    int arr[] = { 20, 25, 25, 25, 25, 42, 67 };
    int item = 25;
    int loop = 0;
    int count = 0;
    int size = 0;

    size = sizeof(arr) / sizeof(arr[0]);

    for (loop = 0; loop < size; loop++) {
        if (item == arr[loop])
            count = count + 1;
    }

    if (count > (size / 2))
        printf("The number %d appears more than %d times in arr[]\n", item, size / 2);
    else
        printf("The number %d does not appear more than %d times in arr[]\n", item, size / 2);

    return 0;
}

Output:

The number 25 appears more than 3 times in arr[]

Explanation:

Here, we created an array arr that contains 7 integer elements. And, we created a variable item initialized with 25. Then we check item 25 is appears more than N/2 times in the array and we printed the appropriate message on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find the median of two sorted arrays ... >>
<< C program to find two elements whose sum is closes...