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
54 changes: 0 additions & 54 deletions Algorithms/binary_exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +0,0 @@
#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

template <class T>
class Algebra{
public:
// Iterative Approach
static T binExpIterative(T a,T b) //a^b
{
T ans=1;
do{
if (b & 1) // 'b' is odd
ans*=a;
b>>=1; //b/=2
a*=a; //a^2
}while(b!=0);
return ans;
}
//Recursive Approach
static T binExpRecursive(T a, T b) //a^b
{
if(b<1)
return 1;
if (b & 1) // 'b' is odd
return a * binExpRecursive(a * a, b / 2);
else
return binExpRecursive(a * a, b / 2);
};

static T modExponentiation(T a,T b,T c) // (a^b)%c
{
T ans=1;
do{
if(b&1)
ans=(ans*a)%c;
b >>= 1; // b/=2
a=a*a%c;
}while(b!=0);
return ans;
}
};


int main()
{
ll a=603,b=1109;
cout<<Algebra<ll>::binExpIterative(a,b)<<endl;

cout << Algebra<ll>::binExpRecursive(a, b) << endl;

cout << Algebra<ll>::modExponentiation(a, b,2881) << endl;
}
63 changes: 26 additions & 37 deletions Algorithms/max_sliding_window.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,39 @@
// Program for max sliding window
#include <bits/stdc++.h>
using namespace std;
// Comparator to sort the vector of pair with increasing first and decreasing second values
bool comparator(pair<int, int> a, pair<int, int> b)
{
if (a.first == b.first)
{
return a.second > b.second;
}
return a.first < b.first;
}
// k-> size of the window
vector<int> maxSlidingWindow(vector<int> &nums, int k)
{
// Create a priority queue with comparator
priority_queue<pair<int, int>, vector<pair<int, int>>, function<bool(pair<int, int>, pair<int, int>)>> pq(comparator);

int n = nums.size();
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int> dq;
vector<int> windowMax;
// Insert first K elements in priority queue
for (int i = 0; i < k; i++)
{
pq.push({nums[i], i});
}
windowMax.push_back(pq.top().first);
// Now slide over remaining elements
for (int i = k; i < n; i++)
{
int n = nums.size();

pq.push({nums[i], i});
// Loop until priority queue top element index comes undes the range
while (pq.top().second <= i - k)
{
pq.pop();
for (int i = 0; i < n; ++i) {
// Remove elements not within the sliding window
if (!dq.empty() && dq.front() == i - k) {
dq.pop_front();
}

// Remove elements smaller than the current element from the back of the deque
while (!dq.empty() && nums[dq.back()] < nums[i]) {
dq.pop_back();
}

// Add the current element index to the deque
dq.push_back(i);

// The front of the deque is the largest element in the current window
if (i >= k - 1) {
windowMax.push_back(nums[dq.front()]);
}
windowMax.push_back(pq.top().first);
}

return windowMax;
}

int main()
{
int main() {
vector<int> arr = {1, 1, 5, 2, 1, 0, 1, 3, 2, 1, 2, 1};
int k=4;
vector<int> max_arr = maxSlidingWindow(arr,k);
for(auto e : max_arr){
cout<<e<<" ";
int k = 4;
vector<int> max_arr = maxSlidingWindow(arr, k);
for (auto e : max_arr) {
cout << e << " ";
}
}