Server Side Programming Articles - Page 1848 of 2650

Nth Digit in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:55:52

1K+ Views

Suppose we have one infinite integer sequence, we have to find the nth digit of this sequence. So if the input is 11, then the output will be 0 as if we place the numbers like 123456789101112, so the 11th digit is 0.To solve this, we will follow these steps −len := 0 and cnt := 9 and start := 1while n > len * cntn := n – (len * cnt)cnt := cnt * 10, start := start * 10increase len by 1start := start +(n - 1) / lens := start as stringreturn s[(n – 1) mod len]Example ... Read More

Reverse Words in a String in C++

Ravi Ranjan
Updated on 18-Jun-2025 19:19:54

6K+ Views

In this article, we have a string. Our task is to reverse the words in the given string. Here is an example of reversing the words of a string: Example The example below reverses the words of the given string: Input: "I am Viper" Output: Viper am I Here are the approaches to reverse the words of a string: Using Two Pointer Approach Using STL reverse() Function Using stringstream with Vector Using Recursive Approach ... Read More

Convert Sorted List to Binary Search Tree in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:16:42

205 Views

Suppose we have a singly linked list where elements are sorted in ascending order, we have to convert it to a height balanced BST. So if the list is like [-10, -3, 0, 5, 9], The possible tree will be like −To solve this, we will follow these steps −If the list is empty, then return nullDefine a recursive method called sortedListToBST() this will take list start nodex := address of the previous node of mid node from list amid := exact mid nodecreate a new node with value by taking from the value of midnextStart := next of mid ... Read More

Four Divisors in C++

Ravi Ranjan
Updated on 17-Jun-2025 17:59:02

630 Views

In this article, we will understand the four divisors problem. We have an array of integers, and our task is to find the sum of divisors of the array elements that have exactly four divisors. If there is no such integer in the array, then return 0. Here is a detailed example given below: Example The following example implements four divisors sum problem: Input: arr = [5, 10, 15, 8] Output: 57 The explanation of the above example is as follows: Divisors of 5 = (1, 5) => count != ... Read More

Sort Integers by The Power Value in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:08:54

252 Views

As we know that the power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps −if x is even then x = x / 2if x is odd then x = 3 * x + 1So for example, the power of x = 3 is 7 because 3 uses 7 steps to become 1 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1). So if we have some integers lo, hi and k. We have to sort all integers in the interval ... Read More

Balance a Binary Search Tree in c++

Ravi Ranjan
Updated on 19-Aug-2025 17:22:10

3K+ Views

In this article, we have a binary search tree and our task is to balance the given binary search tree. A binary search tree is a tree data structure and a special type of binary tree that follows the conditions given below: The left child node's value is always less than the parent node. The right child node has a greater value than the parent node. All the nodes individually form a binary search tree. The inorder traversal of nodes in BST are ... Read More

Design a Stack With Increment Operation in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:51:06

684 Views

Suppose we want to design a stack that supports the following operations.CustomStack(int maxSize) This initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.void push(int x) This inserts x to the top of the stack if the stack hasn't reached the maxSize.int pop() This deletes and returns the top of stack or -1 if the stack is empty.void inc(int k, int val) This increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all ... Read More

Find a Corresponding Node of a Binary Tree in a Clone of That Tree in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:44:49

656 Views

Suppose we have two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is actually a copy of the original tree. We have to find a reference to the same node in the cloned tree.So if the tree is like below and the target is 3, then the output will be 3.To solve this, we will follow these steps −Define a method called solve(), this will take the node1m node2 and targetif node1 is null, then return nullif node1 is target and value of node 1 is the value ... Read More

Time Needed to Inform All Employees in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:37:56

467 Views

Suppose we have a company has n employees with a unique ID for each employee. These IDs are ranging from 0 to n - 1. The head of the company has is the one with headID. Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it's guaranteed that the subordination relationships have a tree-like structure. Here the head of the company wants to inform all the employees of the company of an urgent piece of news. He can inform his direct subordinates and they ... Read More

Bulb Switcher III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:21:40

447 Views

Suppose we have a room with n bulbs, these are numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k in range 0 to n - 1), we turn on the light[k] bulb. A bulb changes color to blue only if it is on and all the previous bulbs (to the left) are turned on too. We have to find the number of moments in which all turned on bulbs is blue. So this is an example −The output will be 3 as the moments ... Read More

Advertisements