Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 507 of 719
174 Views
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 More
117 Views
Problem statementGiven an array of n integers containing only 0 and 1. Find the minimum toggles (switch from 0 to 1 or vice-versa) required such the array the array become partitioned, i.e., it has first 0s then 1s.ExampleIf arr[] = {1, 0, 0, 1, 1, 1, 0} then 2 toggle is required i.e. toggle first one and last zero.AlgorithmIf we observe the question, then we will find that there will definitely exist a point from 0 to n-1 where all elements left to that point should contains all 0’s and right to point should contains all 1’sThose indices which don’t ... Read More
419 Views
Problem statementGiven a binary string of even length and equal number of 0’s and 1’s. What is the minimum number of swaps to make the string alternating? A binary string is alternating if no two consecutive elements are equalExampleIf str = 11110000 then 2 swaps are required.AlgorithmCount number of zeroes at odd position and even position of the string. Let their count be oddZeroCnt and evenZeroCnt respectivelyCount number of ones at odd position and even position of the string. Let their count be oddOneCnt and evenOneCnt respectivelyWe will always swap a 1 with a 0. So we just check if ... Read More
270 Views
Problem statementGiven an array of 0’s and 1’s. The task is to find the minimum number of swaps required to group all 1’s present in the array together.ExampleIf input array = {1, 0, 1, 1, 0, 1} then 1 swap is required. i.e. swap first 0 with last 1.AlgorithmCount total number of 1’s in the arrayIf count is x, then we need to find the subarray of length x of this array with maximum number of 1’sMinimum swaps required will be the number of 0’s in the subarray of length x with maximum number of 1’sExample Live Demo#include using namespace ... Read More
499 Views
Problem statementGiven an array of n positive integers and a number k. Find the minimum number of swaps required to bring all the numbers less than or equal to k together.ExampleIf input array is = {1, 5, 4, 7, 2, 10} and k = 6 then 1 swap is required i.e. swap element 7 with 2.AlgorithmCount all elements which are less than or equals to ‘k’. Let’s say the count is ‘cnt’Using two pointer technique for window of length ‘cnt’, each time keep track of how many elements in this range are greater than ‘k’. Let’s say the total count ... Read More
203 Views
Problem statementGiven a triangular structure of numbers, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.ExampleIf input is − 5 7 3 8 1 2 9 6 4 5Then minimum sum is 13 as follows −5 + 3 + 1 + 4AlgorithmUse memorization technique of dynamic programmingCreate 1-D array for memorization namely memorizationFor each K row use below formula −memorization[i] = min( memorization[i], memorization[i+1]) + A[k][i];Example Live Demo#include using namespace std; int getMinSum(vector &arr) { int memorization[arr.size()]; int n = arr.size() - 1; for ... Read More
224 Views
Problem statementGiven a binary tree in which each node element contains a number. The task is to find the minimum possible sum from one leaf node to another.ExampleIn above tree minimum sub path is -6 as follows: (-4) + 3 + 2 + (-8) + 1AlgorithmThe idea is to maintain two values in recursive calls −Minimum root to leaf path sum for the subtree rooted under current nodeThe minimum path sum between leavesFor every visited node X, we have to find the minimum root to leaf sum in left and right sub trees of X. Then add the two values ... Read More
534 Views
DescriptionGiven an array of digits which contains values from 0 to 9. The task is to find the minimum possible sum of two numbers formed from digits of the array. Please note that we have to use all digits of given arrayExampleIf input array is {7, 5, 1, 3, 2, 4} then minimum sum is 382 as, we can create two number 135 and 247.AlgorithmSort the array in ascending orderCreate two number by picking a digit from sorted array alternatively i.e. from even and odd indexExample Live Demo#include using namespace std; int getMinSum(int *arr, int n) { sort(arr, arr ... Read More
680 Views
Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example Live Demo#include using namespace std; class Node { public: int data; Node *next; }; void prepend(Node** start, int new_data) ... Read More
485 Views
Here we will see the second largest element in the linked list. Suppose there are n different nodes with numerical values. So if the list is like [12, 35, 1, 10, 34, 1], then second largest element will be 34.This process is similar to the finding of second largest element in an array, we will traverse through the list and find second largest element by comparing.Example#include using namespace std; class Node { public: int data; Node *next; }; void prepend(Node** start, int new_data) { Node* new_node = new Node; new_node->data = ... Read More