Programming Articles - Page 1998 of 3366

Position of robot after given movements in C++

sudhir sharma
Updated on 17-Apr-2020 08:01:15

2K+ Views

In this problem, we are given a robot that moves in all four directions but only one move. The directions are up(‘U’), down(‘D’), left(‘L’), right(‘R’). And we are given a string that contains initials of directions of the number. Our task is to print the final position of the robot, given the initial position of the robot is (0, 0).Let’s take an example to understand the problemInput − input: ‘LDRRUL’Output − (0, 0)Explanation −L (left) : (0, 0) -> (-1, 0) D (down) : (-1, 0) -> (-1, -1) R (right) : (-1, -1) -> (0, -1) R (right) : ... Read More

Position of the K-th set bit in a number in C++

sudhir sharma
Updated on 17-Apr-2020 07:58:52

575 Views

In this problem, we are given two integers N and K. Our task is to find the index of Kth set a bit of the number N, counted from right.Set bits are checked from the binary representation of the number. The indexing in binary representation starts from index 0 from the right direction and propagates towards left.Example − in the binary number ‘011101’, at index 0 from right we have 1, at index 1 from right we have 0, and so on.Now, let’s take an example to understand the problemInput − N = 6, K = 2Output − 2Explanation − ... Read More

Positive elements at even and negative at odd positions (Relative order not maintained) in C++

sudhir sharma
Updated on 17-Apr-2020 07:55:04

366 Views

In this problem, we are given an array and our task is to convert the array in such a way that all positive numbers are at even index places and all negative numbers are at odd index places.There might be an unequal number of positive and negative values, in this case, we will not move extra values.Let’s take an example to understand the problem, Input − {3, 5, -1, 19, -7, -2}Output − {3, -1, 5, -7, 19, -2}To solve this problem, we will have to find the element which is out of order in the array. There can be ... Read More

Possibility of a word from a given set of characters in C++

sudhir sharma
Updated on 17-Apr-2020 07:49:52

179 Views

In this problem, we are two string str1 and str2. Our task is to check whether all characters of str2 and present in str1.Let’s take an example to understand the problemInput −str1 = “Hello” str2 = “Hell”Output − yesExplanation − all characters of str2 are present in str1.To solve this problem, a simple solution will be checking each character of str2 in str1 and then returning the solution.But we need to create effective solutions. So, we will use a frequency array (length 256 for all valid characters) and then traverse str1 and increase the value in frequency array based on ... Read More

Possibility of moving out of maze in C++

sudhir sharma
Updated on 17-Apr-2020 07:46:33

290 Views

In this problem, we are given a maze of n integers, each integer indicates the number of moves to be done. Along with the direction indicated using ‘>’ and ‘ < > >Output − YESExplanation − moving from start, we will move 2 positions ahead, then 1 position ahead, then 4 positions ahead. This will move our of the maze.To solve this problem, we will check whether moving out of the maze is possible or not. For this either we need to go below 0 or above n. Starting from 0, we will process the direction based on the sign ... Read More

Possible cuts of a number such that maximum parts are divisible by 3 in C++

sudhir sharma
Updated on 17-Apr-2020 07:43:24

174 Views

In this problem, we are given a large integer value (with up to 105 digits). Our task is to print the total number of cuts required such that maximum parts are divisible by 3.Let’s take an example to understand the problemInput − 9216Output − 3Explanation − the number is divided as 9|21|6.To solve this problem, we will have to start for the least significant bit of the number i.e. the last digit of the number. For here, we will find the smallest number divisible by three. And then update count based on it.Example − if arr[i] makes a 3 divisible ... Read More

Possible moves of knight in C++

sudhir sharma
Updated on 17-Apr-2020 07:41:13

1K+ Views

In this problem, we are given an m*n chessboard with filled positions marked by 1 i.e. if board[i][j] = 1, there is some piece there and we are given the starting position. Our task is to find the total number of possible moves for a knight in the board, if there are all pieces of the same color i.e. no attack will be done.Knight is chess is a piece that can move in all directions with some special sort of move. The moves of Knight in chess are −Two horizontal moves and a vertical move.Two vertical moves and a horizontal ... Read More

Possible number of Rectangle and Squares with the given set of elements in C++

sudhir sharma
Updated on 17-Apr-2020 07:37:24

362 Views

In this problem, we are given an array of N integers denoting the length of n sticks. Our task is to print the count of rectangles and squares that can be created from the sticks of the given length.Let’s take an example to understand the problemInput − array = {5, 5, 7, 7, 1, 4}Output − 1Explanation − a rectangle with sides 5 5 7 7.To solve this problem, we will have to check if rectangles and squares are possible or not.Now, for creating a square or rectangle, there should be two sticks of the same length, 2 for rectangle and 4 for ... Read More

Possible timings in C++

sudhir sharma
Updated on 17-Apr-2020 07:29:13

75 Views

In this problem, we are given two-digit timing using a glow digit display or seven-segment display (as in calculator). Our task is to calculate the possibility of occurrence of other timings that can occur by glowing or deleting one bit of the display.Seven-segment display is a special display that is used to display digits by glowing lines of the display.Sample of the seven-segment display is −Let’s take an example to understand the problem, Input − 7 5Output −Explanation − For 7, 5 numbers can be used to replace it. They are 9, 3, 8, 0, 7. For 5, 4 numbers ... Read More

Possible to form a triangle from array values in C++

sudhir sharma
Updated on 17-Apr-2020 07:26:45

5K+ Views

In this problem, we are given an array of integers. Our task is to check if the creation of a non-degenerate triangle taking the elements of the array as sides of the triangle.Non-degenerate triangle − it is a triangle that has a positive area. The condition for a non-degenerate triangle with sides a, b, c is −a + b > c a + c > b b + c > aLet’s take an example to understand the problem better −Input − arr[2, 5 ,9, 4, 3]Output − YesExplanation − the triangle formed is 2 3 4.To solve this problem, we ... Read More

Advertisements