Frequency of an integer in the given array using Divide and Conquer - GeeksforGeeks

03 Jun, 2021
Given an unsorted array arr[] and an integer K, the task is to count the occurrences of K in the given array using the Divide and Conquer method.
Examples: 
Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 1 Output: 2
Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 4 Output: 0
Approach: The idea is to divide the array into two parts of equal size and count the number of occurrences of K in each half and then add them up.
Divide the array into two parts until there is only one element left in the array.
Check whether a single element in the array is K or not. If it is K then return 1 otherwise 0.
Add up the returned values for each of the elements to find the occurrence of K in the whole array.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implrmrntation of the approach #include <iostream>using namespace std; // Function to return the frequency of x// in the subarray arr[low...high]int count(int arr[], int low, int high, int x){     // If the subarray is invalid or the    // element is not found    if ((low > high)        || (low == high && arr[low] != x))        return 0;     // If there's only a single element    // which is equal to x    if (low == high && arr[low] == x)        return 1;     // Divide the array into two parts and    // then find the count of occurrences    // of x in both the parts    return count(arr, low,                 (low + high) / 2, x)           + count(arr, 1 + (low + high) / 2,                   high, x);} // Driver codeint main(){    int arr[] = { 30, 1, 42, 5, 56, 3, 56, 9 };    int n = sizeof(arr) / sizeof(int);    int x = 56;     cout << count(arr, 0, n - 1, x);     return 0;}
// Java implrmrntation of the approach class GFG {     // Function to return the frequency of x    // in the subarray arr[low...high]    static int count(int arr[], int low,                     int high, int x)    {         // If the subarray is invalid or the        // element is not found        if ((low > high)            || (low == high && arr[low] != x))            return 0;         // If there's only a single element        // which is equal to x        if (low == high && arr[low] == x)            return 1;         // Divide the array into two parts and        // then find the count of occurrences        // of x in both the parts        return count(arr, low,                     (low + high) / 2, x)            + count(arr, 1 + (low + high) / 2,                    high, x);    }     // Driver code    public static void main(String args[])    {        int arr[] = { 30, 1, 42, 5, 56, 3, 56, 9 };        int n = arr.length;        int x = 56;        System.out.print(count(arr, 0, n - 1, x));    }}
# Python3 implrmrntation of the approach # Function to return the frequency of x# in the subarray arr[low...high]def count(arr, low, high, x):     # If the subarray is invalid or the    # element is not found    if ((low > high) or (low == high and arr[low] != x)):        return 0;     # If there's only a single element    # which is equal to x    if (low == high and arr[low] == x):        return 1;     # Divide the array into two parts and    # then find the count of occurrences    # of x in both the parts    return count(arr, low, (low + high) // 2, x) +\    count(arr, 1 + (low + high) // 2, high, x); # Driver codeif __name__ == '__main__':    arr = [ 30, 1, 42, 5, 56, 3, 56, 9];    n = len(arr);    x = 56;    print(count(arr, 0, n - 1, x)); # This code is contributed by PrinciRaj1992
// C# implrmrntation of the approachusing System; class GFG{     // Function to return the frequency of x    // in the subarray arr[low...high]    static int count(int []arr, int low,                    int high, int x)    {         // If the subarray is invalid or the        // element is not found        if ((low > high)            || (low == high && arr[low] != x))            return 0;         // If there's only a single element        // which is equal to x        if (low == high && arr[low] == x)            return 1;         // Divide the array into two parts and        // then find the count of occurrences        // of x in both the parts        return count(arr, low,                    (low + high) / 2, x)            + count(arr, 1 + (low + high) / 2,                    high, x);    }     // Driver code    public static void Main()    {        int []arr = { 30, 1, 42, 5, 56, 3, 56, 9 };        int n = arr.Length;        int x = 56;        Console.Write(count(arr, 0, n - 1, x));    }} // This code is contributed by AnkitRai01
<script>// Javascript implrmrntation of the approach// Function to return the frequency of x// in the subarray arr[low...high] function count(arr, low, high, x) {     // If the subarray is invalid or the    // element is not found    if ((low > high)        || (low == high && arr[low] != x))        return 0;     // If there's only a single element    // which is equal to x    if (low == high && arr[low] == x)        return 1;     // Divide the array into two parts and    // then find the count of occurrences    // of x in both the parts    return count(arr, low,        Math.floor((low + high) / 2), x)        + count(arr, 1 + Math.floor((low + high) / 2),            high, x);} // Driver code let arr = [30, 1, 42, 5, 56, 3, 56, 9];let n = arr.length;let x = 56; document.write(count(arr, 0, n - 1, x)); // This code is contributed by _saurabh_jaiswal</script>
2
 
Time Complexity: O(NlogN) 
ankthon
princiraj1992
uttamsingh1
santradibbo
_saurabh_jaiswal
Arrays
Divide and Conquer
Recursion
Arrays
Recursion
Divide and Conquer
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Program to find sum of elements in a given array
Merge Sort
QuickSort
Binary Search
Program for Tower of Hanoi
Divide and Conquer Algorithm | Introduction