Found 7347 Articles for C++

Product of lengths of all cycles in an undirected graph in C++

Sunidhi Bansal
Updated on 23-Dec-2019 07:30:16

177 Views

We are given with the undirected as well as unweighted graph as an input and the task is to find the product of the cycles that are formed in the given and display the result.ExampleInputIn the given figure, there are 8 nodes and out of that 5 nodes are forming cycle including 1, 6, 3, 5, 8 and rest of the node are not included in the cycle. So, the length of the cycle is 5 as it includes 5 node therefore product is 5In the given figure, there are 12 nodes and out of that 11(5 +6) nodes are ... Read More

Priority Queue Introduction in C/C++

Sunidhi Bansal
Updated on 23-Dec-2019 07:25:27

347 Views

A priority queue is a type of queue in which elements are inserted or deleted according to the priorities assigned to them where priority is an integer value that can range between 0-10 where 0 shows the element with the highest priority and 10 shows the element with the lowest priority. There are two rules that are followed for implementing priority queue −Data or element with the highest priority will get executed before the data or element with the lowest priority.If two elements have the same priority than they will be executed in the sequence they are added in the ... Read More

Probability of getting a sum on throwing 2 Dices N times in C++

Sunidhi Bansal
Updated on 23-Dec-2019 07:24:49

251 Views

We are given with the sum and the number of times the pair of dice is thrown as an input and the task is to determine the probability of getting the given sum on throwing a pair of dice N times.Probability is the chances of getting the desired output from the set of data available. The range of probability lie between 0 and 1 where an integer 0 shows the chances of impossibility and 1 indicates certainty.ExampleInput-: sum = 12, N = 1 Output-: Probability = 1/36 Explanation-: if a pair of dice is thrown once then the combinations will ... Read More

Priority Queue using doubly linked list in C++

Sunidhi Bansal
Updated on 23-Dec-2019 07:10:39

559 Views

We are given with the data and the priority as an integer value and the task is to create a doubly linked list as per the priority given and display the result.Queue is a FIFO data structure in which the element which is inserted first is the first one to get removed. A Priority Queue is a type of queue in which elements can be inserted or deleted depending upon the priority. It can be implemented using queue, stack or linked list data structure. Priority queue is implemented by following these rules −Data or element with the highest priority will ... Read More

Minimum sum falling path in a NxN grid in C++

Narendra Kumar
Updated on 23-Dec-2019 07:05:13

129 Views

Problem statementGiven a matrix A of integers of size NxN. The task is to find the minimum sum of a falling path through A.A falling path will start at any element in the first row and ends in last row.It chooses one element from each next row. The next row’s choice must be in a column that is different from the previous row’s column by at most onesExampleIf N = 2 and matrix is: {    {5, 10},    {25, 15} } then output will be 20 as element 5 and 15 are selectedExample Live Demo#include #define MAX 2 using ... Read More

Minimum steps to remove substring 010 from a binary string in C++

Narendra Kumar
Updated on 23-Dec-2019 07:02:29

91 Views

Problem statementGiven a binary string, the task is to count the minimum steps to remove substring 010 from this binary stringExampleIf input string is 010010 then 2 steps are requiredConvert first 0 to 1. Now string becomes 110010Convert last 0 to 1. Now final string becomes 110011Algorithm1. Iterate the string from index 0 sto n-2 2. If in binary string has consecutive three characters ‘0’, ‘1’, ‘0’ then any one character can be changed Increase the loop counter by 2Example Live Demo#include using namespace std; int getMinSteps(string str) {    int cnt = 0;    for (int i = ... Read More

C++ Program for Shortest Job First (SJF) scheduling(preemptive)

Sunidhi Bansal
Updated on 23-Dec-2019 07:02:50

15K+ Views

Given process, the burst time of a process respectively and a quantum limit; the task is to find and print the waiting time, turnaround time and their respective average time using Shortest Job First Scheduling preemptive method.What is the shortest job first scheduling?Shortest job first scheduling is the job or process scheduling algorithm that follows the nonpreemptive scheduling discipline. In this, scheduler selects the process from the waiting queue with the least completion time and allocate the CPU to that job or process. Shortest Job First is more desirable than FIFO algorithm because SJF is more optimal as it reduces ... Read More

Minimum steps to make all the elements of the array divisible by 4 in C++

Narendra Kumar
Updated on 23-Dec-2019 06:57:29

379 Views

Problem statementGiven an array of size n, the task iss to find the minimum number of steps required to make all the elements of the array divisible by 4. A step is defined as removal of any two elements from the array and adding the sum of these elements to the arrayExampleIf input array is {1, 2, 0, 2, 4, 3} then 3 operations are required −1 + 3 = 4 2 + 2 = 4 0 + 4 = 4Algorithm1. Sum of all the elements of the array should be divisible by If not, this task is not possible ... Read More

Minimum steps to delete a string after repeated deletion of palindrome substrings in C++

Narendra Kumar
Updated on 23-Dec-2019 06:52:26

767 Views

Problem statementGiven a string containing characters as integers only. We need to delete all character of this string in a minimum number of steps where in one step we can delete the substring which is a palindrome. After deleting a substring remaining parts are concatenated.ExampleIf input string is 3441213 then minimum 2 steps requiredFirst remove 121 from string. Now remaining string is 3443Remove remaining string as it’s palindromeAlgorithmWe can use dynamic programming to solve this problem1. Let dp[i][j] denotes the number of steps it takes to delete the substring s[i, j] 2. Each character will be deleted alone or as ... Read More

Minimum rotations required to get the same string in C++

Narendra Kumar
Updated on 23-Dec-2019 06:45:35

227 Views

Problem statementGiven a string, we need to find the minimum number of rotations required to get the same stringExampleIf input string is “bbbbb” then minimum 1 rotation is requiredAlgorithm1. Initialize result = 0 2. Make a temporary string equals to original string concatenated with itself. 3. Take the substring of temporary string of size same as original string starting from second character i.e. index 1 4. Increment the counter 5. Check whether the substring becomes equal to original string. If yes, then break the loop. Else go to step 2 and repeat it from the next indexExample Live Demo#include using ... Read More

Advertisements