From d408c50a2acab67913b869ad12ffbe04c2729e86 Mon Sep 17 00:00:00 2001 From: Arpit Date: Sun, 30 Oct 2022 19:29:06 +0530 Subject: [PATCH] added leader in an array problem solution --- Cpp/leader-array.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Cpp/leader-array.cpp diff --git a/Cpp/leader-array.cpp b/Cpp/leader-array.cpp new file mode 100644 index 0000000..f202c84 --- /dev/null +++ b/Cpp/leader-array.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +class Solution{ + + //Function to find the leaders in the array. + + public: + + vector leaders(int a[], int n){ + + // Code here + vector vec; + int max = a[n-1]; + vec.push_back(max); + + for(int i=n-2;i>=0;i--) + { + + if(a[i]>=max ) + + { + max=a[i]; + vec.push_back(a[i]); + } + + } + + reverse(vec.begin(), vec.end()); + + return vec; + } +}; \ No newline at end of file