Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#19 Bit-manipulation in C++ #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions c++/Bit_Manipulation/Fenwick_Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//Fenwick Tree(Binary Indexed Tree)
//Make a structure to handle Dynamic Range queries
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
/*
LSB-Least Significant Bit
it is the rightmost set Bit in the Binary representation of a number.

One's complement:
it is the value obtained by inverting all the zeros of binary representation of the number

Two's complement:
=One's complement + 1

How LSB can be found?
LSB is the common set bit among the number and it's Two's complement.

How to find Two's complement?
Negative of a number is stored as Two's complement the number in computers.

Hence LSB= num & (-num)
*/

class Fenwick
{
//ith element of the tree stores the sum of last LSB(i) indexes(including ith index)
//where LSB(k) represents the Decimal equivalent of k
vector<ll> tree;
public:
int n;
vector<ll> arr;
//constructor to recieve size of tree and the vector
Fenwick(vector<ll> &tarr)
{
n=tarr.size();
arr.clear();

arr.push_back(0); //Push zero to front of array to make it one based
for(auto &i:tarr)
{
arr.push_back(i);
}

//using one based indexing for tree
tree.resize(n+1);
build();
}

//Build function to add the array values to the tree
//Adding elements using update query for each index
void build()
{
tree[0]=0;
for(int i=1;i<=n;i++)
{
treeupdate_add(i,arr[i]);
}
}

//Function to add val to xth index of arr
//Pass the index and the value to be added
//Time-complexity => O(log(n))
void treeupdate_add(int x,ll val)
{
while(x<=n)
{
tree[x]+=val;
x+=(x&(-x)); //Next update occurs after LSB(x) indexes
}
}

//sum_qurey gives the sum of first x indexes
//Time-Complexity => O(log(n))
ll sum_query(int x)
{
if(x==0)
return 0;
//Since tree[x] stores the sum of last LSB indexes
//we make a recursive call of sum query of first x-LSB(x) indexes.
return tree[x]+sum_query(x -(x&(-x)));
}

//Sum of range [l,r] can be calculated using sum_query(r)-sum_query(l-1)
ll range_sum(int l,int r)
{
return sum_query(r)-sum_query(l-1);
}
};

//Driver code
int main()
{

vector<ll> arr={4,3,2,5,1,6,7,8};
Fenwick f(arr); //passing the array in constructor
//sum of range [3,6]
cout<<f.range_sum(3,6)<<"\n";

f.treeupdate_add(4,6);

cout<<f.range_sum(3,6)<<"\n";

// int n,q;
// cin>>n>>q;
// vector<ll> arr(n);
// for(auto &i:arr) cin>>i;
// Fenwick f(arr);
// for(int i=0;i<q;i++)
// {
// int w;
// cin>>w;
// if(w==1)
// {
// int k,u;
// cin>>k>>u;
// f.treeupdate_add(k,u-arr[k-1]);
// arr[k-1]=u;
// }
// else
// {
// int l,r;
// cin>>l>>r;
// cout<<f.range_sum(l,r)<<"\n";
Comment on lines +105 to +125
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the commented code and add the example along with the output at the end of each code

// }
// }
}
116 changes: 116 additions & 0 deletions c++/Bit_Manipulation/ModularArithmetic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//Modular exponentation and Modular Multiplication
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007

/*
Modular Exponentiation is used to calculate
the value of (a^b)mod m

A number can be written in its binary form as
b=p[0]*2^0 + p[1]*2^1 + p[2]*2^2 .....
where p is the binary representation of b.(p[i] is ith bit from left)
p[i] can be 0 or 1
so (a^b)=(a^(p[0]*2^0))*(a^(p[1]*2^1))*(a^(p[2]*2^2))*....
(a^b)%M=(
(
((a^(2^0))^p[0])%M
*
((a^(2^1))^p[1])%M
)%M
*((a^(2^2))^p[2])%M
)%M*....

This expression is implemented to calculate the value of (a^b) mod M
*/
/*
Modular Multiplication is used to calculate
value of (a*b)%M.

When a and b are of order 10^9 this can be calculated just by (a*1ll*b)%M
but when a*b exceeds the LONG_LONG_MAX limit then we have to use Modular multiplication to calculate (a*b)%M

(a*b)=(a*(p[0]*2^0))+(a*(p[1]*2^1))+(a*(p[2]*2^2))+....

(a*b)%M=(
(
((a*(2^0))*p[0])%M
+
((a*(2^1))*p[1])%M
)%M
+((a*(2^2))*p[2])%M
)%M*....

This expression is implemented to calculate the value of (a*b) mod M

*/

