Home »
C programs »
C one-dimensional array programs
C program to check a given number appears more than N/2 times in a sorted array of N integers
Here, we are going to learn how to check a given number appears more than N/2 times in a sorted array of N integers in C programming language?
Submitted by Nidhi, on July 11, 2021
Problem statement
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.
Checking a given number appears more than N/2 times in a sorted array of N integers
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
// 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.
C One-Dimensional Array Programs »