C++ Articles

Page 243 of 597

Subtract the Product and Sum of Digits of an Integer in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 561 Views

Suppose we have one number. We have to find the sum of digits and product of digits. After that find the difference between sum and product. So if the number is 5362, then sum is 5 + 3 + 6 + 2 = 16, and 5 * 3 * 6 * 2 = 180. So 180 – 16 = 164To solve this take each digit and add and multiply one by one, then return the difference.ExampleLet us see the following implementation to get better understanding −#include using namespace std; class Solution {    public:       int subtractProductAndSum(int n) {          int prod = 1;          int sum = 0;          for(int t = n;t;t/=10){             sum += t % 10;             prod *= t % 10;          }          return prod - sum;       } }; main(){    Solution ob;    cout

Read More

Element Appearing More Than 25% In Sorted Array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 344 Views

Suppose we have an array A. There are few elements. Some elements are common. We have to return an element that is appearing more than 25% spaces in the array. So if A = [1, 2, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7], Here 4 has occurred four times. This is more than 25% of 12 (size of the array)To solve this, we will follow these steps −Read elements and store their respective frequenciesIf the frequency is greater than 25% of the array size, then return the result.ExampleLet us see the following implementation to get better understanding ...

Read More

Replace Elements with Greatest Element on Right Side in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 480 Views

Suppose we have an array A. We have to replace every element by the greatest element on the right side of this element. And replace the last one by -1. So if A = [5, 17, 40, 6, 3, 8, 2], then it will be [40,40,8,8,8,2,-1]To solve this, we will follow these steps −We will read the array element from right to left.take e := -1for i := n – 1 to 0temp := ee := max between e and array[i]array[i] := tempreturn arrayExampleLet us see the following implementation to get better understanding −#include using namespace std; void print_vector(vector v){    cout

Read More

Find N Unique Integers Sum up to Zero in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 407 Views

Suppose we have an integer n. We have to return any array that contains n unique integers, such that they add up to 0. So if input is n = 5, then one possible output will be [-7, -1, 1, 3, 4]To solve this, we will follow these steps −take an array A as final answer, and take x := 0for i in range 0 to n – 2A[i] = (i + 1)x := x + i + 1A[n – 1] = xreturn AExampleLet us see the following implementation to get better understanding −#include using namespace std; void print_vector(vector v){    cout

Read More

Copysign() function in C++

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

Given the task is to show the working of copysign() in C++.The copysign() function is a part of the C++ standard template library. It takes two arguments and produces the result by combining the magnitude of the first value and the sign of the second value. or header file should be included to call this function.SyntaxThe syntax is as follows −copysign(x, y)ExampleInput: copysign(4, -5) Output: -4Explanation − The following example demonstrates how we can copy the sign of one value to the magnitude of another value. The sign of the second argument, that is “-“and the magnitude of the ...

Read More

const_cast in C++ - Type casting operators

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 3K+ Views

Given the task is to show the working of const_cast in c++. const_cast is one of the type casting operators. It is used to change the constant value of any object or we can say it is used to remove the constant nature of any object. const_cast can be used in programs that have any object with some constant value which need to be changed occasionally at some point. Syntax The syntax is as follows − const_cast(expression) Example Input: const int x = 50; const int* y = &x; cout

Read More

isprint() Working with C++

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

Isprint() in C++ is inbuilt function in “cctype.h” header file which checks whether the character is printable or not.Isprint returns true for constant cases as Isprint aside from the house character (' '), that returns true.A locale-specific model version of this function (Isprint) exists in cctype header file.-Isprint() function can be used to check any Non-Printing character in a series of sentences.-Isprint() is an Inbuilt function that provides efficient way to handle non printing characters-Isprint() helps to minimize the lines of code for programmer.-Isprint() is in true sense decreases the compilation time of program.Including cctype.h in your program not only ...

Read More

Find elements of an Array which are Odd and Even using STL in C++

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

Given with an array and the task is to find the number of odd and even elements in an array 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 ...

Read More

Maximum path sum in matrix in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 878 Views

In this problem, we are given a 2D matrix of size M*N. Our task is to create a program that will find the maximum path sum in the matrix.Here, the maximum path sum in the matrix is defined as the sum of all elements for one row to the last row. The allowed moves for traversing the path are downward move and diagonal move. The start and endpoints can be any element of the first and last row of the matrix respectively.Let's take an example to understand the problemInput −matrix [][] =    3 5 9    1 7 2 ...

Read More

Maximum Perimeter Triangle from array in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 360 Views

Problem statementGiven an Array of non-negative integers. Find out three elements from the array which form a triangle of maximum perimeterExampleIf input array is {5, 1, 3, 5, 7, 4} then maximum perimeter is (7 + 5 + 5) = 17AlgorithmSort the array in non-increasing order. So, the first element will be maximum and the last will be minimumIf the first 3 elements of this sorted array forms a triangle, then it will be the maximum perimeter triangleExample#include using namespace std; int getMaxPerimeter(int *arr, int n) {    sort(arr, arr + n, greater());    int maxPerimeter = 0;   ...

Read More
Showing 2421–2430 of 5,962 articles
« Prev 1 241 242 243 244 245 597 Next »
Advertisements