From 057f4be3f0c154b23f7ebc940465a9521149a74d Mon Sep 17 00:00:00 2001 From: karan siddhu Date: Thu, 28 Apr 2022 07:56:53 +0530 Subject: [PATCH] Added new question in Array section --- .../code.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Learning C++ with DSA/4. Arrays/Questions/12. Frequencies in A Sorted Array/code.cpp diff --git a/Learning C++ with DSA/4. Arrays/Questions/12. Frequencies in A Sorted Array/code.cpp b/Learning C++ with DSA/4. Arrays/Questions/12. Frequencies in A Sorted Array/code.cpp new file mode 100644 index 0000000..2a74c6e --- /dev/null +++ b/Learning C++ with DSA/4. Arrays/Questions/12. Frequencies in A Sorted Array/code.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +#define ll long long +#define db double +#define ld long double +#define el "\n" + +//Time Complexity: O(N) +void solution(){ + int n = 6; + int a[n] = {10, 10, 10, 25, 30, 30}; + + int ans = 1; + for (int i = 1; i < n; i++){ + if(a[i] == a[i-1]){ + ans++; + }else{ + cout << a[i-1] << " - " << ans << el; + ans = 1; + } + } + cout << a[n-1] << " - " << ans << el; +} + +int main(){ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + while (t--) + solution(); + + return 0; +}