From 66df4898dbb699766e3ae72546ba0a8729d0cd7b Mon Sep 17 00:00:00 2001 From: Priya11gupta <74971761+Priya11gupta@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:58:58 +0530 Subject: [PATCH 1/2] Create duplicate.cpp --- DSA cpp/duplicate.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 DSA cpp/duplicate.cpp diff --git a/DSA cpp/duplicate.cpp b/DSA cpp/duplicate.cpp new file mode 100644 index 0000000..a4cf0b0 --- /dev/null +++ b/DSA cpp/duplicate.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; +int findDuplicate(vector < int > & nums) { + int slow = nums[0]; + int fast = nums[0]; + do { + slow = nums[slow]; + fast = nums[nums[fast]]; + } while (slow != fast); + fast = nums[0]; + while (slow != fast) { + slow = nums[slow]; + fast = nums[fast]; + } + return slow; +} +int main() { + vector < int > arr; + arr = {1,3,4,2,3}; + cout << "The duplicate element is " << findDuplicate(arr) << endl; +} From 71617432ff05302b2fe509bc6c1056967414408d Mon Sep 17 00:00:00 2001 From: Priya11gupta <74971761+Priya11gupta@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:02:19 +0530 Subject: [PATCH 2/2] Create largest.cpp --- DSA cpp/largest.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 DSA cpp/largest.cpp diff --git a/DSA cpp/largest.cpp b/DSA cpp/largest.cpp new file mode 100644 index 0000000..b56afa0 --- /dev/null +++ b/DSA cpp/largest.cpp @@ -0,0 +1,55 @@ +#include +using namespace std ; + +class Solution { + +public: + + void kth_Largest_MaxHeap(vector&arr, int k) { + + priority_queuepq ; + int n = arr.size() ; + + for (int i = 0; i < arr.size(); i++) { + pq.push(arr[i]) ; + } + + int f = k - 1 ; + + while (f > 0) { + pq.pop() ; + f-- ; + } + + cout << "Kth Largest element " << pq.top() << "\n" ; + } + + void kth_Smallest_MinHeap(vector&arr, int k) { + + priority_queue, greater>pq ; + int n = arr.size() ; + + for (int i = 0; i < arr.size(); i++) { + pq.push(arr[i]) ; + } + + int f = k - 1 ; + + while (f > 0) { + pq.pop() ; + f-- ; + } + + cout << "Kth Smallest element " << pq.top() << "\n" ; + } +} ; +int main() { + + vectorarr = {1, 2, 6, 4, 5, 3} ; + + Solution obj ; + obj.kth_Largest_MaxHeap(arr, 3) ; + obj.kth_Smallest_MinHeap(arr, 3) ; + + return 0 ; +}