-
-
Notifications
You must be signed in to change notification settings - Fork 130
Add Some Array Manipulation in CPP #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
"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) {", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of this function too, |
||
"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" | ||
} | ||
] | ||
} | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?