Priority Queue Using Doubly Linked List in C++

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

754 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

235 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

Number of Elements with Odd Factors in Given Range

Pavitra
Updated on 23-Dec-2019 07:03:27

589 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a range, we need to find the number of odd factors in the range.ApproachAs we all know that all perfect squares have an odd number of factors in a range. So here we will compute a number of perfect squares.As m and n both are inclusive, so to avoid error in case of n being a perfect square we take n-1 in the formulae.Now let’s see the implementation below−Example Live Demo# count function def count(n, m):    return int(m**0.5) - int((n-1)**0.5) # ... Read More

C++ Program for Shortest Job First (SJF) Scheduling - Preemptive

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

19K+ 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 Remove Substring 010 from a Binary String in C++

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

215 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

Nth Multiple of a Number in Fibonacci Series

Pavitra
Updated on 23-Dec-2019 07:00:09

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement− We are given a number, we need to find the nth multiple of a number k in Fibonacci number.The solution to the problem is discussed below−Example Live Demo# find function def find(k, n):    f1 = 0    f2 = 1    i =2;    #fibonacci recursion    while i!=0:       f3 = f1 + f2;       f1 = f2;       f2 = f3;       if f2%k == 0:          return n*i     ... Read More

Minimum Steps to Make Array Elements Divisible by 4 in C++

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

488 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

C++ Program for Shortest Job First (SJF) Scheduling Non-Preemptive

Sunidhi Bansal
Updated on 23-Dec-2019 06:52:39

7K+ 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 non-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 Delete a String after Repeated Deletion of Palindrome Substrings in C++

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

962 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

319 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