#include <iostream>
Saturday, April 12, 2025
Counting Sort in C++
Insertion Sort in C++
#include <iostream>
Selection Sort in C++
#include <iostream>
Friday, April 11, 2025
Bubble Sort in C++
Check duplicate numbers return 1 if true, return 0 if false in C++ (Array Assignment) - Full Explaination
That's the spirit, Debadatta! 🔥 Let's break down your teacher's code step-by-step and understand everything clearly, like a pro. 💪
✅ Full Teacher’s Code First:
#include <iostream>
#include <vector>
#include <algorithm> // for sort
using namespace std;
// Function to check for duplicates
bool containsDuplicate(vector<int> nums) {
sort(nums.begin(), nums.end()); // Step 1: Sort the array
for (int i = 1; i < nums.size(); i++) { // Step 2: Check adjacent elements
if (nums[i - 1] == nums[i]) {
return true; // Duplicate found
}
}
return false; // No duplicates found
}
int main() {
vector<int> numbers = {1, 1, 2, 3, 4, 5}; // Sample input
if (containsDuplicate(numbers)) {
cout << "Duplicate found" << endl;
} else {
cout << "No duplicates" << endl;
}
return 0;
}
🧠 Step-by-step Breakdown:
🟩 #include and setup:
#include <iostream>
#include <vector>
#include <algorithm>
-
iostream: Forcin,cout. -
vector: A dynamic array that grows/shrinks. -
algorithm: Containssort()function.
🟦 Function Header:
bool containsDuplicate(vector<int> nums)
-
This function takes a copy of a vector of integers.
-
It returns a
bool—trueif a duplicate is found,falseotherwise.
🟨 Step 1: Sort the array
sort(nums.begin(), nums.end());
-
Sorting makes it easy to compare each number with its neighbor.
-
After sorting:
{1, 1, 2, 3, 4, 5}
🟧 Step 2: Check each pair
for (int i = 1; i < nums.size(); i++) {
if (nums[i - 1] == nums[i]) {
return true;
}
}
-
Start from index 1 (second element)
-
Compare
nums[i - 1]andnums[i] -
If they are equal, it means a duplicate exists.
🟥 Step 3: No duplicates? Return false
return false;
-
If loop finishes and no duplicate is found, return
false.
🟦 main() Function:
int main() {
vector<int> numbers = {1, 1, 2, 3, 4, 5};
if (containsDuplicate(numbers)) {
cout << "Duplicate found" << endl;
} else {
cout << "No duplicates" << endl;
}
return 0;
}
-
Create a vector
numbers -
Call the function
containsDuplicate(numbers) -
Print message based on the result
🔁 Example Runs:
| Input | Output |
|---|---|
{1, 1, 2, 3} |
Duplicate found |
{1, 2, 3, 4} |
No duplicates |
{9, 8, 9, 7} |
Duplicate found |
🔍 Summary of Logic:
| Step | What it does |
|---|---|
| 1 | Sort the array |
| 2 | Loop through it |
| 3 | Compare each number to its previous one |
| 4 | If equal → duplicate found |
| 5 | Else → return false |
Thursday, April 10, 2025
Finding Factorial in C++
#include <iostream>
using namespace std;
int main() {
int n, i, f = 1;
cout << "enter a num : ";
cin >> n;
if( n == 0 ) { // n should be assigned to 0
cout << "Factorial; " << f << endl;
} else {
for(i = 1; i <= n; i++) {
f = f*i;
}
cout << "Factorial: " << f << endl; // result come outside the for loop, inside for loop you only see the process BTS
}
return 0;
}
Wednesday, April 9, 2025
Buy and Sell Stocks (Code) in C++
#include<iostream>
how to become red on codeforces by Errichto Algorithms | explained simply in a blog
Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...
-
public class Main { public static void printSolution(int sol[][]) { for (int i = 0; i < sol.length; i++) { for (...
-
Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...
-
#include <iostream> #include <vector> using namespace std ; // conquer step - O(n) void merge ( int arr[], int si, int mid...