Found 26504 Articles for Server Side Programming

Sentence Screen Fitting in C++

Arnab Chakraborty
Updated on 29-Apr-2020 15:09:43

207 Views

Suppose we have a rows x cols screen and a sentence represented by a list of non-empty words, so we have to find how many times the given sentence can be fitted on the screen. There are certain properties −A word will not be split into two lines.The order of words in the sentence must not be changed.There will be only one space between two words.The total number of words in the sentence won't exceed 100.The length of each word is greater than 0 but less than 10.1 ≤ rows, cols ≤ 20, 000.So if the input is like rows ... Read More

Remove K Digits in C++ program

Arnab Chakraborty
Updated on 29-Apr-2020 15:03:15

309 Views

Suppose we have a sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. So for example, these are arithmetic sequence: [1, 3, 5, 7, 9], [7, 7, 7, 7], [3, -1, -5, -9], But the following sequence is not arithmetic. [1, 1, 2, 5, 7]Now a zero-indexed array A consisting of N numbers is given. A slice of that given array is any pair of integers (P, Q) such that 0

Remove K Digits in C++

Arnab Chakraborty
Updated on 31-Mar-2020 08:38:57

737 Views

Suppose we have a non-negative integer num that is represented as a string, we have to remove k digits from the number so that the new number is the smallest possible. So if the input is like “1432219” and k = 3, then the result will be “1219”.To solve this, we will follow these steps −Define a stack st, create an empty string retn := size of numfor i in range 0 to n – 1while k is non zero and stack is not empty and top of stack > num[i]delete from the stack and decrease k by 1insert num[i] ... Read More

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

192 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

616 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

246 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

664 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

Advertisements