C++ Articles

Found 5,961 articles

C++ Program to Find GCD

Samual Sam
Samual Sam
Updated on 12-Mar-2026 15K+ Views

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them without leaving a remainder. For example, let's say we have two numbers 45 and 27 − 45 = 5 * 3 * 3 27 = 3 * 3 * 3 The common factors are 3 and 3, so the GCD of 45 and 27 is 9. Using Euclidean Algorithm (Modulo Method) The Euclidean algorithm finds the GCD by repeatedly replacing the larger number with the remainder of dividing the two numbers, until the remainder becomes 0. At that point, the other ...

Read More

C++ Program to Find All Roots of a Quadratic Equation

karthikeya Boyini
karthikeya Boyini
Updated on 12-Mar-2026 9K+ Views

A quadratic equation is in the form ax2 + bx + c = 0. The roots of the quadratic equation are given by the following formula − x = −b ± √ b² − 4ac 2a The value b2 − 4ac is called the discriminant. It determines the nature of the roots. There are three cases − b2 > 4ac − The roots are real and different. b2 = 4ac − The roots are real and both roots are the same. b2 ...

Read More

How to wrap python object in C/C++?

Gireesha Devara
Gireesha Devara
Updated on 12-Mar-2026 651 Views

To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) { ...

Read More

Maximum Sum Increasing Subsequence using DP in C++ program

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 281 Views

In this problem, we are given an array arr[] of size n. Our task is to create a program to find the maximum Sum Increasing Subsequence using DP in C++.Problem Description − To find the maximum sum increasing subsequence, we will be creating a subsequence in which the next element is greater than the current element.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 6, 5, 9}Output20ExplanationIncreasing subsequence with maximum sum: {2, 3, 6, 9} = 2 + 3 + 6 + 9 = 20Solution ApproachTo solve the problem using a dynamic Program Approach. We will ...

Read More

Find Maximum XOR value of a sub-array of size k in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 624 Views

In this problem, we are given an array arr[] consisting of n elements and an integer k. Our task is to find the Maximum XOR value of a sub-array of size k.Let’s take an example to understand the problem, Inputarr[] = {3, 1, 6, 2 ,7, 9} k = 3Output12ExplanationAll subarray and the xor of all element of size k, {3, 1, 6} = 4 {1, 6, 2} = 5 {6, 2, 7} = 3 {2, 7, 9} = 12Solution ApproachA simple solution to the problem is by using two loops. One to iterate over the array and other to ...

Read More

Find maximum possible stolen value from houses in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 779 Views

In this problem, we are given n houses with some values in them. Our task is to Find the maximum possible stolen value from houses.Problem Description − We have an array houses[] that consist of the values that are in each house. A thief robs the houses but he cannot steal from two adjacent houses as neighbours know about the theft. We need to find the maximum possible value that can be stolen by the thief from the house without getting caught.Let’s take an example to understand the problem, Inputhouses[] = {5, 2, 1, 6, 7, 9, 4, 3}Output23ExplanationThe max ...

Read More

Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 223 Views

In this problem, we are given an array arr[] consisting of n elements. We need to Find maximum value of Sum( i*arr[i]) with only rotations on a given array allowed. For find the maximum sum of (i*arr[i]), we can perform any number of rotations.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 3, 7, 2}Output43ExplanationWe will rotate the array once to get the maximum value, After rotation array will be {2, 4, 1, 3, 7}Sum = 0*2 + 1*4 + 2*1 + 3*3 + 4*7 = 0 + 4 + 2 + 9 + 28 = 43Solution ...

Read More

Find maximum XOR of given integer in a stream of integers in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 230 Views

In this problem, we are given Q queries each of which is one of the following type, Type 1 − insertion (1, i) to add the element with value i, in your data structure.Type 2 − findXOR (2, i), to find the XOR of all elements of the data structure with the element i.The data structure should contain only 1 element initially which will be 0.Let’s take an example to understand the problem, InputQueries: (1, 9), (1, 3), (1, 7), (2, 8), (1, 5), (2, 12)Output15 15ExplanationSolving each query, (1, 9) => data structure => {9} (1, 3) => data ...

Read More

Find number of subarrays with even sum in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 425 Views

In this problem, we are given an array arr[] consisting of N elements. Our task is to find the subarray with even sum.Let’s take an example to understand the problem,Inputarr[] = {2, 1, 3, 4, 2, 5}Output28ExplanationThe subarrays are −{2}, {4}, {2}, {2, 4}, {2, 2}, {1, 3}, {1, 5}, {3, 5}, {4, 2}, {2, 1, 3}, {2, 1, 5}, {2, 3, 5}, {2, 4, 2}, {1, 3, 4}, {1, 3, 2}, {1, 4, 5}, {1, 2, 5}, {3, 4, 5}, {3, 2, 5}, {2, 1, 3, 4}, {2, 1, 3, 2}, {2, 3, 4, 5}, {2, 3, 2, 5}, {2, 4, 2, 5}, {1, 3, 4, 2}, {1, 4, 2, 5}, {3, 4, 2, 5}, {2, 1, 3, 4, 2}, {2, 1, 3, 4, 2, 5}Solution ApproachA simple solution to the problem is using the direct method which is by calculating all subarray and their sums. And the increase count for the subarray with even sum. And at last return count.Program to illustrate the working of our solution,Example#include using namespace std; int countEvenSumSubArray(int arr[], int n){    int evenSumCount = 0, sum = 0;    for (int i=0; i

Read More

Find elements of an array which are divisible by N using STL in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 307 Views

Given with an array and the task is to find the number which are divisible by N using standard template library in C++.To solve this problem we are using the function count_if() present in C++ standard template library.What is a count_if() function?Syntaxcount_if(LowerBound, UpperBound, function)Description − This function returns the number of elements in an array that satisfies the given condition. It takes three parameters.Lower Bound − It points to the first element of an array or any other sequence.Upper Bound − It points to the last element of an array or any other sequence.Function − It returns the Boolean value ...

Read More
Showing 1–10 of 5,961 articles
« Prev 1 2 3 4 5 597 Next »
Advertisements