diff --git a/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem1/GeeksAndGroceries.cpp b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem1/GeeksAndGroceries.cpp new file mode 100644 index 0000000..7447b70 --- /dev/null +++ b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem1/GeeksAndGroceries.cpp @@ -0,0 +1,45 @@ +// +// Created by ronal on 2/11/2023. +// Problem Description link. +// https://practice.geeksforgeeks.org/problems/geeks-and-groceries/0 + +#include + +using namespace std; + +void PrintProducts(vector> &P, int qnt){ + vector S; + + for(auto value : P){ + if(S.size() < qnt){ + S.push_back(value.second); + }else{ + break; + } + } + + for(auto products : S){ + cout << products << " "; + } + cout << "\n"; +} +int main() { + vector> produtos; + int tests, qnt_pro, choose_produ, price; + string value; + + cin >> tests; + for(int i = 0; i < tests; i++){ + cin >> qnt_pro >> choose_produ; + + for(int j = 0; j < qnt_pro; j++){ + cin >> value >> price; + produtos.emplace_back(price, value); + } + sort(produtos.begin(), produtos.end()); + PrintProducts(produtos, choose_produ); + produtos.clear(); + } + return 0; +} + diff --git a/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem2/KLargerValues.cpp b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem2/KLargerValues.cpp new file mode 100644 index 0000000..3684b82 --- /dev/null +++ b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem2/KLargerValues.cpp @@ -0,0 +1,32 @@ +// +// Created by ronal on 2/11/2023. +// Problem Description Link. +// https://practice.geeksforgeeks.org/problems/k-larger-values/0 + +#include +using namespace std; + +void PrintValuesMoreThanKey(vector &v, int key){ + for(int i = 0; i < key; i++){ + cout << v[i] << " "; + } + cout << "\n"; + v.clear(); +} +int main() { + vector v; + int tests, size, key, value; + + cin >> tests; + for(int i = 0; i < tests; i++){ + cin >> size >> key; + for(int j = 0; j < size; j++){ + cin >> value; + v.push_back(value); + } + + sort(v.begin(), v.end(), greater()); + PrintValuesMoreThanKey(v, key); + } + return 0; +} diff --git a/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem3/KLargerValuesTwo.cpp b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem3/KLargerValuesTwo.cpp new file mode 100644 index 0000000..2d0ae50 --- /dev/null +++ b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem3/KLargerValuesTwo.cpp @@ -0,0 +1,39 @@ +// +// Created by ronal on 2/11/2023. +// Problem Decription Link. +// https://practice.geeksforgeeks.org/problems/k-larger-values-1611827113/0 + +#include + +using namespace std; + +void PrintSet(set> &S, int key){ + vector v; + for(auto value : S){ + v.push_back(value); + } + + if(v.size() < key){ + cout << "-1"; + }else{ + for(int i = 0; i < key; i++){ + cout << v[i] << " "; + } + } + cout << "\n"; +} +int main() { + set> S; + int tests, size, key, value; + cin >> tests; + for(int i = 0; i < tests; i++){ + cin >> size >> key; + for(int j = 0; j < size; j++){ + cin >> value; + S.insert(value); + } + PrintSet(S, key); + S.clear(); + } + return 0; +} diff --git a/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem4/KConsecutiveOddElements.cpp b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem4/KConsecutiveOddElements.cpp new file mode 100644 index 0000000..84c9b7c --- /dev/null +++ b/SearchingSortingandBasicDataStructures/InbuiltSorting/Problem4/KConsecutiveOddElements.cpp @@ -0,0 +1,52 @@ +// +// Created by ronal on 2/11/2023. +// Problem Description Link. +// https://practice.geeksforgeeks.org/problems/k-consecutive-odd-elements/0 + +#include +using namespace std; + +void PrintResult(vector &v, int key){ + int count = 0; + for(int i = 0; i < v.size(); i++){ + if(v[i] % 2 != 0){ + count++; + }else{ + count = 0; + } + if(count == key){ + break; + }else{ + continue; + } + } + + if(count == key){ + cout << "yes"; + }else{ + cout << "no"; + } + cout << "\n"; + +} + +int main() { + vector v; + int tests, size, key, value; + + cin >> tests; + for(int i = 0; i < tests; i++){ + cin >> size >> key; + + for(int j = 0; j < size; j++){ + cin >> value; + v.push_back(value); + } + + sort(v.begin(), v.end()); + PrintResult(v, key); + v.clear(); + } + return 0; +} +