Given an array arr[] of N positive integers and a string patt which consists 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 elements 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++
#include <bits/stdc++.h>
using namespace std;
string decToBinary( int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
string binary = "" ;
for ( int j = i - 1; j >= 0; j--)
binary += to_string(binaryNum[j]);
return binary;
}
int countFreq(string& pat, string& txt)
{
int M = pat.length();
int N = txt.length();
int res = 0;
for ( int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M) {
res++;
j = 0;
}
}
return res;
}
void findOccurrence( int arr[], int n, string pattern)
{
for ( int i = 0; i < n; i++) {
string binary = decToBinary(arr[i]);
cout << countFreq(pattern, binary) << " " ;
}
}
int main()
{
int arr[] = { 5, 106, 7, 8 };
string pattern = "10" ;
int n = sizeof (arr) / sizeof (arr[0]);
findOccurrence(arr, n, pattern);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String decToBinary( int n)
{
int [] binaryNum = new int [ 32 ];
int i = 0 ;
while (n > 0 )
{
binaryNum[i] = n % 2 ;
n = n / 2 ;
i++;
}
String binary = "" ;
for ( int j = i - 1 ; j >= 0 ; j--)
{
binary += String.valueOf(binaryNum[j]);
}
return binary;
}
static int countFreq(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int res = 0 ;
for ( int i = 0 ; i <= N - M; i++)
{
int j;
for (j = 0 ; j < M; j++)
{
if (txt.charAt(i + j) != pat.charAt(j))
{
break ;
}
}
if (j == M)
{
res++;
j = 0 ;
}
}
return res;
}
static void findOccurrence( int arr[], int n,
String pattern)
{
for ( int i = 0 ; i < n; i++)
{
String binary = decToBinary(arr[i]);
System.out.print(countFreq(pattern,
binary) + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 106 , 7 , 8 };
String pattern = "10" ;
int n = arr.length;
findOccurrence(arr, n, pattern);
}
}
|
Python3
def decToBinary(n):
binaryNum = [ 0 for i in range ( 32 )]
i = 0
while (n > 0 ):
binaryNum[i] = n % 2
n = n / / 2
i + = 1
binary = ""
for j in range (i - 1 , - 1 , - 1 ):
binary + = str (binaryNum[j])
return binary
def countFreq(pat, txt):
M = len (pat)
N = len (txt)
res = 0
for i in range (N - M + 1 ):
j = 0
while (j < M):
if (txt[i + j] ! = pat[j]):
break
j + = 1
if (j = = M):
res + = 1
j = 0
return res
def findOccurrence(arr, n, pattern):
for i in range (n):
binary = decToBinary(arr[i])
print (countFreq(pattern, binary), end = " " )
arr = [ 5 , 106 , 7 , 8 ]
pattern = "10"
n = len (arr)
findOccurrence(arr, n, pattern)
|
C#
using System;
class GFG
{
static String decToBinary( int n)
{
int [] binaryNum = new int [32];
int i = 0;
while (n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
String binary = "" ;
for ( int j = i - 1; j >= 0; j--)
{
binary += String.Join( "" , binaryNum[j]);
}
return binary;
}
static int countFreq(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
int res = 0;
for ( int i = 0; i <= N - M; i++)
{
int j;
for (j = 0; j < M; j++)
{
if (txt[i + j] != pat[j])
{
break ;
}
}
if (j == M)
{
res++;
j = 0;
}
}
return res;
}
static void findOccurrence( int []arr, int n,
String pattern)
{
for ( int i = 0; i < n; i++)
{
String binary = decToBinary(arr[i]);
Console.Write(countFreq(pattern,
binary) + " " );
}
}
public static void Main(String[] args)
{
int []arr = {5, 106, 7, 8};
String pattern = "10" ;
int n = arr.Length;
findOccurrence(arr, n, pattern);
}
}
|
Javascript
<script>
function decToBinary(n)
{
var binaryNum = Array(32);
var i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = parseInt(n / 2);
i++;
}
var binary = "" ;
for ( var j = i - 1; j >= 0; j--)
binary += (binaryNum[j].toString());
return binary;
}
function countFreq(pat, txt)
{
var M = pat.length;
var N = txt.length;
var res = 0;
for ( var i = 0; i <= N - M; i++) {
var j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break ;
if (j == M) {
res++;
j = 0;
}
}
return res;
}
function findOccurrence(arr, n, pattern)
{
for ( var i = 0; i < n; i++) {
var binary = decToBinary(arr[i]);
document.write( countFreq(pattern, binary) + " " );
}
}
var arr = [ 5, 106, 7, 8 ];
var pattern = "10" ;
var n = arr.length;
findOccurrence(arr, n, pattern);
</script>
|
Time Complexity: O(n*N*M), where n, N, and M are the length of the given array, text length, and pattern length respectively.
Auxiliary Space: O(1)