From 43afdc3fb8bd88acd0e19f3e7325cea302dc56c5 Mon Sep 17 00:00:00 2001 From: Anshuman7080 <164294129+Anshuman7080@users.noreply.github.com> Date: Sun, 13 Oct 2024 15:32:11 +0000 Subject: [PATCH] Deque for Efficient Window Management ( O(N) Time Complexity) --- Algorithms/binary_exponentiation.cpp | 54 ------------------------ Algorithms/max_sliding_window.cpp | 63 ++++++++++++---------------- 2 files changed, 26 insertions(+), 91 deletions(-) diff --git a/Algorithms/binary_exponentiation.cpp b/Algorithms/binary_exponentiation.cpp index 71ce2a6..e69de29 100644 --- a/Algorithms/binary_exponentiation.cpp +++ b/Algorithms/binary_exponentiation.cpp @@ -1,54 +0,0 @@ -#include - -using namespace std; -typedef long long int ll; - -template -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<::binExpIterative(a,b)<::binExpRecursive(a, b) << endl; - - cout << Algebra::modExponentiation(a, b,2881) << endl; -} diff --git a/Algorithms/max_sliding_window.cpp b/Algorithms/max_sliding_window.cpp index a4b5adb..47bccd0 100644 --- a/Algorithms/max_sliding_window.cpp +++ b/Algorithms/max_sliding_window.cpp @@ -1,50 +1,39 @@ -// Program for max sliding window #include using namespace std; -// Comparator to sort the vector of pair with increasing first and decreasing second values -bool comparator(pair a, pair b) -{ - if (a.first == b.first) - { - return a.second > b.second; - } - return a.first < b.first; -} -// k-> size of the window -vector maxSlidingWindow(vector &nums, int k) -{ - // Create a priority queue with comparator - priority_queue, vector>, function, pair)>> pq(comparator); - int n = nums.size(); +vector maxSlidingWindow(vector& nums, int k) { + deque dq; vector 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 arr = {1, 1, 5, 2, 1, 0, 1, 3, 2, 1, 2, 1}; - int k=4; - vector max_arr = maxSlidingWindow(arr,k); - for(auto e : max_arr){ - cout< max_arr = maxSlidingWindow(arr, k); + for (auto e : max_arr) { + cout << e << " "; } }