Found 7197 Articles for C++

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

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

412 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

609 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

568 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

301 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

302 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

Merge two sorted arrays using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:14:58

3K+ Views

Problem statementGiven 2 sorted arrays list. Write a function to merge given two sorted arrays into oneArr1[] = {10, 15, 17, 20} Arr2[] = {5, 9, 13, 19} Result[] = {5, 9, 10, 13, 15, 17, 19, 20}Algorithm1. Traverse both array    1.1. If arr1[i] < arr2[j]       1.1.1. Add arr[i] to new array       1.1.2. Increment ‘i’ and index of result array ‘k’    1.2. If arr2[i] < arr1[j]       1.2.1. Add arr[j] to new array       1.2.2. Increment ‘j’ and index of result array ‘k’ 2. Repeat procedure until both ... Read More

Merge two sorted linked lists using C++.

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

3K+ Views

Problem statementGiven 2 sorted singly linked list. Write a function to merge given two sorted linked listsList1: 10->15->17->20 List2: 5->9->13->19 Result: 5->9->10->13->15->17->19->20Algorithm1. Traverse both lists    1.1. If list1->data < list2->data       1.1.1 Add list1->data to new list and increment list1 pointer    1.2 If list2->data < list1->data       1.2.1 Add list2->data to new list and increment list2 pointer 2. Repeat procedure until both lists are exhausted 3. Return resultant 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, ... Read More

Merging elements of two different arrays alternatively in third array in C++.

Narendra Kumar
Updated on 31-Oct-2019 06:09:34

849 Views

Problem statementGiven two arrays, we need to combine two arrays in such a way that the combined array has alternate elements of the first and second array. If one of the arrays has an extra element, then these elements should be appended at the end of the combined array.arr1[] = {10, 20, 30, 40} arr2[] = {-10, -20, -30, -40} result[] = {10, -10, 20, -20, 30, -30, 40, -40}Algorithm1. Traverse both arrays and one by one put elements into result array. 2. If one of the array exhausts then put remaining elements of other array into result array.Example#include ... Read More

Merging two unsorted arrays in sorted order in C++.

Narendra Kumar
Updated on 31-Oct-2019 06:03:10

460 Views

Problem statementWrite a function that takes two unsorted arrays and merges them into a new array in sorted order.arr1[] = {10, 5, 7, 2} arr2[] = {4, 17, 9, 3} result[] = {2, 3, 4, 5, 7, 9, 10, 17}Algorithm1. Merge two unsorted array into new array 2. Sort newly create arrayExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; void mergeAndSort(int *arr1, int n1, int *arr2, int n2, int *result){    merge(arr1, arr1 + n1, arr2, arr2 + n2, result);    sort(result, result + n1 + n2); } void displayArray(int *arr, int n){    for (int i = 0; i < n; ++i) {       cout

Mid-Square hashing in C++.

Narendra Kumar
Updated on 31-Oct-2019 05:56:52

3K+ Views

Problem statementThe mid-square method is a method of generating pseudorandom numbers. This method was invented by John von Neumann and was described at a conference in 1949In this technique, an initial seed value is taken and it is squared.Some digits from the middle are extracted and these extracted digits form a number which is taken as the new seed.Let us take 3456 as seed. Its square is 11943936Take the middle 4 digits as new seed i.e. 9439. Its square is 89094721Take middle 4 digits as new seed i.e. 0947Repeat this processAlgorithm1. Choose initial seed value 2. Take the square of ... Read More

Advertisements