Found 7197 Articles for C++

C++ Program for Best Fit algorithm in Memory Management

Sunidhi Bansal
Updated on 20-Dec-2019 12:30:51

5K+ Views

Given two arrays containing block size and process size; the task is to print the results according to Best Fit algorithm in memory management.What is Best Fit Algorithm?Best Fit is a memory management algorithm; it deals with allocating smallest free partition which meets the requirement of the requesting process. In this algorithm we look for the whole memory block and check the smallest and most appropriate block for the process and then look for the immediate near block which can be used to fulfill the adequate process.So we will take the block size and process size and return the output ... Read More

C++ Program for Optimal Page Replacement Algorithm

Sunidhi Bansal
Updated on 20-Dec-2019 12:20:43

4K+ Views

Given page number and page size; the task is to find number of hits and misses as when we allocate the memory block to a page using Optimal Page Replacement Algorithm.What is Optimal Page Replacement Algorithm?Optimal page replacement algorithm is a page replacement algorithm. A page replacement algorithm is an algorithm which decides which memory page is to be replaced. In Optimal page replacement we replace the page which is not referred to the near future, although it can’t be practically implemented, but this is most optimal and have minimal miss, and is most optimal.Let’s understand by using an example ... Read More

C++ Program for Derivative of a Polynomial

Sunidhi Bansal
Updated on 20-Dec-2019 11:32:06

5K+ Views

Given a string containing the polynomial term, the task is to evaluate the derivative of that polynomial.What is a Polynomial?Polynomial comes from two words: - “Poly” which means “many” and “nomial” means “terms”, which comprises many terms. Polynomial expression is an expression containing variables, coefficients and exponents, which only involves operations such as, addition, multiplication and subtraction of variable(s).Example of polynomialx2+x+1Derivative of the polynomial p(x) = mx^n  will be −m * n * x^(n-1)ExampleInput: str = "2x^3 +1x^1 + 3x^2"    val = 2 Output: 37 Explanation: 6x^2 + 1x^0 + 6x^1    Putting x = 2    6*4 + ... Read More

Program for Mobius Function in C++

Sunidhi Bansal
Updated on 20-Dec-2019 11:17:22

464 Views

Given a number n; the task is to find the Mobius function of the number n.What is Mobius Function?A Mobius function is number theory function which is defined by$$\mu(n)\equiv\begin{cases}0\1\(-1)^{k}\end{cases}$$n=  0 If n has one or more than one repeated factorsn= 1 If n=1n= (-1)k  If n is product of k distinct prime numbersExampleInput: N = 17 Output: -1 Explanation: Prime factors: 17, k = 1, (-1)^k 🠠(-1)^1 = -1 Input: N = 6 Output: 1 Explanation: prime factors: 2 and 3, k = 2 (-1)^k 🠠(-1)^2 = 1 Input: N = 25 Output: 0 Explanation: Prime factor is ... Read More

Max sum of M non-overlapping subarrays of size K in C++

Narendra Kumar
Updated on 20-Dec-2019 10:39:21

293 Views

Problem statementGiven an array and two numbers M and K. We need to find sum of max M subarrays of size K (non-overlapping) in the array. (Order of array remains unchanged). K is the size of subarrays and M is the count of subarray. It may be assumed that size of array is more than m*k. If total array size is not multiple of k, then we can take partial last array.ExampleIf Given array is = {2, 10, 7, 18, 5, 33, 0}. N = 7, M = 3 and K = 1 then output will be 61 as subset ... Read More

Missing Permutations in a list in C++

Narendra Kumar
Updated on 20-Dec-2019 10:34:29

125 Views

Problem statementGiven a list of permutations of any word. Find the missing permutation from the list of permutations.ExampleIf permutation is = { “ABC”, “ACB”, “BAC”, “BCA”} then missing permutations are {“CBA” and “CAB”}AlgorithmCreate a set of all given stringsAnd one more set of all permutationsReturn difference between two setsExample Live Demo#include using namespace std; void findMissingPermutation(string givenPermutation[], size_t permutationSize) {    vector permutations;    string input = givenPermutation[0];    permutations.push_back(input);    while (true) {       string p = permutations.back();       next_permutation(p.begin(), p.end());       if (p == permutations.front())          break;     ... Read More

Missing even and odd elements from the given arrays in C++

Narendra Kumar
Updated on 20-Dec-2019 10:29:28

296 Views

Problem statementGiven two integer arrays even [] and odd [] which contains consecutive even and odd elements respectively with one element missing from each of the arrays. The task is to find the missing elements.ExampleIf even[] = {10, 8, 6, 16, 12} and odd[] = {3, 9, 13, 7, 11} then missing number from even array is 14 and from odd array is 5.AlgorithmStore the minimum and the maximum even elements from the even[] array in variables minEven and maxEvenSum of first N even numbers is N * (N + 1). Calculate sum of even numbers from 2 to minEven ... Read More

Mirror of n-ary Tree in C++

Narendra Kumar
Updated on 20-Dec-2019 10:25:57

220 Views

Problem statementGiven a Tree where every node contains variable number of children, convert the tree to its mirrorExampleIf n-ary tree is −Then it’s mirror is −Example Live Demo#include using namespace std; struct node {    int data;    vectorchild; }; node *newNode(int x) {    node *temp = new node;    temp->data = x;    return temp; } void mirrorTree(node * root) {    if (root == NULL) {       return;    }    int n = root->child.size();    if (n < 2) {       return;    }    for (int i = 0; i < ... Read More

Minimum XOR Value Pair in C++

Narendra Kumar
Updated on 20-Dec-2019 10:21:42

154 Views

Problem statementGiven an array of integers. Find the pair in an array which has minimum XOR valueExampleIf arr[] = {10, 20, 30, 40} then minimum value pair will be 20 and 30 as (20 ^ 30) = 10. (10 ^ 20) = 30 (10 ^ 30) = 20 (10 ^ 40) = 34 (20 ^ 30) = 10 (20 ^ 40) = 60 (30 ^ 40) = 54AlgorithmGenerate all pairs of given array and compute XOR their valuesReturn minimum XOR valueExample Live Demo#include using namespace std; int getMinValue(int *arr, int n) {    int minValue = INT_MAX;    for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {          minValue = min(minValue, arr[i] ^ arr[j]);       }    }    return minValue; } int main() {    int arr[] = {10, 20, 30, 40};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Minimum value that divides one number and divisible by other in C++

Narendra Kumar
Updated on 20-Dec-2019 10:18:48

159 Views

Problem statementGiven two integer p and q, the task is to find the minimum possible number x such that q % x = 0 and x % p = 0. If the conditions aren’t true for any number, then print -1.ExampleIf p = 3 and q = 66 then answer is 3 as: 66 % 3 = 0 3 % 3 = 0AlgorithmIf a number x satisfies the given condition, then it’s obvious that q will be divided by p i.e. q % p = 0 because x is a multiple of p and q is a multiple of xSo ... Read More

Advertisements