Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Articles
Page 210 of 2544
Program to count number of unique subsequences of a string in C++
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 MoreCount the total number of squares that can be visited by Bishop in one move in C++
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 MoreFind if two people ever meet after same number of jumps in C++
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 MoreProgram to create largest lexicographic number from a list of numbers in C++
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 MoreProgram to find final amount that should be paid to employees based on their performance in C++
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 MoreCount the numbers that can be reduced to zero or less in a gamein C++
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 MoreFind index of an extra element present in one sorted array in C++
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 MoreProgram to construct Frequency Stack in C++
Suppose we want to construct one stack called FrequencyStack, Our FrequencyStack has two functions −append(x), This will append or push a value x onto the stack.pop(), This will remove and returns the most frequent element in the stack. If there are more than one elements with the same frequency, then the element closest to the top of the stack is removed and returned.So, if the input is like append some elements like 7, 9, 7, 9, 6, 7, then perform the pop operations four times, then the output will be 7, 9, 7, 6 respectively.To solve this, we will follow ...
Read MoreCount the number of ways to traverse a Matrix in C++
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 MoreProgram to count how many ways we can cut the matrix into k pieces in python
Suppose we have a binary matrix and another value k. You want to split the matrix into k pieces such that each piece contains at least one 1 in it. But there are some rules for cutting, we have to follow in order: 1. Select a direction: vertical or horizontal 2. Select an index in the matrix to cut into two sections. 3. If we cut vertically, we can no longer cut the left part but can only continue cutting the right part. 4. If we cut horizontally, we can no longer cut the top part and can only continue ...
Read More