Given two numbers n and m representing the length and breadth of the floor of a room. The goal is to count the number of ways in which this floor can be tiled using the tiles of size 1Xm.For ExampleInputn=3 m=2OutputCount the number of ways to tile the floor of size n x m using 1 x m size tiles are: 3ExplanationThe ways will be three 1x2 tiles arranged as shown below −Inputn=3 m=3OutputCount the number of ways to tile the floor of size n x m using 1 x m size tiles are: 2ExplanationThe ways will be three 1x3 ... Read More
Given a 2D matrix with dimensions row X col. The goal is to count the number of ways one can traverse the matrix from cell 0, 0 to cell row, col using only right and down moves, i.e. first move can be 0, 0 to 0, 1 (down) or 1, 0 (right) and not 1, 1(diagonal).For ExampleInputcol = 2; row = 4OutputCount of number of ways to traverse a Matrix are: 4ExplanationThe ways in which we can reach from cell 0, 0 to 2, 4 is shown −Inputcol = 4; row = 3OutputCount of number of ways to traverse a ... Read More
Given an array of positive numbers and two integers A and B. Two players are playing a game in which they will reduce numbers in the array. Player 1 can decrease any element of the array by A and player 2 can increase any element of the array by B. The goal is to find the count of numbers that can be reduced to 0 or less by player 1. The first player makes the first move. The number once reduced to 0 or less can’t be taken into consideration by player 2.For ExampleInputarr[] = { 1, 4, 5, 2 ... Read More
On a chessboard represented as 8 X 8 grid we are given the position of Bishop in form of row and column position. The goal is to find the total number of squares that Bishop can visit in one move. We know the Bishop can move in all directions (diagonally left up/down and right up/down).For ExampleInputrow = 5, column = 4OutputCount of total number of squares that can be visited by Bishop in one move are: 13ExplanationAs shown in above figure the squares that Bishop can cover are 9.Inputrow = 1, column = 1OutputCount of total number of squares that ... Read More
To find the number of occurrences of unique characters in a string vector, we can use table function with the combination of rle and strsplit. For example, if we have a string vector x that contains some unique and repeated values then it can be created by using the below command −table(rle(strsplit(x, "")[[1]]))Example 1Live Demo> x1 x1Output[1] "ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKD"Example> table(rle(strsplit(x1, "")[[1]]))Outputvalues lengths A B C D F J K S V 1 5 1 3 6 3 7 4 3 1 2 0 0 1 1 0 0 0 0 0It means A of length 1 occurred 5 times and A ... Read More
Given a score of runs. The goal is to reach that score in a way that the batsman can take either 1 or 2 runs only in a single ball. The restriction is that no 2 runs can be taken consecutively. For example, to reach the given score 6, one can take runs like: 1+2+1+2 but not 2+2+1+1 or any other way with two consecutive 2’s.For ExampleInputscore=4OutputCount of ways to reach a score using 1 and 2 with no consecutive 2s are: 4ExplanationThe ways in which we can reach the score 4 in following ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1Inputscore=5OutputCount of ... Read More
Given two numbers num and power as input. The goal is to find the ways in which num can be represented as a sum of unique natural numbers raised to the given power. If num is 10 and power is 2 then we can represent 10 as 12+32. So total 1 way.For ExampleInputnum=30OutputCount of ways to express a number as sum of powers are: 2ExplanationThe ways in which we can express 30 as sum of powers: 12 + 22 + 52 and 12 + 22 + 32 + 42Inputnum=35OutputCount of ways to express a number as sum of powers are: ... Read More
Given an integer n as input. The goal is to find the number of ways in which we can represent ‘num’ as the sum of two or more consecutive natural numbers. For example, if n is 3 it can be represented as sum ( 1+2 ) so total 1 way.For ExampleInputnum=6OutputCount of ways to express a number as sum of consecutive numbers are: 1ExplanationThe ways in which we can express ‘num’ as sum of consecutive natural numbers: 1+2+3Inputnum=19OutputCount of ways to express a number as sum of consecutive numbers are: 1ExplanationThe ways in which we can express ‘num’ as sum ... Read More
The boundary argument of geom_histogram function and breaks argument of scale_x_continuous function can help us to set the X-axis labels in histogram using ggplot2 at the center. We need to be careful about choosing the boundary and breaks depending on the scale of the X-axis values. Check out the below example to understand how it works.ExampleConsider the below data frame −ExampleLive Demo> x df dfOutputx 1 5 2 7 3 6 4 4 5 7 6 7 7 10 8 3 9 6 10 6 11 5 12 4 13 4 14 6 15 7 16 4 17 1 18 ... Read More
The default legend direction is vertical but it can be changed to horizontal as well and for this purpose we can use legend.direction argument of theme function of ggplot2 package. For example, if we want to create a bar chart with x as categories and y as frequencies tjat are contained in a data frame df then the bar chart with horizontal legends for categories in x can be created as −ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")+theme(legend.direction="horizontal")ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 A 27 2 B 25 3 C 28Loading ggplot2 package and creating the ... Read More