Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Narendra Kumar
Page 3 of 18
Minimum value among AND of elements of every subset of an array in C++
Problem statementGiven an array of integers, the task is to find the AND of all elements of each subset of the array and print the minimum AND value among all those.ExampleIf arr[] = {1, 2, 3, 4, 5} then (1 & 2) = 0 (1 & 3) = 1 (1 & 4) = 0 (1 & 5) = 1 (2 & 3) = 2 (2 & 4) = 0 (2 & 5) = 0 (3 & 4) = 0 (3 & 5) = 1 (4 & 5) = 4AlgorithmThe minimum AND value of any subset of the array will be ...
Read MoreMinimum value of "max + min" in a subarray in C++
Problem statementGiven a array of n positive elements we need to find the lowest possible sum of max and min elements in a subarray given that size of subarray should be greater than equal to 2.ExampleIf arr[] = {10, 5, 15, 7, 2, 1, 3} then sum of “max + min” is 3 when we add “2 + 1”.AlgorithmAdding any element to a subarray would not increase sum of maximum and minimum.Since the max of an array will never decrease on adding elements to the array. It will only increase if we add larger elements. So it is always optimal ...
Read MoreMinimum value that divides one number and divisible by other in C++
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 MoreMinimum XOR Value Pair in C++
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#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
Read MoreMirror of n-ary Tree in C++
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#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 < n; ...
Read MoreMissing even and odd elements from the given arrays in C++
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 MoreMissing Permutations in a list in C++
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#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 MoreMax sum of M non-overlapping subarrays of size K in C++
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 MoreMaximum path sum in matrix in C++
In this problem, we are given a 2D matrix of size M*N. Our task is to create a program that will find the maximum path sum in the matrix.Here, the maximum path sum in the matrix is defined as the sum of all elements for one row to the last row. The allowed moves for traversing the path are downward move and diagonal move. The start and endpoints can be any element of the first and last row of the matrix respectively.Let's take an example to understand the problemInput −matrix [][] = 3 5 9 1 7 2 ...
Read MoreMaximum Perimeter Triangle from array in C++
Problem statementGiven an Array of non-negative integers. Find out three elements from the array which form a triangle of maximum perimeterExampleIf input array is {5, 1, 3, 5, 7, 4} then maximum perimeter is (7 + 5 + 5) = 17AlgorithmSort the array in non-increasing order. So, the first element will be maximum and the last will be minimumIf the first 3 elements of this sorted array forms a triangle, then it will be the maximum perimeter triangleExample#include using namespace std; int getMaxPerimeter(int *arr, int n) { sort(arr, arr + n, greater()); int maxPerimeter = 0; ...
Read More