Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions Power_of_a_number_using_binary_exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
typedef long long int ll;
#define M 1000000007
using namespace std;

// Time Complexity-> O(Log(n))
ll power(ll base, ll n, ll m)
{
ll ans = 1;
base = base % m;
while (n != 0)
{
if (n % 2 == 1)
{
n = n - 1;
ans = (ans * base) % m;
}
else
{
n = n / 2;
base = (base * base) % m;
}
}
return ans;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

cout << power(2, 10, M) << endl;
//(2^10)%M this will be calculated
// 1024
return 0;
}
53 changes: 53 additions & 0 deletions dsu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
typedef long long int ll;
#define M 1000000007
using namespace std;

// Program for disjoint set union

ll parent[100005];
ll sz[100005];

ll findParent(ll node)
{
if (parent[node] == node)
{
return node;
}
return parent[node] = findParent(parent[node]);
}

void uni(ll u, ll v)
{
ll paru = findParent(u);
ll parv = findParent(v);
if (sz[paru] < sz[parv])
{
parent[paru] = parv;
sz[parv] += sz[paru];
}
else
{
parent[parv] = paru;
sz[paru] += sz[parv];
}
}

void init(ll n)
{
for (ll i = 1; i <= n; i++)
{
parent[i] = i;
sz[i] = 1;
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
init(n);
return 0;
}