Articles on Trending Technologies

Technical articles with clear explanations and examples

Program to find number of good leaf nodes pairs using Python

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

Suppose we have a binary tree. and another value distance d. A pair of two different leaf nodes are said to be good, when the shortest path between these two nodes is smaller or same as distance d.So, if the input is likeAnd distance d = 4, then the output will be 2 because the pairs are (8, 7) and (5, 6) as their path length distance is 2, but (7, 5) or (8, 6) or other pairs are not good as their path length is 5 which is larger than d = 4To solve this, we will follow these ...

Read More

Largest K digit number divisible by X in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 220 Views

In this tutorial, we are going to write a program that finds the largest k-digit number that is divisible by x.Let's see the steps to solve the problem.Initialise the x and k.Find the value of pow(10, k) - 1 which is largest k-digit number.Now, remove the remainder value from the above value to get the largest k-digit number that is divisible by x.ExampleLet's see the code.#include using namespace std; int answer(int x, int k) {    int max = pow(10, k) - 1;    return max - (max % x); } int main() {    int x = 45, k = 7;    cout

Read More

Python Program to Read Height in Centimeters and convert the Height to Feet and Inches

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 4K+ Views

When it is required to read the height in ‘cm’ and convert it into ‘feet’ and ‘inches’, the ‘round’ method can be used.Below is a demonstration of the same −Examplein_cm=int(input("Enter the height in centimeters...")) in_inches=0.394*in_cm in_feet=0.0328*in_cm print("The length in inches is ") print(round(in_inches, 2)) print("The length in feet is") print(round(in_feet, 2))OutputEnter the height in centimeters...178 The length in inches is 70.13 The length in feet is 5.84ExplanationThe input is taken by user, as ‘cm’.It can be converted into inches by multiplying it with 0.394.This is assigned to a variable.It can be converted into feet by multiplying it with 0.0328.This is ...

Read More

Finding nth element of the Padovan sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 277 Views

Padovan SequenceThe Padovan sequence is the sequence of integers P(n) defined by the initial values −P(0) = P(1) = P(2) = 1and the recurrence relation,P(n) = P(n-2) + P(n-3)The first few values of P(n) are1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, …ProblemWe are required to write a JavaScript function that takes in a number n and return the nth term of the Padovan sequence.ExampleFollowing is the code −const num = 32; const padovan = (num = 1) => {    let secondPrev = 1, pPrev = 1, pCurr = 1, pNext = 1;    for (let i = 3; i

Read More

Program to find out the index in an array where the largest element is situated in Python

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

Suppose, we are given a class called 'TestArray' that contains an array that is not accessible by other classes and two public member functions length() and compare(). The function length() returns the length of the array and the function compare() returns three different values comparing various subarrays from the array. The function takes four values l, r, x, y as input and works like this −if (array[l] + array[l+1]+......+array[r-1]+array[r]) > (array[x] + array[x+1]+......+array[y1]+array[y]); it returns 1if (array[l] + array[l+1]+......+array[r-1]+array[r]) = (array[x] + array[x+1]+......+array[y1]+array[y]); it returns 0if (array[l] + array[l+1]+......+array[r-1]+array[r]) < (array[x] + array[x+1]+......+array[y1]+array[y]); it returns -1We have to find out ...

Read More

Largest N digit number divisible by given three numbers in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 339 Views

In this tutorial, we are going to write a program that finds the largest n-digit number that is divisible by the given three numbers.Let's see the steps to solve the problem.Initialise three numbers along with n.Find the LCM of three numbers.Store the largest number with n-digits.If the largest number is divisible by n, then return it.Else check for the number obtained from subtracting remainder in the above step.ExampleLet's see the code.#include using namespace std; int LCM(int x, int y, int z) {    int ans = ((x * y) / (__gcd(x, y)));    return ((z * ans) / (__gcd(ans, ...

Read More

Matrix creation of n*n in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 713 Views

When it is required to create a matrix of dimension n * n, a list comprehension is used.Below is a demonstration of the same −ExampleN = 4 print("The value of N is ") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1)))    for i in range(N)] print("The matrix of dimension N * 0 is :") print(my_result)OutputThe value of N is 4 The matrix of dimension N * 0 is : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]ExplanationThe value of N is predefined.It ...

Read More

Mean of an array rounded down to nearest integer in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 401 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function is supposed to return the average of the given array rounded down to its nearest integer.ExampleFollowing is the code −const arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => {    const { sum, count } = arr.reduce((acc, val) => {       let { sum, count } = acc;       count++;       sum += val;       return { sum, count };    }, {       sum: 0, count: 0    });    const mean = sum / (count || 1);    return Math.round(mean); }; console.log(roundedMean(arr));OutputFollowing is the console output −53

Read More

Program to find out the index of the most frequent element in a concealed array in Python

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

Suppose, we are given a class called 'TestArray' that contains an private array which can only contain values 0 or 1; and two public member functions length() and query(). The function length() returns the length of the array and the function query() returns three different values comparing various values in the array. The function takes four values p, q, r, s as input and works like this −if all the four values in the given indexes of the array are either 0 or 1, it returns 4.else if any three values in the given indexes of the array are the ...

Read More

Program to find minimum swaps to arrange a binary grid using Python

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

Suppose we have a n x n binary matrix. We can perform an operation on it like, at one step we select two adjacent rows and swap them. We have to count number of minimum swaps required, so that all nodes above the major diagonal of the matrix is 0. If there is no such solution, then return -1.So, if the input is like010011100then the output will be 2 because −To solve this, we will follow these steps:n := row count of matrixm := make an array of size n and fill with nfor i in range 0 to n ...

Read More
Showing 941–950 of 61,248 articles
« Prev 1 93 94 95 96 97 6125 Next »
Advertisements