Programming Articles

Page 217 of 2545

Count the number of pop operations on stack to get each element of the array in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 777 Views

Given an array of numbers and a stack. All the elements of the array are present inside the stack The goal is to find the count of pop operations required for getting individual array elements.The stack is filled in decreasing order, the first element is highest and top element is lowest.For ExampleInputStack [ 7, 6, 2, 1 ] array : 2, 1, 6, 7OutputCount of number of pop operations on stack to get each element of the array are: 3 1 0 0ExplanationTraversing array from 0th index, To get 2 we will pop stack three times. So arr[0] is 3. ...

Read More

Check if given string can be split into four distinct strings in Python

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

Suppose we have a string s, we have to check whether we can split it into four sub-strings such that each of them are non-empty and unique.So, if the input is like s = "helloworld", then the output will be True as one of the possible set of sub-strings are ["hel", "lo", "wor", "ld"]To solve this, we will follow these steps −if size of s >= 10, thenreturn Truefor i in range 1 to size of s - 1, dofor j in range i + 1 to size of s - 1, dofor k in range j + 1 to ...

Read More

Find k-th smallest element in given n ranges in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 396 Views

In this problem, we are given n ranges and an integer k. Our task is to find k-th smallest element in given n ranges. We need to find the kth smallest elements from the array which is created after combining the ranges.Let’s take an example to understand the problem,  Input: ranges = {{2, 5}, {7, 9}, {12, 15}}, k = 9Output: 13Explanation: The array created is {2, 3, 4, 5, 7, 8, 9, 12, 13, 14, 15}The smallest elements is 13Solution Approach:A simple solution to the problem is by creating the array from all ranges and as it is created from range it is ...

Read More

Count of distinct rectangles inscribed in an equilateral triangle in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 268 Views

We are an equilateral triangle with side length. The goal is to count the number of distinct rectangles that can be present inside the triangle such that horizontal sides of the rectangle are parallel to the base. Also all end points of the rectangle touch the dots as shown.Let us understand with examplesInput − sides=3Output − Count of distinct rectangles inscribed in an equilateral triangle are − 1Explanation − The figure above shows the rectangle.Input − sides=10Output − Count of distinct rectangles inscribed in an equilateral triangle are − 200Approach used in the below program is as followsAs from the ...

Read More

Program to Find Out the Number of Moves to Reach the Finish Line in Python

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

Suppose, we have a car and are driving it on a one−dimensional road. Currently we are at position = 0 and with speed = 1. We can perform any of these two operations.Acceleration: position := position + speed and speed := speed * 2 Reverse Gear: speed := −1 when speed > 0 otherwise speed := 1.We have to find the number of moves needed at least to reach the target.So, if the input is like target = 10, then the output will be 7.To solve this, we will follow these steps −Define a function dfs() . This will take ...

Read More

Count of all possible values of X such that A % X = B in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 274 Views

Given two integers A and B and a number X. The goal is to find the count of values that X can have so that A%X=B. For the above equation if, A==B then infinite values of X are possible, so return −1. If A < B then there would be no solution so return 0. If A>B then return count of divisors of (AB) as result.For ExampleInputA=5, B=2OutputCount of all possible values of X such that A % X = B are: 1Explanation5%3=2. So X is 3 here.InputA=10, B=10OutputCount of all possible values of X such that A % X ...

Read More

Find kth node from Middle towards Head of a Linked List in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 510 Views

In this problem, we are given a linked-list and a number k. Our task is to find kth node from Middle towards the Head of a Linked List. Let’s take an example to understand the problem,  Input: linked-list : 4 -> 2 -> 7 -> 1 -> 9 -> 12 -> 8 -> 10 -> 5, k = 2Output: 7Explanation: Middle node value is 9.The 2nd node from middle towards head is 7.Solution ApproachWe need to find kth element from the middle of the linked-list towards the beginning. For this we need to find the size of linked-list by traversing the linked-list from beginning ...

Read More

Program to Reverse a Substring Enclosed Within Brackets in Python

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

Suppose, we have a lowercase string s that contains letters and parentheses "(" and ")". We have to reverse every string enclosed within parentheses in a recursive manner and return the resultant string.So, if the input is like s = "back(aps)ce", then the output will be “backspace”.To solve this, we will follow these steps −Define a function trav() . This will take s, dir, start, close:= close, ans:= ansend := "(" if dir is same as −1, otherwise ")"other := "(" if end is same as ")", otherwise ")"while start < size of s, and s[start] is not same as ...

Read More

Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 191 Views

Given a number N as input. The goal is to find the count of all N digit numbers that have sum Count of all N digit numbers such that num + Rev(num) = 10N − 1num+rev(num)=10N−1For ExampleInputN=4OutputCount of all N digit numbers such that num + Rev(num) = 10N − 1 are − 90ExplanationThe numbers would be − 1. 1188 + 8811 = 9999 2. 2277 + 7722 = 9999 3. 1278 + 8721 = 9999 ……...total 90 numbersInputN=5OutputCount of all N digit numbers such that num + Rev(num) = 10N − 1 are − 0ExplanationAs N is odd, ...

Read More

Check if is possible to get given sum from a given set of elements in Python

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

Suppose we have an array called nums and another value sum. We have to check whether it is possible to get sum by adding elements present in nums, we may pick a single element multiple times.So, if the input is like nums = [2, 3, 5] sum = 28, then the output will be True as we can get 26 by using 5 + 5 + 5 + 5 + 3 + 3 + 2To solve this, we will follow these steps −MAX := 1000table := an array of size MAX ad fill with 0Define a function util() . This ...

Read More
Showing 2161–2170 of 25,445 articles
« Prev 1 215 216 217 218 219 2545 Next »
Advertisements