This C Program checks if a given integer X appears more than N/2 times in a sorted array of N integers.
Here is source code of the C Program to find if a given integer X appears more than N/2 times in a sorted array of N integers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find if a given Integer X appears more than N/2 times in a Sorted Array of N Integers
*/
# include <stdio.h>
# define bool int
bool Morenooftimes(int array[], int n, int x)
{
int i;
int final_index = n % 2 ? n / 2 : (n / 2 + 1);
for (i = 0; i < final_index; i++)
{
/* check if x is presents more than n/2 times */
if (array[i] == x && array[i + n / 2] == x)
return 1;
}
return 0;
}
int main()
{
int array[] = {10, 15, 15, 12, 17 ,15};
int n = sizeof(array) / sizeof(array[0]);
int x = 15;
if (Morenooftimes(array, n, x))
printf("The given no %d appears more than %d times in array[]", x, n/2);
else
printf("The given no %d does not appear more than %d times in array[]", x, n/2);
getchar();
return 0;
}
$ cc pgm95.c $ a.out The given no 15 appears more than 3 times in array[]
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice BCA MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Buy C Books
- Watch Advanced C Programming Videos