Found 7347 Articles for C++

Print 2-D co-ordinate points in ascending order followed by their frequencies in C++

sudhir sharma
Updated on 03-Feb-2020 10:09:17

130 Views

In this problem, we are given 2 arrays x[] , y[] such that (x, y) gives a cordinate of a point in 2D plane. Our task is to print all points along with their frequencies of occurence.Let’s take an example to understand the problemInput: x[]={0, 1, 1, 0, 0} ; y[]={1, 2, 2, 2, 1} Output (0, 1) = 2 (1, 2) = 2 (0, 2) = 1To solve this problem, we need to store frequency of occurence of each point. So this we need to use map data-structure. The key of the map is (x[i], y[i]), mapped value is ... Read More

Print 2D matrix in different lines and without curly braces in C/C++

sudhir sharma
Updated on 03-Feb-2020 09:58:11

199 Views

Here, we will see the code that will print a 2D matrix in c/c++ programming language without using curly braces.Curly braces are separators in a programming language that are used to define separate code blocks in the program. Without curly braces defining scopes is difficult in c/c++.Let’s see the basic code and sample output to print 2D matrix.Example Live Demo#include using namespace std; int main() {    int arr[2][2] = {{12, 67},    {99, 5}};    int n = 2, m = 2;    for (int i = 0; i < m; i++){       for (int j = 0; j < n; j++){          cout

Minimum Path Sum in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:19:34

150 Views

Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b - 1while j >= 0matrix[a, j] ... Read More

Unique Paths II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:06:20

161 Views

Suppose there is a robot is located at the top-left corner of a n x m grid (n rows and m columns). The robot can only move either down side or right side at any point in time. The robot wants to reach the bottom-right corner of the grid (marked 'END in the diagram below).Some cell in the grid is marked, that will be considered as obstacles. So we have to find how many possible unique paths are there? For example if the grid is like [[0, 0, 0], [0, 1, 0], [0, 0, 0]], then the grid will be ... Read More

Unique Paths in Python

Arnab Chakraborty
Updated on 04-May-2020 06:02:28

571 Views

Suppose there is a robot is located at the top-left corner of a n x m grid (n rows and m columns). The robot can only move either down side or right side at any point in time. The robot wants to reach the bottom-right corner of the grid (marked 'END in the diagram below). So we have to find how many possible unique paths are there? For example if m = 3 and n = 2, then the grid will be like below −RoboENDThe output will be 3, So there are total 3 different ways to reach from start ... Read More

Rotate List in C++

Arnab Chakraborty
Updated on 04-May-2020 05:59:49

491 Views

Suppose we have a linked list. We have to rotate the list to the right k places. The value of k is not negative. So if the list is like [1, 23, 4, 5, NULL], and k = 2, then the output will be [4, 5, 1, 2, 3, NULL]Let us see the steps −If the list is empty, then return nulllen := 1create one node called tail := headwhile next of tail is not nullincrease len by 1tail := next of tailnext of tail := headk := k mod lennewHead := nullfor i := 0 to len – ktail ... Read More

Permutation Sequence in C++

Arnab Chakraborty
Updated on 04-May-2020 05:58:12

453 Views

Suppose the set is like [1, 2, 3, ..., n], contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get these sequence for n = 3: ["123", "132", "213", "231", "312", "321"] So if n and k are given, then return the kth permutation sequence. The n will be between 1 to 9 (inclusive) and k will be between 1 to n! (inclusive). For example if n = 3.Let us see the steps −ans := empty string, define array called candidates of size nfor i in range 0 to n – ... Read More

Combination Sum II in C++

Arnab Chakraborty
Updated on 04-May-2020 05:54:02

421 Views

Suppose we have a set of candidate numbers (all elements are unique) and a target number. We have to find all unique combinations in candidates where the candidate numbers sum to the given target. The same number will not be chosen from candidates more than once. So if the elements are [2, 3, 6, 7, 8] and the target value is 8, then the possible output will be [[2, 6], [8]]Let us see the steps −We will solve this in recursive manner. The recursive function is named as solve(). This takes index, an array a, the integer b and another ... Read More

Valid Parenthesis String in C++

Arnab Chakraborty
Updated on 28-Apr-2020 07:02:40

1K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.To solve this, we will follow these steps −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from stack, andcheck whether the popped bracket is corresponding starting bracket of thecurrent character, ... Read More

Evaluate Reverse Polish Notation in C++ Program

Arnab Chakraborty
Updated on 28-Apr-2020 06:44:43

4K+ Views

Suppose we have Reverse polish notation and we have to evaluate the value. The reverse polish notation is also known as postfix expression. Here we have to use the stack data structure to solve the postfix expressions.From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from stack and then the operation is performed in the correct sequence. After that, the result is also pushed in the stack for future use. After completing the whole expression, the final result is also stored in the stack top. So ... Read More

Advertisements