Programming Articles

Page 506 of 2544

Customizing termination behavior for uncaught exception In C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 383 Views

In this tutorial, we will be discussing a program to customize behavior for an uncaught exceptions in C++.Usually, the exception is handled by the try-catch block, but there are instances where there isn’t a matching catch block and the program just terminates. This terminate() function is modifiable as per user requirements.Example#include #include using namespace std; //defining custom terminator void myhandler(){    cout

Read More

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 667 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

UTF-8 Validation in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have a list integers representing the data. We have to check whether it is valid UTF-8 encoding or not. One UTF-8 character can be 1 to 4-byte long. There are some properties −For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits are all 1s, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.So the encoding technique is as follows −Character Number RangeUTF-8 octet sequence0000 0000 0000 007F0xxxxxxx0000 0080 0000 07FF110xxxxx 10xxxxxx0000 0800 0000 FFFF1110xxxx 10xxxxxx 10xxxxxx0001 0000 0010 FFFF11110xxx 10xxxxxx 10xxxxxx ...

Read More

Maximum Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 691 Views

Suppose we have an integer array. All elements in that array is unique. A maximum tree building on this array is defined as follow −The root will hold the maximum number in the array.The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number.The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.We have to construct the maximum binary tree. So if the input is like: [3, 2, 1, 6, 0, 5], then the output will be −To solve this, we will follow these ...

Read More

Ternary Expression Parser in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 669 Views

Suppose we have a string representing arbitrarily nested ternary expressions, we have to calculate the result of the expression. we can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F these few characters. (Here T and F represent True and False respectively). There are some properties −The length of the given string must be less than or equal to 10000.Each number will contain only one digit.The conditional expressions group right-to-left.The condition will always be either T or F. So the condition will never be a digit.The result of the expression ...

Read More

Robot Bounded In Circle C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 363 Views

Suppose we have an infinite plane, a robot initially stands at position (0, 0) and faces north. The robot can receive one of three instructions −G − go straight 1 unit;L − turn 90 degrees to the left direction;R − turn 90 degrees to the right direction.The robot performs the instructions given in order, Instructions are repeated forever. We have to check whether there exists a circle in the plane such that the robot never leaves the circle. So if the input is like [GGLLGG], then the answer will be true. from (0, 0) to (0, 2), it will loop ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 2K+ 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, #include ...

Read More

Rotate Function in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 392 Views

Suppose we have Given an array of integers A and let n is the length of array A. Now assume Bk to be an array obtained by rotating the array A, k positions clock-wise. Here the rotation can be defined as −F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].Now find the maximum value of F(0), F(1), ..., F(n-1).So if the input is like A = [4, 3, 2, 6], then −F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 ...

Read More

Maximum Width of Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 258 Views

Suppose we have a binary tree, we have to define a function to get the maximum width of the given tree. Here the width of a tree is the maximum width among all levels. We will consider the binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level is actually the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted for the length calculation). So if the tree is like −Then the maximum width ...

Read More

Different methods to reverse a string in C/C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 3K+ Views

In this tutorial, we will be discussing a program to understand different methods to reverse a string in C/C++.ExampleUser-defined reverse() function −#include using namespace std; //function to reverse given string void reverse_str(string& str){    int n = str.length();    for (int i = 0; i < n / 2; i++)       swap(str[i], str[n - i - 1]); } int main(){    string str = "tutorialspoint";    reverse_str(str);    cout

Read More
Showing 5051–5060 of 25,433 articles
« Prev 1 504 505 506 507 508 2544 Next »
Advertisements