Found 26504 Articles for Server Side Programming

Zigzag (or diagonal) traversal of Matrix in C++

sudhir sharma
Updated on 17-Apr-2020 11:11:01

1K+ Views

In this problem, we are given a 2D matrix. Our task is to print all the elements of the matric in a diagonal order.Let’s take an example to understand the problem, 1    2    3 4    5    6 7    8    9Output −1 4    2 7    5    3 8    6 9Let’s see the pattern that is followed while printing the matrix in a zigzag form or diagonal form.This is the way diagonal traversal works.The number of lines in output is always dependent on the row and columns of the 2D matrix.For a ... Read More

ZigZag Tree Traversal in C++

sudhir sharma
Updated on 17-Apr-2020 11:05:47

635 Views

In this problem, we are given a binary tree. Our task is to print the binary tree in a zigzag form.Let’s take an example to understand the problem, The zigzag traversal of the above binary tree is3    5    1    8    7    0    4To solve this problem, we need to traverse the binary tree level by level. The order of traversal will be flipped after each level.Now, we will use two stacks(current and next) and one value for order. First, we will traverse the node from current and feed nodes from the left child to ... Read More

Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++

sudhir sharma
Updated on 17-Apr-2020 11:03:02

283 Views

In this problem, we are given array arr[] and two integers M and K. our task is to create an Array using elements of the given array. The size of the new array should M and any sub-array of size greater than K cannot have all elements the same. we have to print the maximum sum possible by the created array.Let’s take an example to understand the problemInput − arr[] = {1, 2, 4, 5, 7 }, M = 5, K = 2Explanation − array created that satisfies the condition {7, 7, 5, 7, 7}. Here, no sub-array with size ... Read More

Pick points from array such that minimum distance is maximized in C++

sudhir sharma
Updated on 17-Apr-2020 11:00:07

232 Views

In this problem, we are given an array arr[] of n elements that represent N index positions and there are C magnets. Our task is to print all these magnets in such a way that the distance between the two nearest magnets is as large as possible.Let’s take an example to understand the problem, Input − array = { 1, 4, 6, 12, 28, 44 } C = 4Output − 11To solve this problem, we will use a binary search to maximum distance. We will fix a maximum distance and then placing all magnets between 0 to maximum distance is ... Read More

Pierpont Prime in C++

sudhir sharma
Updated on 17-Apr-2020 10:57:26

159 Views

In this problem, we are given a number n. Our task is to print all Pierpont prime numbers less than n.Pierpont Prime number is a special type of prime number that is of the form, p= 2i . 3k + 1.Where p is a prime number, and i and k are some integers.Let’s take an example to understand the problem, Input − n = 50Output − 2, 3, 5, 7, 13, 17, 19, 37To solve this problem, we have to find all the prime numbers that follow the condition. For this, we will find a number with factors of powers ... Read More

Pipes and Cisterns in C++

sudhir sharma
Updated on 17-Apr-2020 10:52:55

204 Views

Pipes and cisterns problem is a very common problem and is generally included in competitive exams. So, learning questions related to pipers and cisterns is important and you should know how to solve them as these are not too difficult to learn.Pipes and cisternsThese problems involve pipes that are used to either fill or empty a tank/reservoir/cistern.Here, are some basics of pipes and cisterns problem, The pipes are inlet pipes or outlet pipes. Inlet pipe fills the tank and the outlet pipe empties the tank.If a pipe fills/empties in ‘n’ hours and the capacity of the tank is ‘c’ liters. ... Read More

Place k elements such that minimum distance is maximized in C++

sudhir sharma
Updated on 17-Apr-2020 10:45:21

1K+ Views

In this problem, we are given an array of n points that lie on the same line. Our task is to place k elements of the array in such a way that the minimum distance between them is maximized.Let’s take an example to understand the problem, Input − array = {}Output −To solve this problem, we will find have to find the maximum possible minimum distance. For such a problem first, we need to sort the given array and then do a binary search until we get the solution at mid.ExampleProgram to show the implementation of our solution,  Live Demo#include ... Read More

Place K-knights such that they do not attack each other in C++

sudhir sharma
Updated on 17-Apr-2020 10:43:06

580 Views

In this problem, we are given three integer value K, N, M. our task is to place K knights in an NxM chessboard such that no two knights attack each other. There can be cases with 0 valid ways and also cases with multiple valid ways. You need to print all valid cases.Knight is a chess piece that moves two moves ahead and then one move to the left of right. It can move in any direction in the chessboard.Attack is the position when one piece can be in the same place as other pieces in one chance of its ... Read More

Place N^2 numbers in matrix such that every row has an equal sum in C++

sudhir sharma
Updated on 17-Apr-2020 10:39:25

188 Views

In this problem, we are given an integer value N. our task is to print numbers within the range (1, N2) in a 2D matrix of size NxN in such a way that the sum elements of each row are equal.Let’s take an example to understand the problem, Input − N = 4Output −1 6 11 16 2 7 12 13 3 8  9 14 4 5 10 15Sum of elements in each row is 34To solve this method, we need to place each element in the matrix in such a way that the total in each row is equal. ... Read More

Playing with Destructors in C++

sudhir sharma
Updated on 17-Apr-2020 10:35:34

978 Views

Destructor is a function of a class in c++ that does the job of deleting the object of a class.Calling a destructorDestructor is called when the object of a class goes out of the scope in the program. The cases when object goes out of scope, The program goes out of the scope of a function.The program ends.The block initializing local variables of object goes out of scope.When the operator of the object is deleted.ExampleLet’s see a code and guess the output of the program,  Live Demo#include using namespace std; int i; class destructor {    public:     ... Read More

Advertisements