Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
K-th Element of Merged Two Sorted Arrays
Next article icon

Check for Majority Element in a sorted array

Last Updated : 21 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr of N elements,  A majority element in an array arr of size N is an element that appears more than N/2 times in the array. The task is to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true if x is a majority element (present more than n/2 times).

Examples: 

Input: arr[] = {1, 2, 3, 3, 3, 3, 10}, x = 3
Output: True (x appears more than n/2 times in the given array)

Input: arr[] = {1, 1, 2, 4, 4, 4, 6, 6}, x = 4
Output: False (x doesn't appear more than n/2 times in the given array)

Input: arr[] = {1, 1, 1, 2, 2}, x = 1
Output: True (x appears more than n/2 times in the given array)
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 
 

METHOD 1 (Using Linear Search): Linearly search for the first occurrence of the element, once you find it (let at index i), check the element at index i + n/2. If the element is present at i+n/2 then return 1 else return 0.

C++




/* C++ Program to check for majority element in a sorted array */
#include<bits/stdc++.h>
using namespace std;
 
bool isMajority(int arr[], int n, int x)
{
    int i;
 
    /* get last index according to n (even or odd) */
    int last_index = n % 2 ? (n / 2 + 1): (n / 2);
 
    /* search for first occurrence of x in arr[]*/
    for (i = 0; i < last_index; i++)
    {
       
        /* check if x is present and is present more than n/2
        times */
        if (arr[i] == x && arr[i + n / 2] == x)
            return 1;
    }
    return 0;
}
 
/* Driver code */
int main()
{
    int arr[] ={1, 2, 3, 4, 4, 4, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    int x = 4;
    if (isMajority(arr, n, x))
        cout <<    x <<" appears more than "<<
                              n/2 << " times in arr[]"<< endl;
    else
        cout <<x <<" does not appear more than" << n/2 <<"  times in arr[]" << endl;
 
return 0;
}
 
// This code is contributed by shivanisinghss2110
 
 

C




/* C Program to check for majority element in a sorted array */
# include <stdio.h>
# include <stdbool.h>
 
bool isMajority(int arr[], int n, int x)
{
    int i;
 
    /* get last index according to n (even or odd) */
    int last_index = n%2? (n/2+1): (n/2);
 
    /* search for first occurrence of x in arr[]*/
    for (i = 0; i < last_index; i++)
    {
        /* check if x is present and is present more than n/2
           times */
        if (arr[i] == x && arr[i+n/2] == x)
            return 1;
    }
    return 0;
}
 
/* Driver program to check above function */
int main()
{
     int arr[] ={1, 2, 3, 4, 4, 4, 4};
     int n = sizeof(arr)/sizeof(arr[0]);
     int x = 4;
     if (isMajority(arr, n, x))
        printf("%d appears more than %d times in arr[]",
               x, n/2);
     else
        printf("%d does not appear more than %d times in arr[]",
                x, n/2);
 
   return 0;
}
 
 

Java




/* Program to check for majority element in a sorted array */
import java.io.*;
 
class Majority {
 
    static boolean isMajority(int arr[], int n, int x)
    {
        int i, last_index = 0;
 
        /* get last index according to n (even or odd) */
        last_index = (n%2==0)? n/2: n/2+1;
 
        /* search for first occurrence of x in arr[]*/
        for (i = 0; i < last_index; i++)
        {
            /* check if x is present and is present more
               than n/2 times */
            if (arr[i] == x && arr[i+n/2] == x)
                return true;
        }
        return false;
    }
 
    /* Driver function to check for above functions*/
    public static void main (String[] args) {
        int arr[] = {1, 2, 3, 4, 4, 4, 4};
        int n = arr.length;
        int x = 4;
        if (isMajority(arr, n, x)==true)
           System.out.println(x+" appears more than "+
                              n/2+" times in arr[]");
        else
           System.out.println(x+" does not appear more than "+
                              n/2+" times in arr[]");
    }
}
/*This article is contributed by Devesh Agrawal*/
 
 

Python3




'''Python3 Program to check for majority element in a sorted array'''
 
def isMajority(arr, n, x):
    # get last index according to n (even or odd) */
    last_index = (n//2 + 1) if n % 2 != 0 else (n//2)
 
    # search for first occurrence of x in arr[]*/
    for i in range(last_index):
        # check if x is present and is present more than n / 2 times */
        if arr[i] == x and arr[i + n//2] == x:
            return 1
 
# Driver program to check above function */
arr = [1, 2, 3, 4, 4, 4, 4]
n = len(arr)
x = 4
if (isMajority(arr, n, x)):
    print ("% d appears more than % d times in arr[]"
                                            %(x, n//2))
else:
    print ("% d does not appear more than % d times in arr[]"
                                                    %(x, n//2))
 
 
# This code is contributed by shreyanshi_arun.
 
 

C#




// C# Program to check for majority
// element in a sorted array
using System;
 
class GFG {
    static bool isMajority(int[] arr,
                            int n, int x)
    {
        int i, last_index = 0;
 
        // Get last index according to
        // n (even or odd)
        last_index = (n % 2 == 0) ? n / 2 :
                                    n / 2 + 1;
 
        // Search for first occurrence
        // of x in arr[]
        for (i = 0; i < last_index; i++) {
            // Check if x is present and
            // is present more than n/2 times
            if (arr[i] == x && arr[i + n / 2] == x)
                return true;
        }
        return false;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 4, 4, 4 };
        int n = arr.Length;
        int x = 4;
        if (isMajority(arr, n, x) == true)
            Console.Write(x + " appears more than " +
                            n / 2 + " times in arr[]");
        else
            Console.Write(x + " does not appear more than " +
                             n / 2 + " times in arr[]");
    }
}
 
// This code is contributed by Sam007
 
 

Javascript




<script>
 
    // Javascript Program to check for majority
    // element in a sorted array
     
    function isMajority(arr, n, x)
    {
        let i, last_index = 0;
  
        // Get last index according to
        // n (even or odd)
        last_index = (n % 2 == 0) ?
        parseInt(n / 2, 10) : parseInt(n / 2, 10) + 1;
  
        // Search for first occurrence
        // of x in arr[]
        for (i = 0; i < last_index; i++) {
            // Check if x is present and
            // is present more than n/2 times
            if (arr[i] == x && arr[i +
            parseInt(n / 2, 10)] == x)
                return true;
        }
        return false;
    }
       
    let arr = [ 1, 2, 3, 4, 4, 4, 4 ];
    let n = arr.length;
    let x = 4;
    if (isMajority(arr, n, x) == true)
      document.write(x + " appears more than " +
      parseInt(n / 2, 10) + " times in arr[]");
    else
      document.write(x + " does not appear more than " +
      parseInt(n / 2, 10) + " times in arr[]");
                              
</script>
 
 

PHP




<?php
// PHP Program to check for
// majority element in a
// sorted array
 
// function returns majority
// element in a sorted array
function isMajority($arr, $n, $x)
{
    $i;
 
    // get last index according
    // to n (even or odd)
    $last_index = $n % 2? ($n / 2 + 1): ($n / 2);
 
    // search for first occurrence
    // of x in arr[]
    for ($i = 0; $i < $last_index; $i++)
    {
         
        // check if x is present and
        // is present more than n/2
        // times
        if ($arr[$i] == $x && $arr[$i + $n / 2] == $x)
            return 1;
    }
    return 0;
}
 
    // Driver Code
    $arr = array(1, 2, 3, 4, 4, 4, 4);
    $n = sizeof($arr);
    $x = 4;
    if (isMajority($arr, $n, $x))
        echo $x, " appears more than "
             , floor($n / 2), " times in arr[]";
         
    else
        echo $x, "does not appear more than "
              , floor($n / 2), "times in arr[]";
                 
// This code is contributed by Ajit
?>
 
 
Output
4 appears more than 3 times in arr[]  

Time Complexity: O(n)
Auxiliary Space: O(1)

METHOD 2 (Using Binary Search): Use binary search methodology to find the first occurrence of the given number. The criteria for binary search is important here. 

C++




// C++ program to check for majority
// element in a sorted array
#include<bits/stdc++.h>
using namespace std;
 
// If x is present in arr[low...high]
// then returns the index of first
// occurrence of x, otherwise returns -1
int _binarySearch(int arr[], int low,
                  int high, int x);
 
// This function returns true if the x
// is present more than n/2 times in
// arr[] of size n
bool isMajority(int arr[], int n, int x)
{
     
    // Find the index of first occurrence
    // of x in arr[]
    int i = _binarySearch(arr, 0, n - 1, x);
 
    // If element is not present at all,
    // return false
    if (i == -1)
        return false;
 
    // Check if the element is present
    // more than n/2 times
    if (((i + n / 2) <= (n - 1)) &&
      arr[i + n / 2] == x)
        return true;
    else
        return false;
}
 
// If x is present in arr[low...high] then
// returns the index of first occurrence
// of x, otherwise returns -1
int _binarySearch(int arr[], int low,
                  int high, int x)
{
    if (high >= low)
    {
        int mid = (low + high)/2; /*low + (high - low)/2;*/
 
        /* Check if arr[mid] is the first occurrence of x.
            arr[mid] is first occurrence if x is one of
            the following is true:
            (i) mid == 0 and arr[mid] == x
            (ii) arr[mid-1] < x and arr[mid] == x
        */
        if ((mid == 0 || x > arr[mid - 1]) &&
            (arr[mid] == x) )
            return mid;
             
        else if (x > arr[mid])
            return _binarySearch(arr, (mid + 1),
                                 high, x);
        else
            return _binarySearch(arr, low, 
                                (mid - 1), x);
    }
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 3, 3, 3, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 3;
     
    if (isMajority(arr, n, x))
        cout << x << " appears more than "
             << n / 2 << " times in arr[]"
             << endl;
    else
        cout << x << " does not appear more than"
             << n / 2 << "  times in arr[]" << endl;
  
    return 0;
}
 
// This code is contributed by shivanisinghss2110
 
 

C




/* C Program to check for majority element in a sorted array */
# include <stdio.h>
# include <stdbool.h>
 
/* If x is present in arr[low...high] then returns the index of
first occurrence of x, otherwise returns -1 */
int _binarySearch(int arr[], int low, int high, int x);
 
/* This function returns true if the x is present more than n/2
times in arr[] of size n */
bool isMajority(int arr[], int n, int x)
{
    /* Find the index of first occurrence of x in arr[] */
    int i = _binarySearch(arr, 0, n-1, x);
 
    /* If element is not present at all, return false*/
    if (i == -1)
        return false;
 
    /* check if the element is present more than n/2 times */
    if (((i + n/2) <= (n -1)) && arr[i + n/2] == x)
        return true;
    else
        return false;
}
 
/* If x is present in arr[low...high] then returns the index of
first occurrence of x, otherwise returns -1 */
int _binarySearch(int arr[], int low, int high, int x)
{
    if (high >= low)
    {
        int mid = (low + high)/2; /*low + (high - low)/2;*/
 
        /* Check if arr[mid] is the first occurrence of x.
            arr[mid] is first occurrence if x is one of the following
            is true:
            (i) mid == 0 and arr[mid] == x
            (ii) arr[mid-1] < x and arr[mid] == x
        */
        if ( (mid == 0 || x > arr[mid-1]) && (arr[mid] == x) )
            return mid;
        else if (x > arr[mid])
            return _binarySearch(arr, (mid + 1), high, x);
        else
            return _binarySearch(arr, low, (mid -1), x);
    }
 
    return -1;
}
 
/* Driver program to check above functions */
int main()
{
    int arr[] = {1, 2, 3, 3, 3, 3, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    int x = 3;
    if (isMajority(arr, n, x))
        printf("%d appears more than %d times in arr[]",
               x, n/2);
    else
        printf("%d does not appear more than %d times in arr[]",
               x, n/2);
    return 0;
}
 
 

Java




/* Java Program to check for majority element in a sorted array */
import java.io.*;
 
class Majority {
 
    /* If x is present in arr[low...high] then returns the index of
        first occurrence of x, otherwise returns -1 */
    static int  _binarySearch(int arr[], int low, int high, int x)
    {
        if (high >= low)
        {
            int mid = (low + high)/2;  /*low + (high - low)/2;*/
 
            /* Check if arr[mid] is the first occurrence of x.
                arr[mid] is first occurrence if x is one of the following
                is true:
                (i)  mid == 0 and arr[mid] == x
                (ii) arr[mid-1] < x and arr[mid] == x
            */
            if ( (mid == 0 || x > arr[mid-1]) && (arr[mid] == x) )
                return mid;
            else if (x > arr[mid])
                return _binarySearch(arr, (mid + 1), high, x);
            else
                return _binarySearch(arr, low, (mid -1), x);
        }
 
        return -1;
    }
 
 
    /* This function returns true if the x is present more than n/2
        times in arr[] of size n */
    static boolean isMajority(int arr[], int n, int x)
    {
        /* Find the index of first occurrence of x in arr[] */
        int i = _binarySearch(arr, 0, n-1, x);
 
        /* If element is not present at all, return false*/
        if (i == -1)
            return false;
 
        /* check if the element is present more than n/2 times */
        if (((i + n/2) <= (n -1)) && arr[i + n/2] == x)
            return true;
        else
            return false;
    }
 
    /*Driver function to check for above functions*/
    public static void main (String[] args)  {
 
        int arr[] = {1, 2, 3, 3, 3, 3, 10};
        int n = arr.length;
        int x = 3;
        if (isMajority(arr, n, x)==true)
            System.out.println(x + " appears more than "+
                              n/2 + " times in arr[]");
        else
            System.out.println(x + " does not appear more than " +
                              n/2 + " times in arr[]");
    }
}
/*This code is contributed by Devesh Agrawal*/
 
 

Python3




'''Python3 Program to check for majority element in a sorted array'''
 
# This function returns true if the x is present more than n / 2
# times in arr[] of size n */
def isMajority(arr, n, x):
     
    # Find the index of first occurrence of x in arr[] */
    i = _binarySearch(arr, 0, n-1, x)
 
    # If element is not present at all, return false*/
    if i == -1:
        return False
 
    # check if the element is present more than n / 2 times */
    if ((i + n//2) <= (n -1)) and arr[i + n//2] == x:
        return True
    else:
        return False
 
# If x is present in arr[low...high] then returns the index of
# first occurrence of x, otherwise returns -1 */
def _binarySearch(arr, low, high, x):
    if high >= low:
        mid = (low + high)//2 # low + (high - low)//2;
 
        ''' Check if arr[mid] is the first occurrence of x.
            arr[mid] is first occurrence if x is one of the following
            is true:
            (i) mid == 0 and arr[mid] == x
            (ii) arr[mid-1] < x and arr[mid] == x'''
         
        if (mid == 0 or x > arr[mid-1]) and (arr[mid] == x):
            return mid
        elif x > arr[mid]:
            return _binarySearch(arr, (mid + 1), high, x)
        else:
            return _binarySearch(arr, low, (mid -1), x)
    return -1
 
 
# Driver program to check above functions */
arr = [1, 2, 3, 3, 3, 3, 10]
n = len(arr)
x = 3
if (isMajority(arr, n, x)):
    print ("% d appears more than % d times in arr[]"
                                            % (x, n//2))
else:
    print ("% d does not appear more than % d times in arr[]"
                                                    % (x, n//2))
 
# This code is contributed by shreyanshi_arun.
 
 

C#




// C# Program to check for majority
// element in a sorted array */
using System;
 
class GFG {
 
    // If x is present in arr[low...high]
    // then returns the index of first
    // occurrence of x, otherwise returns -1
    static int _binarySearch(int[] arr, int low,
                               int high, int x)
    {
        if (high >= low) {
            int mid = (low + high) / 2;
            //low + (high - low)/2;
 
            // Check if arr[mid] is the first
            // occurrence of x.    arr[mid] is
            // first occurrence if x is one of
            // the following is true:
            // (i) mid == 0 and arr[mid] == x
            // (ii) arr[mid-1] < x and arr[mid] == x
             
            if ((mid == 0 || x > arr[mid - 1]) &&
                                 (arr[mid] == x))
                return mid;
            else if (x > arr[mid])
                return _binarySearch(arr, (mid + 1),
                                           high, x);
            else
                return _binarySearch(arr, low,
                                      (mid - 1), x);
        }
 
        return -1;
    }
 
    // This function returns true if the x is
    // present more than n/2 times in arr[]
    // of size n
    static bool isMajority(int[] arr, int n, int x)
    {
         
        // Find the index of first occurrence
        // of x in arr[]
        int i = _binarySearch(arr, 0, n - 1, x);
 
        // If element is not present at all,
        // return false
        if (i == -1)
            return false;
 
        // check if the element is present
        // more than n/2 times
        if (((i + n / 2) <= (n - 1)) &&
                                arr[i + n / 2] == x)
            return true;
        else
            return false;
    }
 
    //Driver code
    public static void Main()
    {
 
        int[] arr = { 1, 2, 3, 3, 3, 3, 10 };
        int n = arr.Length;
        int x = 3;
        if (isMajority(arr, n, x) == true)
           Console.Write(x + " appears more than " +
                             n / 2 + " times in arr[]");
        else
           Console.Write(x + " does not appear more than " +
                             n / 2 + " times in arr[]");
    }
}
 
// This code is contributed by Sam007
 
 

Javascript




<script>
    // Javascript Program to check for majority
    // element in a sorted array */
     
    // If x is present in arr[low...high]
    // then returns the index of first
    // occurrence of x, otherwise returns -1
    function _binarySearch(arr, low, high, x)
    {
        if (high >= low) {
            let mid = parseInt((low + high) / 2, 10);
            //low + (high - low)/2;
  
            // Check if arr[mid] is the first
            // occurrence of x.    arr[mid] is
            // first occurrence if x is one of
            // the following is true:
            // (i) mid == 0 and arr[mid] == x
            // (ii) arr[mid-1] < x and arr[mid] == x
              
            if ((mid == 0 || x > arr[mid - 1]) && (arr[mid] == x))
                return mid;
            else if (x > arr[mid])
                return _binarySearch(arr, (mid + 1), high, x);
            else
                return _binarySearch(arr, low, (mid - 1), x);
        }
  
        return -1;
    }
  
    // This function returns true if the x is
    // present more than n/2 times in arr[]
    // of size n
    function isMajority(arr, n, x)
    {
          
        // Find the index of first occurrence
        // of x in arr[]
        let i = _binarySearch(arr, 0, n - 1, x);
  
        // If element is not present at all,
        // return false
        if (i == -1)
            return false;
  
        // check if the element is present
        // more than n/2 times
        if (((i + parseInt(n / 2, 10)) <= (n - 1)) && arr[i + parseInt(n / 2, 10)] == x)
            return true;
        else
            return false;
    }
     
    let arr = [ 1, 2, 3, 3, 3, 3, 10 ];
    let n = arr.length;
    let x = 3;
    if (isMajority(arr, n, x) == true)
      document.write(x + " appears more than " + parseInt(n / 2, 10) + " times in arr[]");
    else
      document.write(x + " does not appear more than " + parseInt(n / 2, 10) + " times in arr[]");
     
</script>
 
 
Output
3 appears more than 3 times in arr[]  

Time Complexity: O(log n)
Auxiliary Space: O(1)

Algorithmic Paradigm: Divide and Conquer



Next Article
K-th Element of Merged Two Sorted Arrays
author
kartik
Improve
Article Tags :
  • Arrays
  • Divide and Conquer
  • DSA
  • Binary Search
Practice Tags :
  • Arrays
  • Binary Search
  • Divide and Conquer

Similar Reads

  • Divide and Conquer Algorithm
    Divide and Conquer algorithm is a problem-solving strategy that involves. Divide : Break the given problem into smaller non-overlapping problems.Conquer : Solve Smaller ProblemsCombine : Use the Solutions of Smaller Problems to find the overall result.Examples of Divide and Conquer are Merge Sort, Q
    1 min read
  • Introduction to Divide and Conquer Algorithm
    Divide and Conquer Algorithm is a problem-solving technique used to solve problems by dividing the main problem into subproblems, solving them individually and then merging them to find solution to the original problem. Divide and Conquer is mainly useful when we divide a problem into independent su
    9 min read
  • Dynamic Programming vs Divide-and-Conquer
    In this article I’m trying to explain the difference/similarities between dynamic programming and divide and conquer approaches based on two examples: binary search and minimum edit distance (Levenshtein distance).The ProblemWhen I started to learn algorithms it was hard for me to understand the mai
    12 min read
  • Decrease and Conquer
    As divide-and-conquer approach is already discussed, which include following steps: Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the sub problems by solving them recursively. If the subproblem sizes are small enough, however, just solve the
    5 min read
  • Advanced master theorem for divide and conquer recurrences
    The Master Theorem is a tool used to solve recurrence relations that arise in the analysis of divide-and-conquer algorithms. The Master Theorem provides a systematic way of solving recurrence relations of the form: T(n) = aT(n/b) + f(n) where a, b, and f(n) are positive functions and n is the size o
    5 min read
  • Some standard Divide and Conquer Algorithms

    • Write program to calculate pow(b, e)
      Given two numbers b and e, the task is to implement a function to compute b^e. Examples: Input: b = 3.00000, e = 5Output: 243.00000 Input: b = 0.55000, e = 3Output: 0.16638 Input: b = -0.67000, e = -7Output: -16.49971 Table of Content [Naive Approach 1] Using Iteration - O(e) Time and O(1) Space[Nai
      10 min read

    • Karatsuba algorithm for fast multiplication using Divide and Conquer algorithm
      Given two binary strings that represent value of two integers, find the product of two strings. For example, if the first bit string is "1100" and second bit string is "1010", output should be 120. For simplicity, let the length of two strings be same and be n. A Naive Approach is to follow the proc
      15+ min read

    • Strassen's Matrix Multiplication
      Given two square matrices arr[][] and brr[][] of order n * n. Your task is to multiply both the matrices and find the resultant matrix. Examples: Input: arr[][] = [ [7, 8], [2, 9] ]brr[][] = [ [14, 5], [5, 18] ]Output: [ [138, 179], [73, 172] ] Input: arr[][] = [ [17, 4], [17, 16] ]brr[][] = [ [9, 2
      15+ min read

    • Convex Hull using Divide and Conquer Algorithm
      In computational geometry, a convex hull is the smallest convex polygon that contains a given set of points. It is a fundamental concept with applications in various fields such as computer graphics, robotics, and image processing. Importance of Convex Hull:Convex hulls are important in computationa
      15 min read

    • Quickhull Algorithm for Convex Hull
      Given a set of points, a Convex hull is the smallest convex polygon containing all the given points. Input : points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};Output : The points in convex hull are: (0, 0) (0, 3) (3, 1) (4, 4)Input : points[] = {{0, 3}, {1, 1}Output : Not P
      14 min read

    Binary Search based problems

    • Peak Element in Array
      Given an array arr[] where no two adjacent elements are same, find the index of a peak element. An element is considered to be a peak element if it is strictly greater than its adjacent elements. If there are multiple peak elements, return the index of any one of them. Note: Consider the element bef
      13 min read

    • Check for Majority Element in a sorted array
      Given an array arr of N elements, A majority element in an array arr of size N is an element that appears more than N/2 times in the array. The task is to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true i
      15+ min read

    • K-th Element of Merged Two Sorted Arrays
      Given two sorted arrays of sizes m and n respectively, the task is to find the element that would be at the k-th position in the final sorted array formed by merging these two arrays. Examples: Input: a[] = [2, 3, 6, 7, 9], b[] = [1, 4, 8, 10], k = 5Output: 6Explanation: The final sorted array is [1
      15+ min read

    • Find the number of zeroes
      Given an array of 1s and 0s which has all 1s first followed by all 0s. Find the number of 0s. Count the number of zeroes in the given array.Examples : Input: arr[] = {1, 1, 1, 1, 0, 0} Output: 2 Input: arr[] = {1, 0, 0, 0, 0} Output: 4 Input: arr[] = {0, 0, 0} Output: 3 Input: arr[] = {1, 1, 1, 1} O
      12 min read

    • Rotation Count in a Rotated Sorted array
      Given an array arr[] having distinct numbers sorted in increasing order and the array has been right rotated (i.e, the last element will be cyclically shifted to the starting position of the array) k number of times, the task is to find the value of k. Examples: Input: arr[] = {15, 18, 2, 3, 6, 12}O
      13 min read

    • Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
      Given a function 'int f(unsigned int x)' which takes a non-negative integer 'x' as input and returns an integer as output. The function is monotonically increasing with respect to the value of x, i.e., the value of f(x+1) is greater than f(x) for every input x. Find the value 'n' where f() becomes p
      11 min read

    • Median of two Sorted Arrays of Different Sizes
      Given two sorted arrays, a[] and b[], the task is to find the median of these sorted arrays. Assume that the two sorted arrays are merged and then median is selected from the combined array. This is an extension of Median of two sorted arrays of equal size problem. Here we handle arrays of unequal s
      15+ min read

    • The Painter's Partition Problem using Binary Search
      Given an array arr[] and k, where the array represents the boards and each element of the given array represents the length of each board. k numbers of painters are available to paint these boards. Consider that each unit of a board takes 1 unit of time to paint.The task is to find the minimum time
      10 min read

    Some practice problems on Divide and Conquer algorithm

    • Program for Square Root of Integer
      Given a positive integer n, find its square root. If n is not a perfect square, then return floor of √n. Examples : Input: n = 4Output: 2Explanation: The square root of 4 is 2. Input: n = 11Output: 3Explanation: The square root of 11 lies in between 3 and 4 so floor of the square root is 3. Table of
      13 min read

    • Maximum and minimum of an array using minimum number of comparisons
      Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples: Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1 Maximum element is: 9 Input: arr[] = {22, 14, 8, 17, 35, 3}Output: Minimum element is: 3 Maxim
      15+ min read

    • Find frequency of each element in a limited range array in less than O(n) time
      Given a sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant M Note: Do this without traversing the complete array. i.e. expected time complexity is less than O(n) Examples: Input: arr[]
      10 min read

    • Tiling Problem - L Shaped
      Given an n×n board (where n = 2k and k≥1), with one missing cell, the task is to fill the remaining cells using L-shaped tiles. An L-shaped tile covers 3 cells in a 2x2 grid, with one cell missing. You need to tile the entire board using the L-shaped tiles, ensuring that the missing cell remains unc
      15+ min read

    • Count Inversions of an Array
      Given an integer array arr[] of size n, find the inversion count in the array. Two array elements arr[i] and arr[j] form an inversion if arr[i] > arr[j] and i < j. Note: Inversion Count for an array indicates that how far (or close) the array is from being sorted. If the array is already sorte
      15+ min read

    • The Skyline Problem | Set-1
      Given n rectangular buildings in a 2-dimensional city, compute the skyline of these buildings, eliminating hidden lines. The main task is to view buildings from a side and remove all sections that are not visible. All buildings share a common bottom and every building is represented by a triplet (le
      14 min read

    • Search in a Row-wise and Column-wise Sorted 2D Array using Divide and Conquer algorithm
      Given an n x n matrix, where every row and column is sorted in increasing order. Given a key, how to decide whether this key is in the matrix. Input: x = 62, mat[][] = [[3, 30, 38], [20, 52, 54], [35, 60, 69]]Output: falseExplanation: 62 is not present in the matrix. Input: x = 30, mat[][] = [[3, 30
      7 min read

    • Allocate Minimum Pages
      Given an array arr[] and an integer k, where arr[i] denotes the number of pages of a book and k denotes total number of students. All the books need to be allocated to k students in contiguous manner, with each student getting at least one book. The task is to minimize the maximum number of pages al
      15+ min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences