Found 7197 Articles for C++

Cells with Odd Values in a Matrix in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:14:01

357 Views

Suppose there are n and m which are the dimensions of a matrix. These are initialized by zeros. And indices are given where indices[i] = [ri, ci]. For each pair of [ri, ci] we have to increment all cells in row ri and column ci by 1. The output will be the number of cells with odd values in the matrix after applying the increment to all indices.To solve this, we will follow these steps −Initialize odd := 0, and x := row count of the matrixcreate a matrix matfor i in range 0 to xr = input[i, 0], c ... Read More

Find Positive Integer Solution for a Given Equation in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:11:04

277 Views

Suppose we have a function f that takes two parameters (x, y). We have to return all pairs of x and y, for which f(x, y) = z. The z is given as input, and x, y are positive integers. The function is constantly increasing function. So f(x, y) < f(x + 1, y) and f(x, y) < f(x, y + 1).To solve this we will perform straight-forward approach. Take i in range 1 to 1000, and j in range 1 to 1000, for all combinations of i, j, if f(i, j) = 0, then return true, otherwise false.Consider the ... Read More

Check If It Is a Straight Line in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:07:21

582 Views

Suppose we have a list of data-points consisting of (x, y) coordinates, we have to check whether the data-points are forming straight line or not. So if the points are like [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)], then they are forming straight line.To solve this, we will take the differences between each consecutive datapoints, and find the slope. For the first one find the slope. For all other points check whether the slope is same or not. if they are same, then simply return true, otherwise falseExampleLet us see the following implementation to get ... Read More

Split a String in Balanced Strings in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:05:26

493 Views

As we know that the balanced strings are those which have equal quantity of left and right characters. Suppose we have a balanced string s split it in the maximum amount of balanced strings. We have to return the maximum amount of splitted balanced strings. So if the string is “RLRRLLRLRL”, then output will be 4. as there are four balanced strings. “RL”, “RRLL”, “RL” and “RL” each substring has equal amount of L and R.To solve this, we will follow these steps −initialize cnt := 0, and ans := 0for i := 0 to size of the stringcnt := ... Read More

Print all possible combinations of r elements in a given array of size n in C++

sudhir sharma
Updated on 17-Jan-2020 11:42:28

2K+ Views

In this problem, we are given an array of size n and a positive integer r. Our task is to print all possible combinations of the elements of the array of size r.Let’s take an example to understand the problem −Input: {5, 6, 7, 8} ; r = 3 Output : {5, 6, 7}, {5, 6, 8}, {5, 7, 8}, {6, 7, 8}To solve this problem an approach would be fixing elements and then recuring or looping over others to find all combinations. In this, we have to fix first n-r+1 elements only and loop or recur over the rest.Example#include ... Read More

Print all possible expressions that evaluate to a target in C++

sudhir sharma
Updated on 17-Jan-2020 11:38:28

308 Views

In this problem, we are given a string of integers from 0 to 9 and a target value. We have to print out ways in which we can generate expression using +, -, and * operation which is evaluated to the value equal to target.Let’s take an example to understand the topic better −Input: string = “123” , target= 6 Output: { “1+2+3”, “1*2*3” }To solve this problem, we will be creating expressions by placing all possible binary operators between digits and then checking the result of the expression with the target value.We will pass all values to a recursive ... Read More

Print all possible paths from top left to bottom right of a mXn matrix in C++

sudhir sharma
Updated on 17-Jan-2020 11:36:41

369 Views

In this problem, we are given an mXn 2D matrix and we have to print all possible paths from top left to the bottom right of the matrix. For traversal, we can move only right and down in the matrix.Let’s take an example to understand the topic better −Input: 1 3 5 2 8 9 Output: 1 -> 3 -> 5 -> 9 1 -> 3 -> 8 -> 9 1 -> 2 -> 8 -> 9To solve this problem, we will move from one cell to another and print the path on going down and right. We will do ... Read More

Print all possible strings of length k that can be formed from a set of n characters in C++

sudhir sharma
Updated on 17-Jan-2020 11:35:01

2K+ Views

In this problem, we are given a set of characters and a positive integer k and we have to print all possible strings of length k that can be generated using the characters of the set.Let’s take an example to understand the problem better −Input: set = {‘x’, ‘y’, ‘z’} , k = 2 Output: xy, xz, yzTo solve this problem, we have to find all possible sequences that can be generated. For the set of size n, the total number of a possible string of length k will be nk (n^k). We will use a recursive call to generate ... Read More

Print all possible strings that can be made by placing spaces in C++

sudhir sharma
Updated on 17-Jan-2020 11:31:08

389 Views

In this problem, we are given a string and we have to print all those string that can be made using this string by placing space in between the characters of the string.Let’s take an example to understand the topic better −Input: string = ‘XYZ’ Output: XYZ, XY Z, X YZ, X Y ZTo solve this problem, we will have to find all the possible ways in which we can put space in the string. For this we will use recursion. In this, we will place spaces on by one and generate a new string.Example Live Demo#include #include using ... Read More

Print all possible sums of consecutive numbers with sum N in C++

sudhir sharma
Updated on 17-Jan-2020 11:30:24

517 Views

In this problem, we are given a positive integer N and we have to print the sequence of all possible consecutive numbers with a sum equal to N.Let’s take an example to understand the problem,Input: N = 15 Output: 1 2 3 4 5 7 8A simple solution to this problem is by adding up consecutive sequence combinations till N/2. And then print the sequence that sums up to N.Example Live Demo#include using namespace std; void printConsequtiveSum(int N){    int start = 1, end = (N+1)/2;    while (start < end){       int sum = 0;       for (int i = start; i

Advertisements