Server Side Programming Articles - Page 2128 of 2650

Minimum number of given moves required to make N divisible by 25 using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:52:07

379 Views

Problem statementGiven a number N without leading zeros. The task is to find the minimum number of moves required to make N divisible by 25. At each move, one can swap any two adjacent digits and make sure that at any time number must not contain any leading zeros. If it is not possible to make N divisible by 25 then print -1If N = 5071 then 4 moves are required to make it divisible by 255071 → 5701 → 7501 → 7510 → 7150Algorithm1. Iterate over all pairs of digits in the number. Let the first digit in the ... Read More

Minimum number of elements to be removed to make XOR maximum using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:48:11

199 Views

Problem statementGiven a number N. The task is to find the minimum number of elements to be removed in between to N such that the XOR obtained from the remaining elements is maximum.Algorithm1. If n is 1 or 2 then there is no need to remove any element. Hence answer is zero 2. Find a number which is power of 2 and greater than or equal to. Let us call this number as nextNumber    2.1. If n == nextNumber or n == (nextNumber – 1) then answer is 1    2.2. If n = (nextNumber -2) then answer is ... Read More

Minimum number of elements to add to make median equals x using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:45:26

245 Views

Problem statementGiven an array “arr” of size n and element x, the task is to find a minimum number of elements to be added in array to make median equals x.A median in an array with the length of n is an element which occupies position number (n-1)/2 after we sort the elements in the non-decreasing order. For example, in below array median is 20 −arr1[] = {10, 20, 30, 40}If arr[] = {1, 2, 3} and x = 4 then we have to add 4 number i.e. {4, 5, 5, 5} in array to make median equal to 4AlgorithmThe ... Read More

Minimum number of elements that should be removed to make the array good using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:43:01

1K+ Views

Problem statementGiven an array “arr”, the task is to find the minimum number of elements to be removed to make the array good.A sequence a1, a2, a3. . .an is called good if for each element a[i], there exists an element a[j] (i not equals to j) such that a[i] + a[j] is a power of two.arr1[] = {1, 1, 7, 1, 5}In above array if we delete element ‘5’ then array becomes good array. After this any pair of arr[i] + arr[j] is power of two −arr[0] + arr[1] = (1 + 1) = 2 which power of twoarr[0] ... Read More

Minimum number of deletions to make a string palindrome in C++.

Narendra Kumar
Updated on 31-Oct-2019 06:40:01

1K+ Views

Problem statementGiven a string of size ‘n’. The task is to delete a minimum number of characters to make string palindrome.If the given string is “abcda” then we can delete any 2 characters except first and last to make it a palindrome.If we delete character ‘b’ and ‘c’ then “ada” string is a palindromeIf we delete character ‘c’ and ‘d’ then “aba” string is a palindromeIf we delete character ‘b’ and ‘d’ then “aca” string is a palindromeAlgorithm1. Find longest palindromic subsequence of given string. Let’s call it as “lpsSize”. 2. Minimum characters to be deleted = (length of string ... Read More

Minimum number of deletions and insertions to transform one string into another using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:33:35

425 Views

DescriptionGiven two strings str1 and str2 of size m and n respectively. The task is to delete and insert a minimum number of characters from/in str1 so as to transform it into str2.Str1 = “tutorialspoint” Str2 = “tutorials” To transform str1 to str2 we have to delete five characters i.e. “point” from str1.Algorithm1. Find longest common subsequence of str1 and str2. Let’s call it as “lcsSize” 2. Number of characters to be deleted = (length of str1 - lcsSize) 3. Number of characters to be inserted = (length of str2 - lcsSize)Example#include #include using namespace std; int lcs(string ... Read More

Merge Overlapping Intervals using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:27:59

620 Views

Problem statementGiven a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervalsGiven set of interval is {{12, 14}, {11, 13}, {20, 22}, {21, 23}} thenInterval {12, 14} and {11, 13} overlap with each other hence merge them as {11, 14}Interval {20, 22} and {21, 23} overlap with each other hence merge them as {20, 23}Algorithm1. Sort the intervals based on increasing order of starting time 2. Push the first interval on to a stack 3. For each interval perform below steps:    3.1. If the ... Read More

Merge Sort for Doubly Linked List using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:24:22

579 Views

Problem statementGiven a doubly linked list. Sort it using merge sort algorithmList: 10->20->8-17->5->13->4 Sorted list: 4->5->8->10->13->17->20Algorithm1. If head is NULL or list contains only one element then return list 2. Create two lists by dividing original list into 2 parts 3. Sort first and second part of list 4. Merge both sorted listExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct node {    int data;    struct node *next;    struct node *prev; }; node *createList(int *arr, int n){    node *head, *p, *q;    p = head = new node;    head->data = arr[0]; ... Read More

Merge Sort for Linked Lists using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:21:00

310 Views

Problem statementGiven a linked list. Sort it using merge sort algorithmList: 10->20->8-17->5->13->4 Sorted list: 4->5->8->10->13->17->20Algorithm1. If head is NULL or list contains only one element then return list 2. Create two lists by dividing original list into 2 parts 3. Sort first and second part of list 4. Merge both sorted listExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct node {    int data;    struct node *next; }; node *createList(int *arr, int n){    node *head, *p;    p = head = new node;    head->data = arr[0];    head->next = NULL;    for ... Read More

Merge two binary Max Heaps using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:18:19

317 Views

Problem statementGiven two binary max heaps as arrays, merge the into single max heap.Heap1[] = {20, 17, 15, 10} Heap2[] = {19, 13, 7} Result[] = {20, 19, 15, 13, 17, 7, 10}Algorithm1.Create an array to store result 2. Copy both given arrays one by one to result array 3. Build heap to construct full merged max heapExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; void heapify(int *arr, int n, int idx){    if (idx >= n) {       return;    }    int l = 2 * idx + 1;    int r ... Read More

Advertisements