Programming Articles

Page 215 of 2545

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 275 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

Find if there is any subset of size K with 0 sum in an array of -1 and +1 in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 222 Views

In this problem, we are given an array arr[] consisting of only 1 and -1 and an integer value k. Our task is to find if there is any subset of size K with 0 sum in an array of -1 and +1. Let’s take an example to understand the problem, Input: arr[] = {-1, 1, -1, -1, 1 , 1, -1}, k = 4Output: YESExplanation:Subset of size 4, {-1, 1, -1, 1}. Sum = -1 + 1 - 1 + 1 = 0Solution Approach: We need to check if there exists any subset of size k whose sum is equal to 0. As ...

Read More

Program to find maximum possible population of all the cities in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 735 Views

Consider a country that is represented as a tree with N nodes and N-1 edges. Now each node represents a town, and each edge represents a road. We have two lists of numbers source and dest of size N-1. According to them the i-th road connects source[i] to dest[i]. And the roads are bidirectional. We also have another list of numbers called population of size N, where population[i] represents the population of the i-th town. We are trying to upgrade some number of towns into cities. But no two cities should be adjacent to each other and every node adjacent ...

Read More

Program to count number of unique subsequences of a string in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 708 Views

Suppose we have a string s, we have to find the number of non-empty unique subsequences of s. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xxy", then the output will be 5, as there are five subsequences: "x", "xx", "xy", "y" and "xxy".To solve this, we will follow these steps −m := 10^9 + 7n := size of sDefine an array table of size 26res := 0for initialize i := 1, when i

Read More

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 969 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 if two people ever meet after same number of jumps in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 340 Views

In this problem, we are given four integers denoting the starting points and jumps taken by each in the race. Our task is to find if two people ever meet after same number of jumps.  Problem Description: Here, we need to check if two persons starting at points p1 and p2 taking jumps j1 and j2 will be at some point in the path or not.Let’s take an example to understand the problem, Input: p1 = 5, p2 = 9, j1 = 4, j2 = 2Output: YesExplanation:After first jump, p1 = 9, p2 = 11After second jump, p1 = 13, p2 = 13Solution Approach: For ...

Read More

Program to create largest lexicographic number from a list of numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 373 Views

Suppose we have a list of numbers called nums, we have to rearrange its order to form the largest possible number and return that as a string.So, if the input is like nums = [20, 8, 85, 316], then the output will be "88531620".To solve this, we will follow these steps −Define an array tempfor each item i in nums:insert i into temp as stringsort the array temp based on lexicographic sequence (check for two strings a, b when a concatenate b is larger than b concatenate a or not)for each string s in temp:res := res concatenate sreturn resLet ...

Read More

Program to find final amount that should be paid to employees based on their performance in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 257 Views

Suppose we have two lists of numbers of same length called performance and costs. And we also have another number k. These indicates that each worker i performs at performance[i] level and it takes costs at least costs[i]. We have to find the minimum cost to hire k employees given also that the workers will be paid proportionate to their performance compared to other employees in the group.So, if the input is like performance = [5, 3, 2] costs = [100, 5, 4] k = 2, then the output will be 10, as we can select emp1 and emp2. They ...

Read More

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 434 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

Find index of an extra element present in one sorted array in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 774 Views

In this problem, we are given two sorted arrays arr1 and arr2 of size n  and n+1 with all elements the same except the extra element. Our task is to find index of an extra element present in one sorted array. Problem Description: We need to find the index of an element from the n+1 size array which is not present in an array of size n.Let’s take an example to understand the problem, Input:         arr1[n]  = {3, 5, 7, 8, 9, 12}                   arr2[n+1] = {3, 4, 5, 7, 8, 9, ...

Read More
Showing 2141–2150 of 25,445 articles
« Prev 1 213 214 215 216 217 2545 Next »
Advertisements