Found 7347 Articles for C++

C++ program to construct graph with certain conditions

Arnab Chakraborty
Updated on 03-Mar-2022 06:55:06

451 Views

Suppose we have two numbers N and K. Consider there is an undirected graph with N elements. N vertices satisfies the following conditions −The graph is simple and connectedVertices are numbered from 1 to NLet M be the number of edges in the graph. The edges are numbered from 1 to M. The length of the edge is 1. And Edge i connects vertex U[i] to vertex V[i].There are exactly K pairs of vertices (i, j) where i < j, such that the shortest distance between them is 2.If such graph exists, we have to construct that graph. Otherwise return ... Read More

C++ program to find winner of cell coloring game

Arnab Chakraborty
Updated on 03-Mar-2022 06:51:20

435 Views

Suppose we have two arrays A and B both are with N elements each. Consider Amal and Bimal are playing a game on a board whose cell numbers are numbered from 1 to N. And N-1 roads. Roads are connecting two cells. So ith road is connecting A[i] to B[i]. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. Initially the cell 1 is marked as black color, and cell N is white. Other cells are not colored. Amal plays first, and they are playing alternatively. Amal selects an uncolored cells that is ... Read More

C++ program to find nearest integer for which the number and its digits sum gcd is greater than 1

Arnab Chakraborty
Updated on 03-Mar-2022 06:52:03

155 Views

Suppose we have a number N. Consider a function gcdSum(x) of a positive integer x which is the gcd of that integer with its sum of digits. We have to find the smallest integer x >= n, such that gcdSum(x) > 1.So, if the input is like N = 31, then the output will be 33, because gcd of 31 and (3+1) is 1. The gcd of 32 and (3+2) is 1, and gcd of 33 and (3+3) is 3.StepsTo solve this, we will follow these steps −for initialize i := n, when i 0, do:       ... Read More

C++ program to find in how many bases we can represent the number not greater than M

Arnab Chakraborty
Updated on 03-Mar-2022 06:48:09

116 Views

Suppose we have a numeric string S, and another number M. Let d be the greatest digit in S. We have to find in many different integers not greater than M can be found by choosing an integer n not less than d+1 and seeing S as a base-n number?So, if the input is like S = "999"; M = 1500, then the output will be 3, because S as base 10 number, we get 999, as base 11 number, we get 1197, as base 12 number, we get 1413. These three values are the only ones that we can ... Read More

C++ program to find longest possible time not greater than T to solve all problems

Arnab Chakraborty
Updated on 03-Mar-2022 06:44:35

173 Views

Suppose we have an array A with N elements. Have another number T. Consider Amal is trying to participate in a programming contest. It lasts for T minutes and present N problems. He has A[i] time to solve ith problem. He will choose zero or more problems to solve from the N problems, so that it takes no longer T minutes in total to solve them. We have to find the longest possible time it will take to solve his choice of problems.So, if the input is like T = 17; A = [2, 3, 5, 7, 11], then the ... Read More

C++ program to count final number of characters are there after typing n characters in a crazy writer

Arnab Chakraborty
Updated on 03-Mar-2022 06:47:47

81 Views

Suppose we have an array A with n elements, and another value c is there. There is a crazy word processor present in our system where we can type characters but if we do not type for consecutive c seconds, all of the written letters will be removed. A[i] represents the time when we have typed the ith character. We have to find the final number of characters that will remain on the screen after typing all n characters.So, if the input is like A = [1, 3, 8, 14, 19, 20]; c = 5, then the output will be ... Read More

C++ program to find minimum number of operations needed to make all cells at r row c columns black

Arnab Chakraborty
Updated on 03-Mar-2022 06:42:15

178 Views

Suppose we have two numbers r, c and a grid of size n x m. Some cells are in black and remaining are white. In one operation, we can select some black cells and can do exactly one of these two −Color all cells in its row black, orcolor all cells in its column black.We have to find the minimum number of operations needed to make the cells in row r and column c black. If impossible, return -1.So, if the input is likeWBWWWBBBWBWWBBBr = 0 and c = 3then the output will be 1, because we can change the ... Read More

C++ program to find maximum how many chocolates we can buy with at most k rupees

Arnab Chakraborty
Updated on 03-Mar-2022 06:28:13

369 Views

Suppose we have an array A with n elements, and other values l, r and k are there. Amal wants to buy chocolates and he will not buy too expensive chocolates, and not also too cheap chocolates. In the store, there are n different chocolate bars and the prices are represented in A. A chocolate bar is too expensive if its price is larger than r and too cheap if its price is less than l. He wants to spend at most k rupees. We have to find the maximum amount of chocolates he can buy.So, if the input is ... Read More

C++ program to count number of steps needed to make sum and the product different from zero

Arnab Chakraborty
Updated on 03-Mar-2022 06:24:13

218 Views

Suppose we have an array A with n elements. In one operation, we can add 1 with any one element preset in A. If either the sum or the product of all elements in the array is equal to zero, We can do this operation one more time. We have to count the minimum number of steps needed to make both the sum and the product of all elements in the array different from zero?So, if the input is like A = [-1, 0, 0, 1], then the output will be 2, because both product and sum are 0. If ... Read More

C++ program to find maximum profit we can make by making hamburger and chicken burgers

Arnab Chakraborty
Updated on 03-Mar-2022 06:20:46

277 Views

Suppose we have five numbers b, p, f, h and c. There are two types of burgers in a restaurant. These are hamburger and chicken burger. Hamburger needs two buns and a beef patty and for chicken burger we need two buns and a chicken cutlet. We have b buns, p beef patties, f chicken cutlets. We are trying to sell hamburger for h rupees and chicken burger for c rupees. We have to find the maximum profit that we can gain.So, if the input is like b = 7; p = 5; f = 2; h = 10; c ... Read More

Advertisements