Found 7197 Articles for C++

Fill missing entries of a magic square in C++

Arnab Chakraborty
Updated on 24-Oct-2019 11:53:22

228 Views

Suppose we have one 3x3 matrix, whose diagonal elements are empty at first. We have to fill the diagonal such that the sum of row, column and the diagonal will be same. Suppose a matrix is like −036505470After filling, it will be −636555474Suppose the diagonal elements are x, y, z. The values will be −x = (M[2, 3] + M[3, 2])/ 2z = (M[1, 2] + M[2, 1])/ 2y = (x + z)/2Example Live Demo#include using namespace std; void displayMatrix(int matrix[3][3]) {    for (int i = 0; i < 3; i++) {       for (int j = 0; j < 3; j++)          cout

Sum of sum of first n natural numbers in C++

sudhir sharma
Updated on 24-Oct-2019 11:18:27

268 Views

In this problem to find the sum of sum of first n natural numbers, we will find the sum of all numbers from 1 to n and add them together to find the sum.Let’s take an example to learn about the concept,Input : 4 Output : 10 Explanation : Sum of first 1 natural number = 1 Sum of first 2 natural number = 1 + 2 = 3 Sum of first 3 natural number = 1 + 2 +3 = 6 Sum of first 4 natural number = 1 + 2 + 3 + 4 = 10 Sum of sum of 4 natural number = 1 + 3 + 6 + 10 = 20Example Live Demo#include using namespace std; int sumofSum(int n){    int sum = 0;    for (int i=1; i

Add all greater values to every node in a given BST in C++ ?

sudhir sharma
Updated on 24-Oct-2019 11:17:01

134 Views

A BST or binary search tree is a form of binary tree that has all left nodes smaller and all right nodes greater than the root value. For this problem, we will take a binary tree and add all the values greater than the current node to it. the problem “ add all greater values to every node in BST” is simplified as for a BST add all the node values that are greater than the current node value to that node value.Add all greater values to each node in BST Problem Statement −Given a Binary Search Tree (BST), we ... Read More

Binary Search in C++ program?

sudhir sharma
Updated on 24-Oct-2019 11:12:46

907 Views

binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the ... Read More

Sum of squares of Fibonacci numbers in C++

sudhir sharma
Updated on 24-Oct-2019 11:08:17

232 Views

Fibonacci series is a mathematical sequence of number which starts from 0 and the sum of two numbers is equal to the next upcoming number, for example, the first number is 0 and the second number is 1 sum of 0 and 1 will be 1F0=0, F1=1AndFn=Fn-1+Fn-2, F2=F0+F1 F2=0+1 F2=1then when we add number 1 and 1 then the next number will be 2F1=1, F2=1AndFn=Fn-1+Fn-2, F3=F1+F2 F3=1+1 F3=2Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …We have to find the square of the fuel energy series and then we have to sum it and find ... Read More

Sum of squares of binomial coefficients in C++

sudhir sharma
Updated on 24-Oct-2019 11:05:24

382 Views

The binomial coefficient is a quotation found in a binary theorem which can be arranged in a form of pascal triangle it is a combination of numbers which is equal to nCr where r is selected from a set of n items which shows the following formulanCr=n! / r!(n-r)! or nCr=n(n-1)(n-2).....(n-r+1) / r!The sum of the square of Binomial Coefficient i.e (nC0)2 + (nC1)2 + (nC2)2 + (nC3)2 + ……… + (nCn-2)2 + (nCn-1)2 + (nCn)2Input :n=5 Output:252ExplanationIn this program first we have to find the binomial coefficient of r which is selected from n set then we have to ... Read More

Binary representation of a given number in C++

sudhir sharma
Updated on 24-Oct-2019 11:02:52

3K+ Views

A binary number is a number that consists of only two digits 0 and 1. For example, 01010111.There are various ways to represent a given number in binary form.Recursive methodThis method is used to represent a number in its binary form using recursion.AlgorithmStep 1 : if number > 1. Follow step 2 and 3. Step 2 : push the number to a stand. Step 3 : call function recursively with number/2 Step 4 : pop number from stack and print remainder by dividing it by 2.Example Live Demo#include using namespace std; void tobinary(unsigned number){    if (number > 1)       tobinary(number/2);    cout

Balance a string after removing extra brackets in C++

sudhir sharma
Updated on 24-Oct-2019 10:56:41

187 Views

A string is an array of characters. In this problem, we are given a string which has opening and closing brackets. And we will balance this string by removing extra brackets from the string.Let’s take an example, Input : “)Tutor)ials(p(oin)t(...)” Output : “Tutorials(p(oin)t(...))”To solve this problem, we will traverse through the string and check for matching brackets. For unmatched brackets eliminate the closing brackets.AlgorithmStep 1 : Traverse the string from left to right. Step 2 : For opening bracket ‘(’ , print it and increase the count. Step 3 : For occurence of closing bracket ‘)’ , print it only ... Read More

Average of max K numbers in a stream in C++

sudhir sharma
Updated on 24-Oct-2019 10:52:36

221 Views

The average of number in a stream means calculating the average after every insertion. But in this problem, we need to find the average of max K numbers in a stream i.e. only k numbers of the array are considered for calculating the average. When we add a number if it is greater than any of the numbers that contribute to the average then only it is considered otherwise the average remains the same.Let’s take an example to understand the concept better −Input : n = 4 , k = 3 , array = { 4, 9, 1 , 5} ... Read More

Average of a stream of numbers in C++

sudhir sharma
Updated on 24-Oct-2019 10:48:19

850 Views

Average of numbers is the sum of numbers divided by the total number of numbers.In this problem, we are given a stream of numbers. And we will print average of the number at every point.Let’s take an example of how it works −We have a stream of 5 number 24 , 76 , 29, 63 , 88The average at each point of the stream would be −24 , 50 , 43 , 48 , 56.For this we will find the average of the stream every time a number is added to the stream. So, we need to find the average ... Read More

Advertisements