
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
Found 7197 Articles for C++

442 Views
Suppose we have two arrays A and B with n numbers, we have to rearrange the elements of B in itself in a way such that the sequence formed by (A[i] + B[i]) % n after rearranging it is lexicographically smallest. Finally we will return the lexicographically smallest possible sequence.So, if the input is like A = {1, 2, 3, 2}, B = {4, 3, 2, 2}, then the output will be [0, 0, 1, 2]To solve this, we will follow these steps −n := size of aDefine one map my_mapDefine one set my_setfor initialize i := 0, when i ... Read More

177 Views
Suppose we have an array of different digits; we have to find the largest multiple of 3 that can be generated by concatenating some of the given digits in that array in any order. The answer may be very large so make it as string. If there is no answer return an empty string.So, if the input is like [7, 2, 8], then the output will be 87.To solve this, we will follow these steps −Define one 2D array d, there will be three rowssort the array digitssum := 0for initialize i := 0, when i < size of digits, ... Read More

289 Views
Suppose we have a 2D matrix mat and a value K, we have to find the longest rectangular submatrix whose sum is same as K.So, if the input is like28-56-778-311-1443-43110And K = 9then the output will be Top-Left point is (1, 0) and Bottom-Right point is (3, 2).-77811-144-431To solve this, we will follow these steps −MAX := 100Define a function sum_k(), this will take one array arr, start, end, n, k, Define one mapsum := 0, maximum_length := 0for initialize i := 0, when i < n, update (increase i by 1), do −sum := sum + arr[i]if sum is ... Read More

387 Views
Suppose we have an array A. In this array there are different numbers that occurs twice. But there is only one number that occurs once. We have to find that element from that array.Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there are each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res := res XOR ereturn resExample Let us see the ... Read More

182 Views
Suppose we have an array of N numbers, where each element in the array appears same number of times (m times, this is also given) except one element, We have to find this element.So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.To solve this, we will follow these steps −INT_SIZE := 8 * size of an integer type variableDefine an array count of size − INT_SIZE. and fill with 0for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do:for initialize ... Read More

700 Views
Suppose we have a string which contains only L and R, this denotes left rotation and right rotation respectively, we have to find the final direction of pivot. Here directions are north(N), east(E), south(S) and west(W). We are assuming that the pivot is pointed towards north(N) in a compass.So, if the input is like "RRLRLLR", then the output will be E, as initial direction is N, RR will point to S, then LR will point to the same N again, then LL will point to previous position N, then R will point to E. So E is the final.To solve ... Read More

382 Views
In this article, we will learn to solve a popular problem that involves finding the closest element in a binary search tree (BST) to a given target value. There are two methods to solve this problem: Brute Force Method (Inorder Traversal) Optimized Method (DFS Traversal) Before moving to the solution, let's understand the problem statement in detail. Find the Closest Element in Binary Search Tree Given a binary search tree (BST) and a target value K, the task is to find a node in BST, whose value is ... Read More

86 Views
Suppose we have k different lists. The elements are sorted. We have to search the smallest range that includes at least one number from each of the k different lists. Here the range [a, b] is smaller than range [c, d] when b-a < d-c or a < c if b-a == d-c.So if the input is like [[4, 10, 15, 25, 26], [0, 9, 14, 20], [5, 18, 24, 30]], then the output will be [14, 18]To solve this, we will follow these steps −minRange := inf, maxRange := -inf, rangeSize := inf, tempMinRange := inf, tempMaxRange := -infn ... Read More

361 Views
Suppose we have a square maze filled with numbers; we have to find all paths from a corner cell to the middle cell. Here, we will proceed exactly n steps from a cell in 4 directions Up, Down, Right and Left where n is the value of the cell. Thus, we can move to cell [i+n, j] to [i-n, j], [i, j+n], and [i, j-n] from a cell [i, j] where n is value of cell [i, j].So, if the input is like344473463675662662334325472655123656334301434334321335354326443351375363624345451then the output will be(0, 0)→(0, 3)→(0, 7)→(6, 7)→(6, 3)→(3, 3)→(3, 4)→(5, 4)→(5, 2)→(1, 2)→(1, 7)→(7, 7)→(7, 1)→(2, ... Read More

165 Views
In this tutorial, we will be discussing a program to find query for ancestor-descendant relationship in a tree.For this we will be provided with a rooted tree and Q queries. Our task is to find the two roots given in the query is an ancestor of the other or not.Example Live Demo#include using namespace std; //using DFS to find the relation between //given nodes void performingDFS(vector g[], int u, int parent, int timeIn[], int timeOut[], int& count) { timeIn[u] = count++; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; ... Read More