Related Articles

# Find the occurrence of the given binary pattern in the binary representation of the array elements

• Last Updated : 10 May, 2021

Given an array arr[] of N positive integers and a string patt which consist of characters from the set {0, 1}, the task is to find the count occurrence of patt in the binary representation of every integer from the given array.
Examples:

Input: arr[] = {5, 106, 7, 8}, patt = “10”
Output: 1 3 0 1
binary(5) = 101 -> occurrence(10) = 1
binary(106) = 1101010 -> occurrence(10) = 3
binary(7) = 111 -> occurrence(10) = 0
binary(8) = 1000 -> occurrence(10) = 1
Input: arr[] = {1, 1, 1, 1}, patt = “10”
Output: 0 0 0 0

Approach: Find the binary representation of each of the array element as discussed in this article.
Now, find the occurrence of the given pattern in the binary representation found previously.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the binary``// representation of n``string decToBinary(``int` `n)``{``    ``// Array to store binary representation``    ``int` `binaryNum;` `    ``// Counter for binary array``    ``int` `i = 0;``    ``while` `(n > 0) {` `        ``// Storing remainder in binary array``        ``binaryNum[i] = n % 2;``        ``n = n / 2;``        ``i++;``    ``}` `    ``// To store the binary representation``    ``// as a string``    ``string binary = ``""``;``    ``for` `(``int` `j = i - 1; j >= 0; j--)``        ``binary += to_string(binaryNum[j]);` `    ``return` `binary;``}` `// Function to return the frequency of``// pat in the given string txt``int` `countFreq(string& pat, string& txt)``{``    ``int` `M = pat.length();``    ``int` `N = txt.length();``    ``int` `res = 0;` `    ``/* A loop to slide pat[] one by one */``    ``for` `(``int` `i = 0; i <= N - M; i++) {``        ``/* For current index i, check for ``           ``pattern match */``        ``int` `j;``        ``for` `(j = 0; j < M; j++)``            ``if` `(txt[i + j] != pat[j])``                ``break``;` `        ``// If pat[0...M-1] = txt[i, i+1, ...i+M-1]``        ``if` `(j == M) {``            ``res++;``            ``j = 0;``        ``}``    ``}``    ``return` `res;``}` `// Function to find the occurrence of``// the given pattern in the binary``// representation of elements of arr[]``void` `findOccurrence(``int` `arr[], ``int` `n, string pattern)``{` `    ``// For every element of the array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Find its binary representation``        ``string binary = decToBinary(arr[i]);` `        ``// Print the occurrence of the given pattern``        ``// in its binary representation``        ``cout << countFreq(pattern, binary) << ``" "``;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 5, 106, 7, 8 };``    ``string pattern = ``"10"``;``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``findOccurrence(arr, n, pattern);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;` `class` `GFG``{` `    ``// Function to return the binary``    ``// representation of n``    ``static` `String decToBinary(``int` `n)``    ``{``        ``// Array to store binary representation``        ``int``[] binaryNum = ``new` `int``[``32``];` `        ``// Counter for binary array``        ``int` `i = ``0``;``        ``while` `(n > ``0``)``        ``{` `            ``// Storing remainder in binary array``            ``binaryNum[i] = n % ``2``;``            ``n = n / ``2``;``            ``i++;``        ``}` `        ``// To store the binary representation``        ``// as a string``        ``String binary = ``""``;``        ``for` `(``int` `j = i - ``1``; j >= ``0``; j--)``        ``{``            ``binary += String.valueOf(binaryNum[j]);``        ``}``        ``return` `binary;``    ``}` `    ``// Function to return the frequency of``    ``// pat in the given string txt``    ``static` `int` `countFreq(String pat, String txt)``    ``{``        ``int` `M = pat.length();``        ``int` `N = txt.length();``        ``int` `res = ``0``;` `        ``/* A loop to slide pat[] one by one */``        ``for` `(``int` `i = ``0``; i <= N - M; i++)``        ``{``            ``/* For current index i, check for``            ``pattern match */``            ``int` `j;``            ``for` `(j = ``0``; j < M; j++)``            ``{``                ``if` `(txt.charAt(i + j) != pat.charAt(j))``                ``{``                    ``break``;``                ``}``            ``}` `            ``// If pat[0...M-1] = txt[i, i+1, ...i+M-1]``            ``if` `(j == M)``            ``{``                ``res++;``                ``j = ``0``;``            ``}``        ``}``        ``return` `res;``    ``}` `    ``// Function to find the occurrence of``    ``// the given pattern in the binary``    ``// representation of elements of arr[]``    ``static` `void` `findOccurrence(``int` `arr[], ``int` `n,``                               ``String pattern)``    ``{` `        ``// For every element of the array``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{` `            ``// Find its binary representation``            ``String binary = decToBinary(arr[i]);` `            ``// Print the occurrence of the given pattern``            ``// in its binary representation``            ``System.out.print(countFreq(pattern,``                                       ``binary) + ``" "``);``        ``}``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = {``5``, ``106``, ``7``, ``8``};``        ``String pattern = ``"10"``;``        ``int` `n = arr.length;``        ``findOccurrence(arr, n, pattern);``    ``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 implementation of the approach` `# Function to return the binary``# representation of n``def` `decToBinary(n):``    ` `    ``# Array to store binary representation``    ``binaryNum ``=` `[``0` `for` `i ``in` `range``(``32``)]` `    ``# Counter for binary array``    ``i ``=` `0``    ``while` `(n > ``0``):` `        ``# Storing remainder in binary array``        ``binaryNum[i] ``=` `n ``%` `2``        ``n ``=` `n ``/``/` `2``        ``i ``+``=` `1` `    ``# To store the binary representation``    ``# as a string``    ``binary ``=` `""``    ``for` `j ``in` `range``(i ``-` `1``, ``-``1``, ``-``1``):``        ``binary ``+``=` `str``(binaryNum[j])` `    ``return` `binary` `# Function to return the frequency of``# pat in the given txt``def` `countFreq(pat, txt):``    ``M ``=` `len``(pat)``    ``N ``=` `len``(txt)``    ``res ``=` `0` `    ``# A loop to slide pat[] one by one``    ``for` `i ``in` `range``(N ``-` `M ``+` `1``):``        ` `        ``# For current index i, check for``        ``# pattern match``        ``j ``=` `0``        ``while``(j < M):``            ``if` `(txt[i ``+` `j] !``=` `pat[j]):``                ``break``            ``j ``+``=` `1` `        ``# If pat[0...M-1] = txt[i, i+1, ...i+M-1]``        ``if` `(j ``=``=` `M):``            ``res ``+``=` `1``            ``j ``=` `0` `    ``return` `res` `# Function to find the occurrence of``# the given pattern in the binary``# representation of elements of arr[]``def` `findOccurrence(arr, n, pattern):` `    ``# For every element of the array``    ``for` `i ``in` `range``(n):` `        ``# Find its binary representation``        ``binary ``=` `decToBinary(arr[i])` `        ``# Print the occurrence of the given pattern``        ``# in its binary representation``        ``print``(countFreq(pattern, binary), end ``=` `" "``)` `# Driver code``arr ``=` `[``5``, ``106``, ``7``, ``8``]``pattern ``=` `"10"``n ``=` `len``(arr)` `findOccurrence(arr, n, pattern)` `# This code is contributed by Mohit Kumar`

