Count Rectangles with Side Ratio in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:33:03

234 Views

Given sides of rectangles in and range variables first and last. The goal is to find the count of rectangles that have a ratio of their side’s length/breadth lying in the range [ first, last].For ExampleInputrec[] = { { 200, 210 }, { 100, 50 }, { 300, 190}, {180, 200}, {300, 200}} and first = 1.0, last = 1.6OutputCount of number of rectangles such that ratio of sides lies in the range [a, b] are: 4ExplanationThe sides that have ratio in the range [ 1.0, 1.6 ] are : {200, 210}, {300, 190}, {180, 200}, {300, 200}Inputrec[] = { ... Read More

Count Subarrays with Greater Average in C++

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

216 Views

Given an array arr[ ] of positive integers. The goal is to find the count of subarrays of arr[ ] that have an average of its elements greater than the average of the rest of the elements of arr[ ] that are not present in it.For ExampleInputarr[ ] = { 3, 2, 4 }OutputCount of number of sub-arrays such that the average of elements present in the sub−array is greater than that do not present in the sub−array are: 2ExplanationThe subarrays are − [ 3 ], [ 2 ], [ 4 ], [ 3, 2 ], [ 2, 4 ], ... Read More

Count Vowels in All Substrings of Given String in C++

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

483 Views

Given a string str containing English alphabets. The goal is to find the number of vowels occurring in all the substrings of str. If string is “abcde” then substrings will be “a”, “b”, “c”, “d”, “e”, “ab”, “bc”, “cd”, “de”, “abc”, “bcd”, “cde”, “abcd”, “bcde”, “abcde”. The count of vowels in these substrings is 10. (a and e)For ExampleInputstr = ”aloe”OutputCount the number of vowels occurring in all the substrings of given string are: 14ExplanationThe substrings are: “a”, “l”, “o”, “e”, “al”, “lo”, “oe”, “alo”, “loe”, “aloe”. Total vowels in these are: 14Inputstr=”http”OutputCount the number of vowels occurring in all ... Read More

Count Ways to Tile n x m Floor using 1 x m Tiles in C++

Sunidhi Bansal
Updated on 05-Jan-2021 06:27:43

1K+ Views

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

Count the Number of Ways to Traverse a Matrix in C++

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

331 Views

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

Count Numbers Reduced to Zero or Less in a Game in 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 Total Squares Visited by Bishop in One Move in C++

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

892 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

Find 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 Reach a Score Using 1 and 2 with No Consecutive 2s in C++

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

241 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

Advertisements