Frequency of an integer in the given array using Divide and Conquer (original) (raw)
Last Updated : 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: 2Input: 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++ `
// C++ implrmrntation of the approach
#include 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 code int 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
// 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
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 code
if 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#
// C# implrmrntation of the approach using 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
JavaScript
`
Time Complexity: O(NlogN)
Similar Reads
- Smallest integer > 1 which divides every element of the given array Given an array arr[], the task is to find the smallest possible integer (other than 1) which divides every element of the given array. Examples: Input: arr[] = { 2, 4, 8 } Output: 2 2 is the smallest possible number which divides the whole array. Input: arr[] = { 4, 7, 5 } Output: -1 There's no inte 13 min read
- Find elements of an array which are divisible by N using STL in C++ Given an array and an integer N, find elements which are divisible by N, using STL in C++ Examples: Input: a[] = {1, 2, 3, 4, 5, 10}, N = 2 Output: 3 Explanation: As 2, 4, and 10 are divisible by 2 Therefore the output is 3 Input:a[] = {4, 3, 5, 9, 11}, N = 5 Output: 1 Explanation: As only 5 is divi 2 min read
- C++ Program to Find the Frequency of Elements in an Array In C++, arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. In this article, we will learn how to find the frequency of elements in an array in C++. Example: Input: Array: {1, 2, 3, 4, 2, 1, 3, 2, 4, 5} Output: Element: 1, Frequency: 2 2 min read
- C program to count frequency of each element in an array Given an array arr[] of size N, the task is to find the frequency of each distinct element present in the given array. Examples: Input: arr[] = { 1, 100000000, 3, 100000000, 3 } Output: { 1 : 1, 3 : 2, 100000000 : 2 } Explanation: Distinct elements of the given array are { 1, 100000000, 3 } Frequenc 4 min read
- Count of elements not divisible by any other elements of Array Given an array arr[], the task is to determine the number of elements of the array which are not divisible by any other element in the given array. Examples: Input: arr[] = {86, 45, 18, 4, 8, 28, 19, 33, 2} Output: 4 Explanation: The elements are {2, 19, 33, 45} are not divisible by any other array 13 min read
- First element that appears even number of times in an array Given an array, find the first element that appears even number of times in the array. It returns the element if exists otherwise returns 0. Examples: Input : arr[] = {1, 5, 4, 7, 4, 1, 5, 7, 1, 5}; Output : 4 Explanation, 4 is the first element that appears even number of times. Input : arr[] = {2, 7 min read
- Count integers in an Array which are multiples their bits counts Given an array arr[] of N elements, the task is to count all the elements which are a multiple of their set bits count.Examples: Input : arr[] = { 1, 2, 3, 4, 5, 6 } Output : 4 Explanation : There numbers which are multiple of their setbits count are { 1, 2, 4, 6 }. Input : arr[] = {10, 20, 30, 40} 9 min read
- Frequency of each element of an array of small ranged values Given an array where elements in small range. The maximum element in the array does not go beyond size of array. Find frequencies of elements. Examples: Input : arr[] = {3, 1, 2, 3, 4, 5, 4} Output: 1-->1 2-->1 3-->2 4-->2 5-->1 Input : arr[] = {1, 2, 2, 1, 2} Output: 1-->2 2--> 6 min read
- Find the element having different frequency than other array elements Given an array of N integers. Each element in the array occurs the same number of times except one element. The task is to find this element. Examples: Input : arr[] = {1, 1, 2, 2, 3}Output : 3Input : arr[] = {0, 1, 2, 4, 4}Output : 4The idea is to use a hash table freq to store the frequencies of g 7 min read
- Maximize frequency of an element by at most one increment or decrement of all array elements Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once. Examples: Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } Output: 4 Explanation: Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1, 8 min read