//Modular Exponentiation function such that 0<=a,b,M<=10^9
//Time-Complexity => O(log(b))
int ModularExp(int a,int b,int M)
{
// base a
//power b
//modular M
//(a^b)%M

int ans=1;
while(b>0)
{
//Each time we check the rightmost bit of b and then we right-shift it.
if(b&1)
{
ans=(ans*1ll*a)%M;
}
//In the exxpression it can be seen that value of a gets squared in each step.
a=(a*1ll*a)%M;
b>>=1;
}
return ans;
}

//Modular Multiplication such that 0<=a,b,M<=10^18
//Time-Complexity O(log(b))
long long ModularMultiply_Big(long long a,long long b,long long M)
{
// (a*b) mod M
long long ans=0;
while(b>0)
{
if(b&1)
{
ans=(ans + a)%M;
}
a=(a + a)%M;
b>>=1;
}
return ans;
}

//Modular Exponentiation such that 0<=a,b,M<=10^18
//Time-Complexity => O(log(b)*log(M))
long long ModularExp_Big(long long a,long long b,long long M)
{
long long ans=1;
while(b>0)
{
if(b&1)
{
//Direct multiplication cannot be used as (ans*a) can exceed LONG_LONG_MAX
ans=ModularMultiply_Big(ans,a,M);
}
//Direct multiplication cannot be used as (a*a) can exceed LONG_LONG_MAX
a=ModularMultiply_Big(a,a,M);
b>>=1;
}
return ans;
}

//Driver Code
int main()
{
cout<<ModularMultiply_Big(12233234124ll,1234122324ll,MOD);
cout<<ModularExp_Big(1233444343434ll,123434343434ll,234234224)<<"\n";
cout<<ModularExp(1234,1234,2234224)<<"\n";

}
50 changes: 50 additions & 0 deletions c++/Bit_Manipulation/subset_generation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//Subset Generation using Bitmasks
//Print all the subsets of a given array of n elements n<=20
#include<bits/stdc++.h>
using namespace std;
#define ll long long int

/*
Each element of set can be either chosen or not chosen so for each element we have 2 choices(to take or not take)
hence for a set of n unique elements there are 2^n subsets that can be formed.

Each subset can be represented by a binary representation where 1 represents element is taken and 0 represents element is not taken
so by each integer's binary representation represents a unique subset of the original set.
when the integer is from [0,2^n-1]
*/
//Time-Compleity => O(n*(2^n)) //n is the size of original set
vector<vector<int>> subsets_generate(vector<int>& arr)
{
//here n is arr.size()
long long sz=1<<arr.size(); //sz=2^n

vector<vector<int>> subsets(sz);
for(long long i=0;i<sz;i++) //iterating from 0 to 2^n-1
{
long long temp=1;
//This loop check which bits are set in binary representation of i.
for(int j=0;j<arr.size();j++,temp<<=1)
{
if(i & temp)
subsets[i].push_back(arr[j]); //for every set bit the element is pushed in the subset
}
}
return subsets;
}

//Driver Code
int main()
{
vector<int> arr={3,4,7}; //Sample input array
vector<vector<int>> subsets=subsets_generate(arr);
//Printing each subset in each line
for(auto &i:subsets)
{
cout<<"{";
for(auto &j:i)
{
cout<<j<<" ";
}
cout<<"}\n";
}
}