Server Side Programming Articles - Page 1371 of 2650

Count the numbers that can be reduced to zero or less in a gamein C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:23:32

391 Views

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

Count the total number of squares that can be visited by Bishop in one move in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:21:47

888 Views

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

Count ways to reach a score using 1 and 2 with no consecutive 2s in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:20:10

240 Views

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

How to find the number of occurrences of unique and repeated characters in a string vector in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:21:42

618 Views

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

Count ways to express a number as sum of powers in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:18:20

1K+ Views

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

How to set the X-axis labels in histogram using ggplot2 at the center in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:14:51

7K+ Views

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

Count ways to express a number as sum of consecutive numbers in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:15:04

896 Views

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

How to create horizontal legend using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:13:13

7K+ Views

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

Count ways to express ‘n’ as sum of odd integers in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:12:56

354 Views

Given an integer n as input. The goal is to find the number of ways in which we can represent ‘n’ as the sum of odd integers. For example, if n is 3 it can be represented as sum ( 1+1+1 ) and (3) so total 2 ways.For ExampleInputn=6OutputCount of ways to express ‘n’ as sum of odd integers are: 8ExplanationThe ways in which we can express ‘n’ as sum of odd integers − 1. 1+1+1+1+1+1 2. 3+1+1+1 3. 1+3+1+1 4. 1+1+3+1 5. 1+1+1+3 6. 3+3 7. 1+5 8. 5+1Inputn=9OutputCount of ways to express ‘n’ as sum of odd integers ... Read More

How to remove names from a named vector in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:10:12

7K+ Views

To assign names to the values of vector, we can use names function and the removal of names can be done by using unname function. For example, if we have a vector x that has elements with names and we want to remove the names of those elements then we can use the command unname(x).Example1Live Demo> x1 names(x1) x1OutputG K N V P F F A P D L N K J V H S L F C M F H T I V 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... Read More

Advertisements