Programming Articles - Page 1993 of 3366

Z-Buffer or Depth-Buffer method in C++

sudhir sharma
Updated on 17-Apr-2020 11:20:45

2K+ Views

The z-buffer also known as depth-buffer is a method that is used for hidden surface detection.Hidden surface detectionFor any picture that has objects and surfaces that are transparent. In this case, objects that are behind other objects are hidden. For proper visual of the image, we need to remove these hidden surfaces is required. The identification is called hidden surface problem.In z-buffer, we will compare surfaces in the z-axis as depths.AlgorithmStep 1: initialize the depth of all pixel max.    d(i, j) = infinity Step 2: Initialize color for all pixels.    c(i, j) = background-color Step 3: for each ... Read More

Zero Initialization in C++

sudhir sharma
Updated on 17-Apr-2020 11:18:30

1K+ Views

Zero initialization is setting the initial value of an object in c++ to zero.SyntaxT{} ; char array [n] = “”;The situations in which zero initialization are performed are −Named variable with static or thread-local storage is initialized to zero.It is used as initialization of values for non-class types and members of a class that do not have a constructor.It is used to initialize a character array when its length is greater than the number of characters that are to be assigned.Points to rememberSome types of variables like static variables and thread-local variables are first initialized to zero then reinitialized to ... Read More

Zig Zag Level order traversal of a tree using single queue in C++

sudhir sharma
Updated on 17-Apr-2020 11:15:53

296 Views

In this problem, we are given a binary tree. Our task is to print the zigzag level order traversal of the tree. For this traversal, we will use a single queue only.Let’s take an example to understand the problem, Output −3    1    7    2    8    9    5To solve this problem using a single queue, we will sue an extra separation flag along with the queue and direction flag.Now, we will traverse the tree level by level, insert root element, now insert for every element of the queue insert its child node to the queue. ... Read More

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

652 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

295 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

239 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

162 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

209 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

Advertisements