Found 7197 Articles for C++

All unique triplets that sum up to a given value in C++

Manisha Chand
Updated on 31-Jul-2025 15:40:28

442 Views

Unique Triplets that Sum up to a Given Value In this article, we will discuss different approaches to find all the unique triplets that sum up to the given value. Before that, first understand the given problem. We have been given an integer array with N elements, along with a target value. Our task is to find unique or distinct triplets (i.e., three numbers) from an array, whose sum is the same as the target value. Let's take a scenario to understand the problem in a better way: Scenario 1 Input: arr ={1, 2, 3, 4, 5, 6, 7} ... Read More

Advantages of vector over array in C++

Nishu Kumari
Updated on 23-Jul-2025 17:42:11

423 Views

In C++, both arrays and vectors are used to store elements, but the main difference is that arrays have a fixed size and cannot be changed once initialized. Whereas, vectors are dynamic, i., e we can change their size during runtime. In this article, we'll look at the advantages of using vectors over arrays in C++. Here's how we declare an array and a vector in C++: // Declaring an array int arr[5]; // Fixed-size array of 5 integers // Declaring a vector #include std::vector vec; // Dynamic vector of integers Advantages of ... Read More

Addition and Subtraction of Matrix using pthreads in C/C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:19:09

921 Views

Here we will see how to perform the matrix addition and subtraction using multithreaded environment. The pthread is used to execute multiple threads simultaneously in C or C++.There are two matrices A and B. Order of each matrix is (m x n). Each thread will take each row, and perform addition or subtraction. So for m rows, there are m different threads.Example#include #include #include #include #define CORE 3 #define MAX 3 using namespace std; int AMat[MAX][MAX] = {{10, 20, 30},    {40, 50, 60},    {70, 80, 50} }; int BMat[MAX][MAX] = {{80, 60, 20},    {30, ... Read More

C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2

Arnab Chakraborty
Updated on 20-Aug-2019 07:15:14

171 Views

Here we will see how to get the sum of the series with n-th term as n2 – (n-1)2. The recurrence relation is like below −Tn = n2 − (n−1)2So the series is −We need to find S mod (109 + 7), where S is the sum of all terms of the given series.Example#include #define X 1000000007 using namespace std; long long getSum(long long n) {    return ((n % X) * (n % X)) % X; } int main() {    long long n = 56789;    cout

Hidden tricks of C++ related to STL

Arnab Chakraborty
Updated on 20-Aug-2019 07:07:31

256 Views

Here we will see some hidden tricks of C++ related to STL.Assign value of pairs using braces ‘{}’. We can use them to assign into tuples also.pair my_pair = make_pair(10, 20); pair my_pair2 = { 10, 20 }; //using braces pair my_pair3 = { 10, { 'A', 20 } }; //complex pairSometimes we do not remember to include lots of headers, or sometimes we forget the names of the headers, in that time we can follow this trick to include all headers.#include C++ has inbuilt GCD function. That function is not so popular so we do not know about it. ... Read More

C++: Methods of code shortening in competitive programming?

Arnab Chakraborty
Updated on 19-Aug-2019 14:35:42

426 Views

In this section we will see some examples of code shortening strategy for competitive programming. Suppose we have to write some large amount of codes. In that code, we can follow some strategy to make them more short.We can change the type-name to make it short. Please check the code to get the ideaExample Code#include using namespace std; int main() {    long long x = 10;    long long y = 50;    cout

A C/C++ Pointer Puzzle?

Arnab Chakraborty
Updated on 19-Aug-2019 14:15:22

393 Views

Suppose we have one integer variable whose size is 4 byte, another pointer variable is there, whose size is 8 bytes. So what will be the output of the following?Example#include using namespace std; main() {    int a[4][5][6];    int x = 0;    int* a1 = &x;    int** a2 = &a1;    int*** a3 = &a2;    cout

C/C++ Program to find the sum of elements in a given array

sudhir sharma
Updated on 19-Aug-2019 11:44:58

1K+ Views

Sum of all Array elements means add all array Elements. Suppose we have 5 Elements in array and we want to find there sum.arr[0]=1 arr[1]=2 arr[2]=3 arr[3]=4 arr[4]=5Sum off all above elements arearr[0]+arr[1]+arr[2]+arr[3]+ arr[4]=1+2+3+4+5=15Input:1,2,3,4,5 Output:15ExplanationUsing For loop to get to the every index element and taking sum of themarr[0]+arr[1]+arr[2]+arr[3]+ arr[4]=1+2+3+4+5=15Example#include using namespace std; int main() {    int i,n,sum=0;    int arr[]={1,2,3,4,5};    n=5;    for(i=0;i

C/C++ Program for the n-th Fibonacci number?

sudhir sharma
Updated on 19-Aug-2019 11:37:38

807 Views

The Fibonacci sequence is a series where the next term is the sum of the previous two terms.The first two terms of the Fibonacci sequence is 0 followed by 1. In this problem, we will find the nth number in the Fibonacci series. For this we will calculate all the numbers and print the n terms.Input:8 Output:0 1 1 2 3 5 8 13Explanation0+1=1 1+1=2 1+2=3 2+3=5Using For loop to sum of previous two terms for next termExample#include using namespace std; int main() {    int t1=0,t2=1,n,i,nextTerm;    n = 8;    for ( i = 1; i

C++ Program for the Recursive Bubble Sort?

sudhir sharma
Updated on 19-Aug-2019 11:35:50

578 Views

In Bubble sort compares adjacent pairs and swaps them if they are in the wrong order. In this type of bubble sort we use the recursive function that calls itself.Input:53421 Output:12345ExplanationUse of recursive (self-calling) function compares adjacent pairs and swaps them if they are in the wrong order until the array is in orderExample#include using namespace std; void bubbleSort(int arr[], int n) {    for (int i = 0; i < n - 1; i++) {       if (arr[i] > arr[i + 1]) {          int temp = arr[i];          arr[i] ... Read More

Advertisements