XOR of a Subarray in C++

sudhir sharma
Updated on 17-Apr-2020 12:14:06

136 Views

In this problem, we are given an arr[] and some queries that are range between L to R in the array. Our task is to print the XOR of the subarray between L to R.Let’s take an example to understand the problem, Input − array = {1, 4, 5, 7, 2, 9} L = 1 , R = 5Output −Explanation − 4^5^7^2^9To solve this problem, we will create an array, based on the following observation, We will XOR multiple bits, if there are odd number of 1s, the result will be 1 otherwise the result is 0.Now, we will create ... Read More

Log1p in C++ Program

Sunidhi Bansal
Updated on 17-Apr-2020 12:07:18

124 Views

We are given with a variable of any type and the task is to find the result using the function log1p(). log1p() is an analytical function that takes an argument ‘a’ and also has a return value.Syntaxdouble log1p (double x); Where x ranges between [-1, ?] float log1p (float x);Return Type − This function returns a non-zero value if the argument is greater than -1 else it will return a non-numeric value.ExampleInputa = 20.34Output3.06058Inputa = 0.0Output0Example Live Demo#include #include using namespace std; int main(){    double ans = 20.34;    double temp;    temp = log1p(ans);    cout

mbrtowc Function in C/C++ Program

Sunidhi Bansal
Updated on 17-Apr-2020 12:04:58

189 Views

In this article we will be discussing the working, syntax and examples of std::mbrtowc() function in C++ STL.What is std::mbrtowc()?std::mbrtowc() function is an inbuilt function in C++ STL, which is defined in the header file. mbrtowc() means that it converts the narrow multibyte character string to wide character. This function is used to convert a narrow multibyte character to wide character representation.Syntaxsize_t mbrtowc( wchar_t* pwc, char* str, size_t n, mbstate_t* ps);ParametersThe function accepts following parameter(s) −pwc − This is the pointer to the location we want the output to be stored.str − Character string which is used as the ... Read More

Z Algorithm: Linear Time Pattern Searching Algorithm in C++

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

1K+ Views

Z algorithm is used to find the occurrence of a pattern in a string in linear time. Suppose if the length of the string is n and the size of the pattern to be searched is m, the time taken to solve will be of the order O(m+n).The z-algorithm uses a Z array to find the occurrence of a pattern.Z arrayIt is an array of the same length as the string. Each element of the z-array consists of the length of the longest substring of the string starting from I which can be used as a prefix of the string ... Read More

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

995 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

273 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

631 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 of M Elements in C++

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

280 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

Advertisements