A

@Adarsh1999

Write a C++ program to check whether numbers in a vector can be rearranged so that each number appea

C++
2 years ago
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> arr = {1,3,2,4,5,7}; std::sort(arr.begin(),arr.end()); for(int i=0; i<arr.size()-1; i++){

rec & loop sort and remove dup integers

C++
2 years ago
#include <iostream> #include <vector> #include <algorithm> // Helper function to remove duplicates void rmdupHelper(std::vector<int> &arr, int n) { // Base case: If we've reached the end of the vector, return if (n >= arr.size() - 1) {

Matrix basics

C++
2 years ago
#include <iostream> #include <vector> int main() { std::vector<std::vector<int>> matrix(3, std::vector<int>(3,0)); for(int i=0; i<3;i++){ for(int j=0; j<3;j++){ matrix[i][j] = j; matrix[i][j] = i;

subset backtraking

C++
2 years ago
#include <iostream> #include <vector> // Function to generate subarrays recursively void subarr(const std::vector<int>& arr, int i, std::vector<int>& tmp) { // base case if (i == arr.size()) { // If not empty, print the current suba

Graph 1

C++
2 years ago
#include <iostream> #include <list> using namespace std; class Graph{ int V; list<int> *l; public: Graph(int v){

Base Case

C++
2 years ago
The base case is a term used in computer science, especially in the context of recursion, which means a situation where the problem can be solved directly without needing further recursion. Imagine you are climbing a ladder. The base case is like t

Number Spell

C++
2 years ago
#include <iostream> std::string spell[] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten"}; void printn(int n){ if(n==0){ return; } int ld = n%10; //ld = 9876 % 10 gives 6//ld = 987 % 10 gives 7//

increment and dec

C++
2 years ago
#include <iostream> int dec(int n){ if(n==0){ return n; } std::cout<<n<<' '; dec(n-1); return false;

Check if array sorted

C++
2 years ago
#include <iostream> #include <vector> bool issort(std::vector<int>& arr, int n) { // base case if (n == 0 || n == 1) { std::cout << "No elements or single element array" << std::endl; return true; }

Factorial

C++
2 years ago
#include <iostream> int fact(int n){ if(n==0){ return 1; } int sum = n*fact(n-1); return sum; }

Recursion

C++
2 years ago
#include <iostream> #include <vector> void sort(std::vector<int> &arr, int n, int j){ if(n==1){ return; } if(arr[j]<arr[j+1]){ int tmp = arr[j];

vector class

C++
2 years ago
#include <iostream> #include <stdexcept> template <typename T> class MyVector { private: T* data; // Pointer to the data array size_t vec_size; // Number of elements in the vector size_t vec_capacity; // Capacity of the vecto

Reallocation of vector

C++
2 years ago
#include <iostream> #include <vector> int main() { std::vector<int> myVector; size_t capacity = myVector.capacity(); std::cout << "Initial capacity: " << capacity << std::endl; // Add elements to the vector and monitor reallocatio

const and class

C++
2 years ago
#include <iostream> #include <vector> #include <cstring> #include <string> void consta(){ std::cout<<"const called"<<std::endl; } class constclass{

Thread ex imp

C++
2 years ago
#include <iostream> #include <mutex> #include <thread> std::mutex mMutex; // Define a mutex int sharedData = 0; // Shared data void incrementData() { std::lock_guard<std::mutex> lock(mMutex); // Lock the mutex

Try Catch example

C++
2 years ago
#include <iostream> #include <stdexcept> int divide(int numerator, int denominator) { if (denominator == 0) { throw std::runtime_error("Division by zero is not allowed."); } return numerator / denominator; }

Map vs Enum

C++
2 years ago
The concept of a std::map is somewhat similar to how an enumeration (enum) works in the sense that both involve associating values with some form of identifier. However, there are some key differences: std::map: A std::map is a data structure that

Map Basics

C++
2 years ago
/*In C++, std::map is a data structure that represents an associative container, which stores key-value pairs in a sorted order based on the keys. It is part of the Standard Template Library (STL) and is included in the <map> header. Here are some

string and int vector

C++
2 years ago
#include<bits/stdc++.h> using namespace std; int main() { std::vector<std::string> str; str.push_back("adarsh"); str.push_back("name"); str.push_back("bahsbd");

imp cons and dist

C++
2 years ago
1. Default Construtor #include <iostream> class code{ public: code(){//constructor std::cout<<"hello"<<std::endl; } ~code(){//distructor