Skip to content

Commit 73a70ba

Browse files
committed
Add Some Array Manipulation in CPP
1 parent 655bbd8 commit 73a70ba

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

public/data/cpp.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,90 @@
1616
"author": "Vaibhav-kesarwani"
1717
}
1818
]
19+
},
20+
{
21+
"categoryName": "Array Manipulation",
22+
"snippets": [
23+
{
24+
"title": "Find Minimum and Maximum Values in an Array",
25+
"description": "Finds the minimum and maximum values in an array using only a single loop.",
26+
"code": [
27+
"void findMinMax(const int arr[], int size, int& min, int& max) {",
28+
" min = arr[0];",
29+
" max = arr[0];",
30+
" for (int i = 1; i < size; i++) {",
31+
" if (arr[i] < min) {",
32+
" min = arr[i];",
33+
" } else if (arr[i] > max) {",
34+
" max = arr[i];",
35+
" }",
36+
" }",
37+
"}"
38+
],
39+
"tags": ["cpp", "array", "loop", "min-max", "min", "max"],
40+
"author": "shovan04"
41+
},
42+
{
43+
"title": "Find Second Largest Element in an Array",
44+
"description": "Finds the second largest element in an array using only a single loop.",
45+
"code": [
46+
"int findSecondLargest(const int arr[], int size) {",
47+
" int largest = INT_MIN;",
48+
" int secondLargest = INT_MIN;",
49+
" for (int i = 0; i < size; i++) {",
50+
" if (arr[i] > largest) {",
51+
" secondLargest = largest;",
52+
" largest = arr[i];",
53+
" } else if (arr[i] < largest && arr[i] > secondLargest) {",
54+
" secondLargest = arr[i];",
55+
" }",
56+
" }",
57+
" return secondLargest;",
58+
"}"
59+
],
60+
"tags": ["cpp", "array", "loop", "Largest", "Second Largest"],
61+
"author": "shovan04"
62+
},
63+
{
64+
"title": "Sort Array in Descending Order",
65+
"description": "Sorts an array in descending order using the Bubble Sort algorithm.",
66+
"code": [
67+
"void bubbleSortDescending(int arr[], int size) {",
68+
" for (int i = 0; i < size - 1; i++) {",
69+
" for (int j = 0; j < size - i - 1; j++) {",
70+
" if (arr[j] < arr[j + 1]) {",
71+
" std::swap(arr[j], arr[j + 1]);",
72+
" }",
73+
" }",
74+
" }",
75+
"}"
76+
],
77+
"tags": ["cpp", "array", "bubble-sort", "descending", "swap"],
78+
"author": "shovan04"
79+
},
80+
{
81+
"title": "Find Common Elements in Two Arrays",
82+
"description": "Finds the common elements in two arrays using a single loop and without using any additional data structures.",
83+
"code": [
84+
"#include <climits>",
85+
"",
86+
"",
87+
"void findCommonElements(const int arr1[], int size1, int arr2[], int size2, int& commonCount) {",
88+
"commonCount = 0; // Initialize the count of common elements",
89+
"for (int i = 0; i < size1; i++) {",
90+
"for (int j = 0; j < size2; j++) {",
91+
"if (arr1[i] == arr2[j]) { // Compare elements from both arrays",
92+
"commonCount++;",
93+
"arr2[j] = INT_MAX; // Mark the element in arr2 as found",
94+
"break; // Stop checking once a match is found",
95+
"}",
96+
" }",
97+
"}",
98+
"}"
99+
],
100+
"tags": ["cpp", "array", "common", "loop", "mark-found"],
101+
"author": "shovan04"
102+
}
103+
]
19104
}
20105
]

0 commit comments

Comments
 (0)