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

252 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

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

Count Ways to Express a Number as Sum of Consecutive Numbers in C++

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

941 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

Set X-Axis Labels in Histogram Using ggplot2 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

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

375 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

Count Ways to Divide Circle Using n Non-Intersecting Chords in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:11:06

476 Views

Given an integer N as input for a number of chords in a circle with 2*N end points. The goal is to count the ways in which we can divide that circle using such chords so that no chord intersects with each other.For N=3, points will be 6, 1 way of getting 3 chords is between 1−2, 3−4, 5−6Other ways −1−6, 2−5, 3−4 1−2, 3−6, 4−5 1−4, 2−3, 5−6 1−6, 2−3, 4−5Total 5 ways.For ExampleInputN=4OutputCount of ways to divide circle using N non-intersecting chords are: 14ExplanationThere will be a total 8 points between which we can draw chords. After drawing ... Read More

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

Count Trailing Zeros in Factorial of a Number in C++

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

2K+ Views

Given an integer number as input. The goal is to find the number of trailing zeroes in the factorial calculated for that number. A factorial of a number N is a product of all numbers in the range [1, N].We know that we get a trailing zero only if the number is multiple of 10 or has a factor pair (2, 5). In all factorials of any number greater than 5, we have a number of 2s more than 5s in prime factorization of that number. Dividing a number by powers of 5 will give us the count of 5s ... Read More

Convert String Vector into Title Case in R

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:08:33

704 Views

We cannot be sure about the data characteristics we get for analysis and mostly it is not well organised, thus, the first task would be to make it more organised. The string values not in title case should also be taken care of if it is especially supposed to be in title case. For this purpose, we can str_to_title function of stringr package.Example1Live Demo> x1 x1Output[1] "india" "united kingdom" "indonesia" "canada" [5] "canada" "india" "united kingdom" "canada" [9] "indonesia" "united kingdom" "indonesia" "canada" [13] "russia" "indonesia" "canada" "russia" [17] "united kingdom" "russia" "russia" "india" [21] "united kingdom" "india" "india" "united ... Read More

Advertisements