Server Side Programming Articles - Page 1957 of 2650

How to sort an array of dates in C/C++?

Arnab Chakraborty
Updated on 03-Jan-2020 10:52:24

933 Views

Suppose we have an array of dates. Here we will see how to sort then using C or C++ code. The dates are stored in a class (struct can be used in C also). We will use the sort function of the C++ STL. For comparing dates, we have to write our own compare function that will be used in the sort function. Let us see the example to get better view.Example Live Demo#include #include #include using namespace std; class Date {    public:       int d, m, y; }; bool compare(const Date &date1, const Date &date2){    if ... Read More

How to Read and Print an Integer value in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:50:31

558 Views

Here we will see how to read integer from user and display in C++. To take input we will use the cin operator, and to display we will use cout operator. The syntax will be like −Input −int x; cin >> x;Output −int x = 110; cout x;    cout

Find numbers of balancing positions in string in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:46:27

148 Views

Suppose we have a string. We have to find the balancing position count in that string from where left and right part of the string contains same characters. The frequency of characters does not matter. So if the string is “ABAABA”, then the number of balancing positions is 3. These positions are AB|AABA, ABA|ABA, ABAA|BA.To solve this, we will follow some efficient approach. After traversing the string we first feel right[] with counts of all characters. Then traverse the string from left to right. For every character we increment its count in left[] and decrement the count in right. For ... Read More

Find number of pairs (x, y) in an array such that x^y > y^x in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:43:48

287 Views

Suppose we have two arrays X and Y of positive integers. Find the number of pairs such that x^y > y^x, where x is an element of X and y is an element of Y. Suppose the X = [2, 1, 6], and Y = [1, 5], then output will be 3. As there are three pairs, these are (2, 1), (2, 5) and (6, 1)We can solve this in an efficient way. The logic is simple, it will be when y > x then x^y > y^x with some exceptions. So this is the trick.Sort the array Yfor each ... Read More

Find maximum sum possible equal sum of three stacks in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:41:17

236 Views

Suppose we have three stacks of positive numbers. We have to find the possible equal maximum sum of stacks with removal of top elements allowed. The stacks are represented as an array. The first index of the array represents the top element of the stack. Suppose the stack elements are like [3, 10], [4, 5] and [2, 1]. The output will be 0. The sum can only be equal after removing all elements from all stacks.To solve this, we will follow this idea. The idea is to compare the sum of each stack, if they are not equal, then remove ... Read More

Find maximum height pyramid from the given array of objects in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:37:33

759 Views

Suppose we have an array of n objects. Each object has width W[i]. We have to arrange them in a pyramidal way like −Total width of ith is less than (i + 1)thTotal number of objects in the ith is less than (i + 1)thFor example, if the weights are like [40, 100, 20, 30], then output will be 2. So top level is 30, then lower level 20, 40 and 100To solve this, we will use the greedy approach. The idea is to use place the objects with lower width at the top, the next object at the level ... Read More

Find Maximum dot product of two arrays with insertion of 0's in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:34:38

375 Views

Suppose we have two arrays of positive integers of size m and n. The m > n. We have to maximize the dot product by inserting zeros in the second array. One thing we have to keep in mind that we will not change the ordering of the elements in the given arrays. Suppose the arrays are A = [2, 3, 1, 7, 8], and another array B = [3, 6, 7]. The output will be 107. We can maximize the dot product after inserting 0s at first and third position of the second array. So the product will be ... Read More

Find intersection point of lines inside a section in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:22:06

196 Views

Suppose we have set of lines in the form y = mx + c. There are sections made by this line and the vertical section. We have to find the intersection point present in the given section or not. Suppose the lines are like −L1 = y = x + 2L2 = y = -x + 7L3 = y = -3L4 = y = 2x - 7And the vertical section is given from x = 2 to x = 4.Here intersection points of L1 and L2 are present inside this section, so the answer will be true.To solve this problem, ... Read More

Print all valid words that are possible using Characters of Array in C++

sudhir sharma
Updated on 03-Jan-2020 10:13:33

261 Views

Ib this problem, we are given a set of words and an array of character and we have to check if the words are possible using the characters of the array.Let’s take an example to understand the problem better −Input : words[] : {‘go’ , ‘hi’ , ‘run’ , ‘on’ , ‘hog’ , ‘gone’}    Char[] : {‘a’ , ‘o’ , ‘h’ , ‘g’} Output : go , hog.Explanation − Out of the words, the words that contain the given characters are - go, hog and rest don’t include characters in the char array.To solve this problem, we will use ... Read More

Find if there is a rectangle in binary matrix with corners as 1 in C++

Arnab Chakraborty
Updated on 03-Jan-2020 10:16:32

266 Views

Suppose we have a binary matrix. We have to find if there is any rectangle or sequence in the given matrix whose all four corners are equal to 1. The matrix is like10010001010001010101The result will be yes. Here one rectangle is present, whose corners are with 1s.101010101To solve this we will use one efficient approach. We will follow these steps −Scan the matrix from top to bottom line by lineFor each line remember each combination of two 1’s and push that into a hash-set.If we ever find that combination again in the later line, we will get our rectangle.Example Live ... Read More

Advertisements