Programs


Title : Integer x appears more than n by 2 times in sorted array

#include <stdio.h>

#include <stdbool.h>

bool ChkMajority(int arr1[], int arr_size, int x)

{

   int i;

   int last_index = arr_size % 2 ? (arr_size / 2 + 1) : (arr_size / 2);

   for (i = 0; i < last_index; i++)

   {

       if (arr1[i] == x && arr1[i + arr_size / 2] == x)

           return 1;

   }

   return 0;

}

int main()

{

   int arr1[] = {1, 2, 4, 2, 2, 1, 2, 3, 2};

   int arr_size = sizeof(arr1) / sizeof(arr1[0]);

   int x = 2, i;

     printf("The given array is :  ");

   for (i = 0; i < arr_size; i++)

   {

       printf("%d  ", arr1[i]);

   }

   printf("\n");

   printf("The given value is :  %d\n", x);

   if (ChkMajority(arr1, arr_size, x))

       printf("%d appears more than %d times in the given array[]", x, arr_size / 2);

   else

       printf("%d does not appear more than %d times in the given array[]", x, arr_size / 2);

   return 0;

}

OUTPUT:

The given array is :  1  2  4  2  2  1  2  3  2 

The given value is :  2

2 appears more than 4 times in the given array[]

Related Topics

  • Program to find majority element of an array

      Jan 03, 2022

  • Program to print a matrix in spiral form

      Jan 03, 2022

  • WAP to find the maximum circular sub array sum of given array

      Jan 03, 2022

  • Program to count the number of triangles from a given array

      Jan 03, 2022

  • WAP to find the number of times occurs a given number in an array

      Jan 03, 2022

  • Program to sort an array of 0s, 1s and 2s.

      Jun 30, 2021

  • Subscribe Now