## C#

 `// C# code implementation for above approach``using` `System;` `class` `GFG``{` `    ``// Function to return the binary``    ``// representation of n``    ``static` `String decToBinary(``int` `n)``    ``{``        ``// Array to store binary representation``        ``int``[] binaryNum = ``new` `int``;` `        ``// Counter for binary array``        ``int` `i = 0;``        ``while` `(n > 0)``        ``{` `            ``// Storing remainder in binary array``            ``binaryNum[i] = n % 2;``            ``n = n / 2;``            ``i++;``        ``}` `        ``// To store the binary representation``        ``// as a string``        ``String binary = ``""``;``        ``for` `(``int` `j = i - 1; j >= 0; j--)``        ``{``            ``binary += String.Join(``""``, binaryNum[j]);``        ``}``        ``return` `binary;``    ``}` `    ``// Function to return the frequency of``    ``// pat in the given string txt``    ``static` `int` `countFreq(String pat, String txt)``    ``{``        ``int` `M = pat.Length;``        ``int` `N = txt.Length;``        ``int` `res = 0;` `        ``/* A loop to slide pat[] one by one */``        ``for` `(``int` `i = 0; i <= N - M; i++)``        ``{``            ``/* For current index i, check for``            ``pattern match */``            ``int` `j;``            ``for` `(j = 0; j < M; j++)``            ``{``                ``if` `(txt[i + j] != pat[j])``                ``{``                    ``break``;``                ``}``            ``}` `            ``// If pat[0...M-1] = txt[i, i+1, ...i+M-1]``            ``if` `(j == M)``            ``{``                ``res++;``                ``j = 0;``            ``}``        ``}``        ``return` `res;``    ``}` `    ``// Function to find the occurrence of``    ``// the given pattern in the binary``    ``// representation of elements of arr[]``    ``static` `void` `findOccurrence(``int` `[]arr, ``int` `n,``                               ``String pattern)``    ``{` `        ``// For every element of the array``        ``for` `(``int` `i = 0; i < n; i++)``        ``{` `            ``// Find its binary representation``            ``String binary = decToBinary(arr[i]);` `            ``// Print the occurrence of the given pattern``            ``// in its binary representation``            ``Console.Write(countFreq(pattern,``                                    ``binary) + ``" "``);``        ``}``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `[]arr = {5, 106, 7, 8};``        ``String pattern = ``"10"``;``        ``int` `n = arr.Length;``        ``findOccurrence(arr, n, pattern);``    ``}``}``    ` `// This code is contributed by PrinciRaj1992`

## Javascript

 ``
Output:
`1 3 0 1`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up