Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by sudhir sharma
Page 52 of 98
Place N^2 numbers in matrix such that every row has an equal sum in C++
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 MorePlace K-knights such that they do not attack each other in C++
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 MorePlace k elements such that minimum distance is maximized in C++
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, #include ...
Read MorePierpont Prime in C++
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 MorePick maximum sum M elements such that contiguous repetitions do not exceed K in C++
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 MoreZig Zag Level order traversal of a tree using single queue in C++
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 MoreZero Initialization in C++
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 MoreXOR of all elements of array with set bits equal to K in C++
In this problem, we are given an array of n elements and an integer value k. Our task is to find XOR of all elements of the array that have set bits equal to k.Let’s take an example to understand the problem, Inputarray = {2, 12, 44, 103, 17} , K =3Output44To solve this problem, we will count set bit of every element of the array and compare it with k. If the number of set bits is equal to k, then we will push it to a vector and find XOR of all elements of the vector.For finding the ...
Read MoreXOR of a submatrix queries in C++
In this problem, we are given a N x N matrix and some queries, each query contains the top-left and bottom-right corner of the submatrix created from this matrix. Our task is to find the XOR of all elements of the submatrix defined by the querries.Let’s take an example to understand the problem, Inputarr[][] = {{1, 2, 3} {4, 5, 6} {7, 8, 9}} Querries: {0, 0, 1, 2} , {1, 2, 2, 2}Output1 15Explainationquerry 1 : 1^2^3^4^5^6 querry 2 : 6^9To solve this problem, we will find a prefix-XOR matrix to solve queries. The value of the matrix at ...
Read MoreXOR of a subarray in C++
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