Found 7197 Articles for C++

Program to find the initials of a name in C++

Ayush Gupta
Updated on 15-Sep-2020 15:19:16

1K+ Views

In the program, we are given a string name that denotes the name of a person. Our task is to create a Program to find the initials of a name in C++.Code Description − Here, we have to find the initials of the name of the person given by the string.Let’s take an example to understand the problem, Inputname = “ram kisan saraswat”OutputR K SExplanationWe will find all the first letters of words of the name.Solution ApproachA simple solution to the problem is by traversing the name string. And all the characters that appear after the newline character or space ... Read More

Program to find the Interior and Exterior Angle of a Regular Polygon in C++

Ayush Gupta
Updated on 15-Sep-2020 15:20:08

483 Views

In this problem, we are given a number n that denotes the sides of a regular polygon. Our task is to create a Program to find the Interior and Exterior Angle of a Regular Polygon in C++.Problem Description − Here, for the given number of sides, we will find the value of each interior and exterior angle of the regular polygon of side n.Interior Angle is the angle between two adjacent sides of a polygon that lies inside the polygon.Exterior Angle is the angle between two adjacent sides of a polygon that lies outside the polygon.Let’s take an example to ... Read More

Program to find the Hidden Number in C++

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

406 Views

In this problem, we are an array arr[] consisting of n integer values. Our task is to create a Program to find the Hidden Number in C++.Code description − For an array, the hidden number, is the number which when subtracted from each element of the array gives the sum 0.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 6, 7, 2}Output4Subtracting 4 from all elements of the array. And adding of values= (1 - 4) + (6 - 4) + (7 - 4) + (4 - 2) = -3 + 2 + 3 - 2 = ... Read More

Rectangle Area II in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:35:39

420 Views

Suppose we have a list of (axis-aligned) rectangles. Here each rectangle[i] = {x1, y1, x2, y2}, where (x1, y1) is the point of the bottom-left corner, and (x2, y2) are the point of the top-right corner of the ith rectangle.We have to find the total area covered by all rectangles in the plane. The answer may be very , so we can use modulo 10^9 + 7.So, if the input is likethen the output will be 6.To solve this, we will follow these steps −m = 10^9 + 7Define a function add(), this will take a, b, return ((a mod ... Read More

Program to find the head start in a race in C++

Ayush Gupta
Updated on 15-Sep-2020 15:22:43

155 Views

In this problem, we are given two integers that give the head start that is given by A to B and C respectively in a 100-meter race. Our task is to create a program to find the head start in a race in C++.Code Description − Here, there are head starts that are given by A to B and A to C respectively in a 100-meter race. We need to find the relative head start that is given by B to C in the 100-meter race.Let’s take an example to understand the problem, Input20, 28Output90ExplanationA gives B a head-start of ... Read More

Count Unique Characters of All Substrings of a Given String in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:32:14

627 Views

Suppose we want to define a function called countUniqueChars(s) that will return the number of unique characters on s, so if s = "HELLOWORLD" then "H", "E", "W", "R", "D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.Now on this problem given a string s we have to find the sum of countUniqueChars(t) where t is a substring of s. (Here some substrings can be repeated so on this case we have to count the repeated ones too.)As the answer can be very large, we can return answer modulo 10^9+7.So, if the input ... Read More

Making A Large Island in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:28:50

274 Views

Suppose we have a 2D grid of binary values (0s and 1s), we change at most one 0 to a 1. After that we have to find what is the size of the largest island? Here an island is a 4-directionally (top, bottom, left, right) connected group of 1s.So, if the input is like [[1, 0], [0, 1]], then the output will be 3, this is because if we change one 0 to 1 and connect two 1s, then we will get an island with area = 3.To solve this, we will follow these steps −Define an array dir of ... Read More

Bricks Falling When Hit in C++

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

206 Views

Suppose we have a grid of binary values (0s and 1s) the 1s in a cell represent the bricks. A brick will not drop when that satisfies these conditions −Either brick is directly connected to the top of the gridor at least one of its adjacent (top, bottom, left, right) bricks will not drop.We will do some erasures sequentially. In each case we want to do the erasure at the location (i, j), the brick (if that is present) on that location will disappear, and then some other bricks may drop because of that erasure. We have to find the ... Read More

Sliding Puzzle in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:23:31

2K+ Views

Suppose we have one 2x3 board, there are 5 tiles those are represented by the numbers 1 through 5, and one empty square is there, that is represented by 0.Here a move means 0 and one adjacent number (top, bottom, left or right) and swapping it. This will be solved when the elements are arranged in this manner: [[1, 2, 3], [4, 5, 0]].We have the puzzle board; we have to find the least number of moves required so that the state of the board is solved. If this is not possible to solve, then return -1.So, if the input ... Read More

Max Chunks To Make Sorted II in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:19:39

242 Views

Suppose we have an array arr of integers, we have to split the array into some number of partitions, and individually sort each partition. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5].To solve this, we will follow these steps −cnt := 1n := size of arrDefine an array maxOfLeft of size nDefine an array minOfRight of size nmaxOfLeft[0] ... Read More

Advertisements