Found 7197 Articles for C++

Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:11:28

347 Views

Suppose we have an array of n elements. We have to find the maximum number of elements to select from the array, such that the absolute difference between any two of the chosen elements is less than or equal to 1. So if the array is like [2, 2, 3, 4, 5], then the element will be 3, so the sequence with maximum count is 2, 2, 3.The absolute difference of 0 and 1 means, that the number can be of type x and x + 1. So the idea is to store the frequencies of array elements. So if ... Read More

Sort in C++ Standard Template Library (STL)

Arnab Chakraborty
Updated on 18-Dec-2019 10:09:23

698 Views

Here we will see how to use the sort() function of C++ STL to sort an array So if the array is like A = [52, 14, 85, 63, 99, 54, 21], then the output will be [14 21 52 54 63 85 99]. To sort we will use the sort() function, that is present in the header file . The code is like below −Example Live Demo#include #include using namespace std; int main() {    int arr[] = {52, 14, 85, 63, 99, 54, 21};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Find maximum in a stack in O(1) time and O(1) extra space in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:07:51

601 Views

Suppose we want to make a stack that can store the maximum element in the stack. And we can get it in O(1) time. The constraint is that, it should use O(1) extra space.We can make one user-defined stack, that will store the max value, when one operation is performed, like pop or peek, then the max will be returned. For peek operation, return the maximum of stack top and the max element, for pop operation, when the top element is larger, then print it and update max as 2*max – top_element. otherwise return top_element. For push operation update the ... Read More

Complex numbers in C++ programming

Arnab Chakraborty
Updated on 18-Dec-2019 10:06:32

1K+ Views

In this section we will see how to create and use complex numbers in C++. We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class.In this example we are creating one complex type class, a function to display the complex number into correct format. Two additional methods to add and subtract two complex numbers etc.Example Live Demo#include using namespace std; class complex{    int real, img;    public:       complex(){       ... Read More

Some useful C++ tricks for beginners in Competitive Programming

Arnab Chakraborty
Updated on 18-Dec-2019 10:02:39

398 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

Find length of the longest consecutive path from a given starting characters in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:01:27

168 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

Comparing two strings in C++

Arnab Chakraborty
Updated on 18-Dec-2019 09:56:31

445 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

Find length of longest subsequence of one string which is substring of another string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 09:40:29

2K+ 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

Find Largest Special Prime which is less than or equal to a given number in C++

Arnab Chakraborty
Updated on 18-Dec-2019 09:36:11

459 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

Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array in C++

Arnab Chakraborty
Updated on 18-Dec-2019 09:27:13

248 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

Advertisements