Count of paths in given Binary Tree with odd bitwise AND for Q queries
Last Updated :
16 Aug, 2021
Given an integer Q representing the number of queries and an array where each query has an integer N. Our task is to iterate through each query and find the number of paths such that bitwise AND of all nodes on that path is odd.
A binary tree is constructed with N vertices numbered 1 through N. For each i, from 2 to N, there is an edge between vertex i and vertex i / 2 (rounded off).
Examples:
Input: Q = 2, [5, 2]
Output: 1 0
Explanation:
For first query the binary tree will be
1
/ \
2 3
/ \
4 5
The path which satisfies
the condition is 1 -> 3.
Hence only 1 path.
For the second query,
the binary tree will be
1
/
2
There no such path that
satisfies the condition.
Input: Q = 3, [3, 7, 13]
Output: 1 3 4
Approach: The idea is to use Dynamic Programming and precomputation of answers for all values from 1 to the maximum value of N in the query.
- Firstly, observe that if a bitwise AND of a path is odd, then none elements of that path can be even. Hence, the required path should have odd elements.
- We know that for an ith node (except for node 1) the parent node will be i/2 (rounded off). Maintain a dp array for storing the answer for the ith node. Another array will be used to store the number of odd elements from the current node until the parents are odd.
- While computing the dp array, the first condition is if the ith node value is even, then dp[i] = dp[i – 1] because the ith node will not contribute to the answer, so dp[i] will be the answer to (i-1)th node. Secondly, if ith node is odd, then dp[i] = dp[i-1] + nC2 -(n-1)C2. On simplification dp[i] = dp[i-1] + (number of odd elements till now) – 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void compute(vector< int > query)
{
vector< int > v(100001), dp(100001);
v[1] = 1, v[2] = 0;
dp[1] = 0, dp[2] = 0;
for ( int i = 3; i < 100001; i++) {
if (i % 2 != 0) {
if ((i / 2) % 2 == 0) {
v[i] = 1;
dp[i] = dp[i - 1];
}
else {
v[i] = v[i / 2] + 1;
dp[i] = dp[i - 1] + v[i] - 1;
}
}
else {
v[i] = 0;
dp[i] = dp[i - 1];
}
}
for ( auto x : query)
cout << dp[x] << endl;
}
int main()
{
vector< int > query = { 5, 2 };
compute(query);
return 0;
}
|
Java
class GFG{
static void compute( int [] query)
{
int []v = new int [ 100001 ];
int []dp = new int [ 100001 ];
v[ 1 ] = 1 ; v[ 2 ] = 0 ;
dp[ 1 ] = 0 ; dp[ 2 ] = 0 ;
for ( int i = 3 ; i < 100001 ; i++)
{
if (i % 2 != 0 )
{
if ((i / 2 ) % 2 == 0 )
{
v[i] = 1 ;
dp[i] = dp[i - 1 ];
}
else
{
v[i] = v[i / 2 ] + 1 ;
dp[i] = dp[i - 1 ] + v[i] - 1 ;
}
}
else
{
v[i] = 0 ;
dp[i] = dp[i - 1 ];
}
}
for ( int x : query)
System.out.print(dp[x] + "\n" );
}
public static void main(String[] args)
{
int []query = { 5 , 2 };
compute(query);
}
}
|
Python3
def compute(query):
v = [ None ] * 100001
dp = [ None ] * 100001
v[ 1 ] = 1
v[ 2 ] = 0
dp[ 1 ] = 0
dp[ 2 ] = 0
for i in range ( 3 , 100001 ):
if (i % 2 ! = 0 ):
if ((i / / 2 ) % 2 = = 0 ):
v[i] = 1
dp[i] = dp[i - 1 ]
else :
v[i] = v[i / / 2 ] + 1
dp[i] = dp[i - 1 ] + v[i] - 1
else :
v[i] = 0
dp[i] = dp[i - 1 ]
for x in query:
print (dp[x])
query = [ 5 , 2 ]
compute(query)
|
C#
using System;
class GFG{
static void compute( int [] query)
{
int []v = new int [100001];
int []dp = new int [100001];
v[1] = 1; v[2] = 0;
dp[1] = 0; dp[2] = 0;
for ( int i = 3; i < 100001; i++)
{
if (i % 2 != 0)
{
if ((i / 2) % 2 == 0)
{
v[i] = 1;
dp[i] = dp[i - 1];
}
else
{
v[i] = v[i / 2] + 1;
dp[i] = dp[i - 1] + v[i] - 1;
}
}
else
{
v[i] = 0;
dp[i] = dp[i - 1];
}
}
foreach ( int x in query)
Console.Write(dp[x] + "\n" );
}
public static void Main(String[] args)
{
int []query = { 5, 2 };
compute(query);
}
}
|
Javascript
<script>
function compute(query)
{
var v = Array(100001).fill(0);
var dp = Array(100001).fill(0);
v[1] = 1, v[2] = 0;
dp[1] = 0, dp[2] = 0;
for ( var i = 3; i < 100001; i++) {
if (i % 2 != 0) {
if (parseInt(i / 2) % 2 == 0) {
v[i] = 1;
dp[i] = dp[i - 1];
}
else {
v[i] = v[parseInt(i / 2)] + 1;
dp[i] = dp[i - 1] + v[i] - 1;
}
}
else {
v[i] = 0;
dp[i] = dp[i - 1];
}
}
query.forEach(x => {
document.write( dp[x] + "<br>" );
});
}
var query = [5, 2];
compute(query);
</script>
|
Time Complexity: O( Nmax + Q*(1) ), where Nmax is the maximum value of N. Q*(1) since we are precomputing each query.
Auxiliary Space: O(Nmax)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...