Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
Program to find number of good leaf nodes pairs using Python
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 MoreLargest K digit number divisible by X in C++
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 MorePython Program to Read Height in Centimeters and convert the Height to Feet and Inches
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 MoreFinding nth element of the Padovan sequence using JavaScript
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 MoreProgram to find out the index in an array where the largest element is situated in Python
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 MoreLargest N digit number divisible by given three numbers in C++
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 MoreMatrix creation of n*n in Python
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 MoreMean of an array rounded down to nearest integer in JavaScript
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 MoreProgram to find out the index of the most frequent element in a concealed array in Python
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 MoreProgram to find minimum swaps to arrange a binary grid using Python
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