C++ Articles

Page 563 of 597

Merge two sorted linked lists using C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 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
Narendra Kumar
Updated on 31-Oct-2019 938 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
Narendra Kumar
Updated on 31-Oct-2019 533 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

Read More

Mid-Square hashing in C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 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

Mersenne Prime Number in C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 769 Views

DescriptionIn mathematics, a Mersenne prime is a prime number that is one less than a power of two. That is, it is a prime number of the form Mn = 2n − 1 for some integer n.Write a C++ program to print all Mersenne Primes smaller than an input positive integer n.The exponents n which give Mersenne primes are 2, 3, 5, 7, ... and the resulting Mersenne primes are 3, 7, 31, 127Algorithm1. Generate all the primes less than or equal to the given number n 2. Iterate through all numbers of the form 2n-1 and check if they ...

Read More

Find the unit place digit of sum of N factorials using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 255 Views

Here we will see how to get the unit place digit of the sum of N factorials. So if N is 3, then after getting sum, we will get 1! + 2! + 3! = 9, this will be the result, for N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3. If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. For N = 4 and more, it will ...

Read More

Find number of pairs in an array such that their XOR is 0 using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 237 Views

Suppose we have an array of n elements; we have to find a number of pairs in the array whose XOR will be 0. The pair (x, y) whose XOR is 0, then x = y. To solve it we can sort the array, then if two consecutive elements are the same, increase the count. If all elements are the same, then the last count may not be counted. In that case, we will check whether the last and first elements are the same or not, if the same, then increase the count by 1.Example#include #include using namespace std; int countPairs(int arr[],  int n) {     int count = 0; ...

Read More

Find max in struct array using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 790 Views

Here we will see how to get max in the struct array. Suppose there is a struct like below is given. We have to find the max element of an array of that struct type. struct Height{     int feet, inch; }; The idea is straight forward. We will traverse the array, and keep track of the max value of array element in inches. Where value is 12*feet + inch Example #include #include using namespace std; struct Height{     int feet, inch; }; int maxHeight(Height h_arr[], int n){     int index = 0;     int height = INT_MIN;     for(int i ...

Read More

Find last two digits of sum of N factorials using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 257 Views

Here we will see how to get the last two digits. The unit place digit and the tens place digit of the sum of N factorials. So if N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3 and ten place is 3. The result will be 33.If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. And after N > 10, the ten places will remain 0. For N ...

Read More

Find floor and ceil in an unsorted array using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Oct-2019 747 Views

Here we will see how to find the floor and ceiling in an unsorted array. The floor value is larger element which is smaller than or equal to x, and the ceiling value is smallest value which is larger than x. If the array A = [5, 6, 8, 9, 6, 5, 5, 6], and x is 7, then the floor value is 6, and the ceiling value is 8.To solve this problem, we will follow the linear search approach. We will traverse the array and track two distances with respect to x.Min distance of element greater than or equal ...

Read More
Showing 5621–5630 of 5,962 articles
« Prev 1 561 562 563 564 565 597 Next »
Advertisements