Found 7197 Articles for C++

Program to find the mid-point of a line in C++

Ayush Gupta
Updated on 15-Sep-2020 15:24:12

1K+ Views

In this problem, we are given two points A and B, starting and ending point of a line. Our task is to create a program to find the mid-point of a line in C++.Problem Description − Here, we have a line with starting and ending points A(x1, y1) and B(x2, y2). And we need to find the mid-point of the line.Let’s take an example to understand the problem, Inputa(x1, y1) = (4, -5) b(x2, y2) = (-2, 6)Output(1, 0.5)Explanation(x1 + x2)/2 = 4 - 2 / 2 = 1 (y1 + y2)/2 = -5 + 6 / 2 = 0.5Solution ... Read More

Binary Tree Cameras in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:46:26

255 Views

Suppose we have a binary tree; we place cameras on the nodes of the tree. Now each camera at a node can monitor its parent, itself, and its children. We have to find the minimum number of cameras needed to monitor all nodes of the tree.So, if the input is like −then the output will be 1, because only one camera is enough to track all.To solve this, we will follow these steps −Define one set called covered, of type TreeNode (Tree node has left, right and data field)Define a function solve(), this will take node, parent, if node is ... Read More

Program to find the maximum difference between the index of any two different numbers in C++

Ayush Gupta
Updated on 15-Sep-2020 15:12:47

379 Views

In this problem, we are given an array arr[] consisting of n integers. Our task is to create a program to find the maximum difference between the index of any two different numbers in C++.Code Description − Here, we need to find the maximum difference between the index of integer values of the array, given that the two integers are different.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 3, 2, 1, 2, 4}Output5ExplanationThe difference between index 0, element 4, and index 5, element 2.Solution ApproachWe will try to find the maximum possible difference between the index ... Read More

Program to find the maximum element in a Matrix in C++

Ayush Gupta
Updated on 15-Sep-2020 15:14:05

1K+ Views

In this problem, we are given a matrix of size nXm. Our task is to create a program to find the maximum element in a Matrix in C++.Problem Description − Here, we need to simply find the largest element of matrix.Let’s take an example to understand the problem, Inputmat[3][3] = {{4, 1, 6}, {5, 2, 9}, {7, 3, 0}}Output9Solution ApproachThe solution to the problem is by simply traversing the matrix. This is done by using two nested loops, and checking whether each element of the matrix is greater than maxVal. And return the maxVal at the end.Program to illustrate the ... Read More

Maximum of four numbers without using conditional or bitwise operator in C++

Ayush Gupta
Updated on 15-Sep-2020 15:15:42

643 Views

In this problem, we are given four integer numbers. Our task is to create a program to find the maximum of four numbers without using conditional or bitwise operator in C++.Code Description − Here, we have four integer values. And we need to find the maximum value out of these numbers without using any conditional or bitwise operator.Let’s take an example to understand the problem, Inputa = 4, b = 7, c = 1, d = 9Output9Solution ApproachTo solve the problem, we will take two elements first, then take the greater element with pair. For each pair, we will create ... Read More

Stamping The Sequence in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:43:06

288 Views

Suppose we want to make a target string of lowercase letters.At first, we have the sequence as n '?' marks (n is the length of target string). We also have a stamp of lowercase letters.On each turn, we can place the stamp over the sequence, and replace every letter in the with the corresponding letter from that stamp. You can make up to 10 * n turns. As an example consider the initial sequence is "?????", and the stamp is "abc", then we may make strings like "abc??", "?abc?", "??abc" in the first turn.If the sequence is possible to stamp, ... Read More

Program to find the Largest Number using Ternary Operator in C++

Ayush Gupta
Updated on 15-Sep-2020 15:17:13

2K+ Views

In this problem, we are given some numbers. Our task is to create a Program to Find the Largest Number using Ternary Operator in C++.The elements can be −Two NumbersThree NumbersFour NumbersCode Description − Here, we are given some numbers (two or three or four). We need to find the maximum element out of these numbers using a ternary operator.Let’s take a few examples to understand the problem, Two NumbersInput − 4, 54Output − 54Three NumbersInput − 14, 40, 26Output − 40Four NumbersInput − 10, 54, 26, 62Output − 62Solution ApproachWe will use ternary Operator, for two, three and four ... Read More

Program to find the largest and smallest ASCII valued characters in a string in C++

Ayush Gupta
Updated on 15-Sep-2020 15:18:18

2K+ Views

In this problem, we are given a string. Our task is to create a program to find the largest and smallest ASCII valued characters in a string in C++.Code Description − Here, we have a string that consists of both upperCase and lowerCase characters. And we need to find the characters that have the largest and the smallest ASCII value character.Let’s take an example to understand the problem, Inputstr = “TutroialsPoint”OutputLargest = u smallest = P.ExplanationAccording to ASCII values, upperCase characters are smaller than lowerCase characters.So, the smallest character of upperCase characters (A) has overall the smallest ASCII value. The ... Read More

Program to find the kth character after decrypting a string in C++

Ayush Gupta
Updated on 08-Jun-2020 10:37:35

226 Views

In this tutorial, we will be discussing a program to find the kth character after decrypting a string.For this, we will be provided with a string that will consist of characters and numbers and integer K. Our task is to decrypt the given string and find the character at Kth position.Example Live Demo#include #include using namespace std; //finding decrypted Kth character char findKthChar(string s, int k) {    int len = s.length();    int i = 0;    int total_len = 0;    while (i < len) {       if (isalpha(s[i])) {          total_len++; ... Read More

Shortest Path to Get All Keys in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:40:00

577 Views

Suppose we have a grid. There are few symbols. "." is indicating empty cell, "#" is for wall, "@" is for starting point, ("a", "b", ...) all are keys, and ("A", "B", ...) all are locks. We will start from the starting point, and one move consists of walking one space in one of the 4 directions (left, right, top, bottom). We will not go outside the grid, and there are walls to block our way. If we walk over a key, we pick it up. We can't walk over a lock unless we have the corresponding key.For each lock ... Read More

Advertisements