Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions public/data/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,90 @@
"author": "Vaibhav-kesarwani"
}
]
},
{
"categoryName": "Array Manipulation",
"snippets": [
{
"title": "Find Minimum and Maximum Values in an Array",
"description": "Finds the minimum and maximum values in an array using only a single loop.",
"code": [
"void findMinMax(const int arr[], int size, int& min, int& max) {",
" min = arr[0];",
" max = arr[0];",
" for (int i = 1; i < size; i++) {",
" if (arr[i] < min) {",
" min = arr[i];",
" } else if (arr[i] > max) {",
" max = arr[i];",
" }",
" }",
"}"
],
"tags": ["cpp", "array", "loop", "min-max", "min", "max"],
"author": "shovan04"
},
{
"title": "Find Second Largest Element in an Array",
Copy link
Collaborator

@saminjay saminjay Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this function, can you make a generalised function that returns Largest k elements?

"description": "Finds the second largest element in an array using only a single loop.",
"code": [
"int findSecondLargest(const int arr[], int size) {",
" int largest = INT_MIN;",
" int secondLargest = INT_MIN;",
" for (int i = 0; i < size; i++) {",
" if (arr[i] > largest) {",
" secondLargest = largest;",
" largest = arr[i];",
" } else if (arr[i] < largest && arr[i] > secondLargest) {",
" secondLargest = arr[i];",
" }",
" }",
" return secondLargest;",
"}"
],
"tags": ["cpp", "array", "loop", "Largest", "Second Largest"],
"author": "shovan04"
},
{
"title": "Sort Array in Descending Order",
"description": "Sorts an array in descending order using the Bubble Sort algorithm.",
"code": [
"void bubbleSortDescending(int arr[], int size) {",
" for (int i = 0; i < size - 1; i++) {",
" for (int j = 0; j < size - i - 1; j++) {",
" if (arr[j] < arr[j + 1]) {",
" std::swap(arr[j], arr[j + 1]);",
" }",
" }",
" }",
"}"
],
"tags": ["cpp", "array", "bubble-sort", "descending", "swap"],
"author": "shovan04"
},
{
"title": "Find Common Elements in Two Arrays",
"description": "Finds the common elements in two arrays using a single loop and without using any additional data structures.",
"code": [
"#include <climits>",
"",
"",
"void findCommonElements(const int arr1[], int size1, int arr2[], int size2, int& commonCount) {",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this function too,
It doesn't return the common elements but the number of common elements.
It also modifies one the array which can be non desired behavior...

"commonCount = 0; // Initialize the count of common elements",
"for (int i = 0; i < size1; i++) {",
"for (int j = 0; j < size2; j++) {",
"if (arr1[i] == arr2[j]) { // Compare elements from both arrays",
"commonCount++;",
"arr2[j] = INT_MAX; // Mark the element in arr2 as found",
"break; // Stop checking once a match is found",
"}",
" }",
"}",
"}"
],
"tags": ["cpp", "array", "common", "loop", "mark-found"],
"author": "shovan04"
}
]
}
]