- 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++
282 Views
Here we will see some good tricks of C++ programming language that can help us in different area. Like if we want to participate in some competitive programming events, then these tricks will help us to reduce the time for writing codes. Let us see some of these examples one by one.Checking whether a number is odd or even without using % operator. This trick is simple. We can perform bitwise AND operation with the number and 1. If the result is non-zero then this is odd, otherwise this is even. The logic is too simple. All odd numbers have ... Read More
108 Views
A matrix of different characters is given. Starting from one character we have to find longest path by traversing all characters which are greater than the current character. The characters are consecutive to each other.Starts from E.To find longest path, we will use the Depth First Search algorithm. During DFS, some sub problems may arise multiple times. To avoid the computation of that sub problems again and again, we will use dynamic programming approach.Example Live Demo#include #define ROW 3 #define COL 3 using namespace std; // tool matrices to recur for adjacent cells. int x[] = {0, 1, 1, -1, 1, ... Read More
301 Views
Here we will see how to compare two strings in C++. The C++ has string class. It also has the compare() function in the standard library to compare strings. This function checks the string characters one by one, if some mismatches are there, it returns the non-zero values. Let us see the code to get better idea.Example Live Demo#include using namespace std; void compareStrings(string s1, string s2) { int compare = s1.compare(s2); if (compare != 0) cout
982 Views
Suppose, we have two strings X and Y, and we have to find the length of longest subsequence of string X, which is substring in sequence Y. So if X = “ABCD” and Y = “BACDBDCD”, then output will be 3. As “ACD” is the longest sub-sequence of X, which is substring of Y.Here we will use the dynamic programming approach to solve this problem. So if the length of X is n, and length of Y is m, then create DP array of order (m+1)x(n+1). Value of DP[i, j] is maximum length of subsequence of X[0…j], which is substring ... Read More
335 Views
Suppose we have a number n. We have to find the largest special prime which is less than or equal to N. The special prime is a number, which can be created by placing digits one after another, so all the resultant numbers are prime.Here we will use Sieve Of Eratosthenes. We will create the sieve array up to the number n. Then start iteratively back from the number N, by checking if the number is prime. When this is prime, check whether this is special prime or not.Example Live Demo#include using namespace std; bool isSpecialPrime(bool sieve[], int num) { ... Read More
156 Views
Suppose, we have an array of N elements. These elements are either 0 or 1. Find the position of 0 to be replaced with 1 to get longest contiguous sequence of 1s. Suppose the array is like arr = [1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1], the output index is 9. Replacing 0 with 1 at index 9 cause the maximum contiguous sequence of 1sWe have to keep track of three indexes. current index(curr), previous zero index(pz), and previous to previous zero index (ppz). Now traverse the array when array element is 0, then ... Read More
54 Views
Suppose we have n points on x-axis and the list of allowed translation between the points. Find if it is possible to reach the end from starting point through these transactions only. So if there is a translation between points x1 and x2, then we can move from point x to any intermediate points between x1 and x2, or directly to x2. So if n = 5. And transactions are 0 to 2, 2 to 4, and 3 to 5. Then output will be YES. There is a path from 0→2→3→5.We have to sort the list according to the first ... Read More
70 Views
Suppose we have two binary trees. We have to find first leaf of two trees, that does not match. If there are no non-matching leaves, then display nothing.If these are two trees, then the first non-matching leaves are 11 and 15.Here we will use the iterative preorder traversal of both of the trees simultaneously using stack. We will use different stack for different trees. We will push nodes into the stack till the top node is the leaf node. Compare two top, if they are same, then do further checking, otherwise show two stack top elements.Example Live Demo#include #include ... Read More
96 Views
Suppose we have one sorted array with n elements. The array is sorted. We have to find whether an element exists in an array from where the number of smaller element is same as the number of larger elements. If the equal point appears multiple times in the array, return the index of first occurrence. If no such point is present, then return -1. Suppose the elements are like A = [1, 1, 2, 3, 3, 3, 3, 3], then the equal point is at index 2, the element is A[2] = 2. As it has only one smaller element ... Read More
207 Views
Consider we have the selling price, and percentage of profit or loss is given. We have to find the cost price of the product. The formula is like below −$$Cost \: Price = \frac{Sell Price * 100}{100 + Percentage \: Profit}$$$$Cost \: Price = \frac{Sell price *100}{100 + percentage\:loss}$$Example Live Demo#include using namespace std; float priceWhenProfit(int sellPrice, int profit) { return (sellPrice * 100.0) / (100 + profit); } float priceWhenLoss(int sellPrice, int loss) { return (sellPrice * 100.0) / (100 - loss); } int main() { int SP, profit, loss; SP = 1020; profit = 20; cout