- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 7347 Articles for C++
118 Views
Suppose we have an array that represents elements of arithmetic progression in order. One element is missing. We have to find the missing element. So if arr = [2, 4, 8, 10, 12, 14], output is 6, as 6 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the difference between middle and next to the middle is same as diff or not. If not, then missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the AP, then missing ... Read More
224 Views
Suppose we have a number N, and unlimited number of coins worth 1, 10 and 25 currency coins. Find minimum number of coins we need to use to pay exactly amount N. Suppose N is 14, then number of coins will be 5, as one 10 value coin and four 1 value coin.To solve this, we have to use these steps −If N < 10, then return N number of 1 value coinsIf N > 9 and N < 25, then divide the value with 10, and get the result, the remaining will be covered using 1 value coin, add ... Read More
730 Views
Suppose we have two integers N and M. We have to find minimum number of steps to reach M from N, by performing given operations −Multiply the number x by 2, so x will be 2*xSubtract one from the number x, so the number will be x – 1If N = 4 and M = 6, then output will be 2. So if we perform operation number 2 on N, then N becomes 3, then perform operation number one on updated value of N, so it becomes 2 * 3 = 6. So the minimum number of steps will be ... Read More
327 Views
Suppose we have a number n, and we have to find the number of diagonals for n sided convex polygon. So if the n = 5, then diagonals count will be 5.As this is n-sided convex polygon, from each vertex we can draw n – 3 diagonals leaving two sided adjacent vertices and itself. So for n vertices, it will be n*(n-3), but as we are considering twice, so it will be n(n – 3)/2.Example Live Demo#include using namespace std; int diagonalCount(int n) { return n * (n - 3) / 2; } int main() { int n = 8; cout
885 Views
Suppose we have one unsorted array A, and two numbers x and y. We have to find the minimum distance between x and y in A. The array can also contain duplicate elements. So if the array is A = [2, 5, 3, 5, 4, 4, 2, 3], x = 3 and y = 2, then the minimum distance between 3 and 2 is just 1.To solve this, we have to follow these steps, Traverse the array from left to right and stop if either x or y has found. Then store the index of that position into prevNow traverse ... Read More
55 Views
Suppose we have one array with n elements. The problem is to find longest sub-array which has exactly k odd numbers. So if A = [2, 3, 4, 11, 4, 12, 7], and k = 1, then output will be 4, sub-array is [4, 11, 4, 12]We can solve this using sliding window. The task is like below −Initialize max := 0, count := 0, and start := 0for i in range 0 to n – 1, do the followingif arr[i] mod 2 is not 0, then increase count by 1while count > k and start k && start
429 Views
Suppose we have one square matrix of order n. It has all distinct elements. So we have to find the maximum length path, such that all cells along the path are in increasing order with a difference of 1. From one cell we can move to four directions. Left, Right, Top and Bottom. So if the matrix is like −129538467So the output will be 4. As the longest path is 6→7→8→ 9To solve this problem, we will follow this idea. We will calculate longest path beginning with every cell. Once we have got the longest for all cells, we return ... Read More
73 Views
Consider one matrix is given. We have to find the number of cavities in the matrix. One element is denoted as cavity when all other elements surrounding it is greater than the element. So if the matrix is like −456715456So the output is 1.We simply check the surrounding elements and form the decision.Example#include #define MAX 100 using namespace std; int numberOfCavities(int array[][MAX], int n) { int arr[n + 2][n + 2]; int count = 0; for (int i = 0; i < n + 2; i++) { for (int j = 0; j < ... Read More
229 Views
Suppose we have two strings str1 and str2. Find the longest common prefix between them after performing zero or more operation on the second string. In each operation, we can swap any two letters. So if str1 = “HERE”, str2 = “THERE”, then output will be 4. The second string can be made “HERET” by just swapping the characters. So the longest prefix is of length 4.As we know that we can only swap on str2. And the length of the matrix should be maximized. So the idea is to traverse str1, and check if the frequency of the current ... Read More
94 Views
Suppose we have a function F(n) such that F(n) = P – (0.006*n), where P is also given. Given a list of integers and a number A. The task is to find the number from given list, for which the value of the function is nearer to A. So if P = 12, and A = 5, then list will be {1000, 2000} So output will be 1000. So if P = 12 and A = 5, then for 1000, F(1000) = 12 – (0.006 * 1000) = 6 and for 2000, F(2000) = 12 – (0.006 * 2000) = ... Read More