Found 7347 Articles for C++

C++ program to find out the number of coordinate pairs that can be made

Arnab Chakraborty
Updated on 25-Feb-2022 11:29:45

852 Views

Suppose, we are given 2n number of coordinates on a two-dimensional plane. The 2n coordinates are divided into two arrays coordA, and coordB. The coordinates are represented as integer pairs. Now we have to form coordinate pairs that will contain one point from coordA and one point from coordB. We can make pairs if and only if the x coordinate of the point from coordA is smaller than that of the point from coordB, and the y coordinate of the point from coordA is smaller than that of the point from coordB. We have to find out the number of ... Read More

C++ program to find out the maximum sum of a minimally connected graph

Arnab Chakraborty
Updated on 25-Feb-2022 11:18:57

194 Views

Suppose, we are given a minimally connected graph. That means removing any edge will make the graph disconnected. The graph has n vertices and the edges are given in an array 'edges'. There is also an array 'vertexValues' given to us that contain n integer values.Now, we do the following −We write a positive integer on each of the vertices and then try to calculate a score.There is an edge connecting two vertices, we put the smaller value of the two vertices on the edges.We calculate the score by adding all the edge values.We have to find the maximum value ... Read More

C++ Program to find out the cost to travel all the given coordinates

Arnab Chakraborty
Updated on 25-Feb-2022 11:07:24

196 Views

Suppose, we are given n three-dimensional coordinates. The cost to travel from coordinate (a, b, c) to (x, y, z) is ∣ x − a∣ + ∣ y − b∣ + max(0, z − c). We start from the first coordinate, then visit all the coordinates at least once, and then return to the first coordinate. We have to find out the total cost of this whole trip. The coordinates are given to us in the array 'coords'.So, if the input is like n = 3, coords = {{1, 1, 0}, {1, 3, 4}, {3, 2, 2}}, then the output ... Read More

C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid

Arnab Chakraborty
Updated on 25-Feb-2022 10:46:30

173 Views

Suppose, we have a grid of dimensions h x w. The grid is represented in a 2D array called ‘initGrid’, where each cell in the grid is either represented by a '#' or a '.'. '#' means that the grid contains an obstacle and '.' means that there is a path through that cell. Now, a robot is placed on a cell 'c' on the grid having row number x and column number y. The robot has to travel to another cell 'd' having row number p and column number q. Both the cell coordinates c and d are presented ... Read More

C++ Program to find out the number of unique matrices that can be generated by swapping rows and columns

Arnab Chakraborty
Updated on 25-Feb-2022 10:31:41

120 Views

Suppose, we have a n x n matrix. Each element in the matrix is unique and is an integer number between 1 and n2. Now we can perform the operations below in any amount and any order.We pick any two integers x and y that are in the matrix, where (1 ≤ x < y ≤ n) and swap the columns containing x and y.We pick any two integers x and y that are in the matrix, where (1 ≤ x < y ≤ n) and swap the rows containing x and y.We have to note that x + y ... Read More

C++ Program to find out the total cost required for a robot to make a trip in a grid

Arnab Chakraborty
Updated on 25-Feb-2022 10:05:21

121 Views

Suppose, we are given a grid of dimensions h x w. Each cell in the grid contains some positive integer number. Now there is a path-finding robot placed on a particular cell (p, q) (where p is the row number and q is the column number of a cell) and it can be moved to cell (i, j). A move operation has a particular cost, which is equal to |p - i| + |q - j|. Now there are q number of trips, which has the following properties.Each trip has two values (x, y) and there is a common value ... Read More

C++ program to find out the maximum number of cells a cleaning robot can clean in a grid

Arnab Chakraborty
Updated on 23-Feb-2022 07:31:48

481 Views

Suppose, we are making a cleaning robot that works on a grid. The grid is of dimensions h x w. There are m dirty cells that need to be cleaned that are given in an array of integer pairs 'dirt'. The cleaning robot, if placed in a particular cell; can clean every cell in that particular row and column. So, our task is to clean the maximum number of dirty cells. We have to find out the count and display it as output.So, if the input is like h = 3, w = 3, m = 3, dirt = {{0, ... Read More

Find the smallest after deleting given elements using C++

sudhir sharma
Updated on 14-Feb-2022 09:21:12

93 Views

In this problem, we are given two arrays arr[] and del[]. Our task is to find the smallest after deleting given elements.We will be deleting values from the array arr[] that are present in del[]. And then print the smallest value after deletion.Let’s take an example to understand the problem, Input arr[] = {2, 5, 6, 9, 1} del[] = {1, 5, 9}Output 2Solution ApproachA simple solution to the problem is using hashing. We will insert all the values of del[] array in the hash table. Then we will traverse the array arr[] and check if the values in the ... Read More

Find the slope of the given number using C++

sudhir sharma
Updated on 14-Feb-2022 09:16:48

314 Views

In this problem, we are given a number N. Our task is to find the slope of the given number.Slope of a number is the total number of maxima and minima digits in the number.Maxima digit is the digit whose both neighbours (previous and next) are smaller.Maxima digit is the digit whose both neighbours (previous and next) are greater.Let’s take an example to understand the problem, Input N = 9594459Output 2Solution ApproachA simple solution to the problem is by traversing the number digit by digit from excluding the first and last one (the don’t count form maxima or minima). Now, ... Read More

Minimum Fibonacci terms with sum equal to K in C++

sudhir sharma
Updated on 14-Feb-2022 09:13:05

127 Views

In this problem, we are given a number K. Our task is to find the Minimum Fibonacci terms with sum equal to K.Fibonacci Series generates subsequent numbers by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.Fibonacci Series is 0 1 1 2 3 5 8 13Let’s take an example to understand the problem, InputK = 5Output2ExplanationThe sum 5 can be made using 3 and 2.Solution ApproachBy using Fibonacci numbers we can get the sum as any number ... Read More

Advertisements