Articles on Trending Technologies

Technical articles with clear explanations and examples

Find distance from root to given node in a binary tree in C++

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

Consider we have a binary tree with few nodes. We have to find the distance between the root and another node u. suppose the tree is like below:Now the distance between (root, 6) = 2, path length is 2, distance between (root, 8) = 3 etc.To solve this problem, we will use a recursive approach to search the node at the left and right subtrees, and also update the lengths for each level.Example#include using namespace std; class Node {    public:       int data;    Node *left, *right; }; Node* getNode(int data) {    Node* node = new ...

Read More

Find the Product of first N Prime Numbers in C++

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

Suppose we have a number n. We have to find the product of prime numbers between 1 to n. So if n = 7, then output will be 210, as 2 * 3 * 5 * 7 = 210.We will use the Sieve of Eratosthenes method to find all primes. Then calculate the product of them.Example#include using namespace std; long PrimeProds(int n) {    bool prime[n + 1];    for(int i = 0; i

Read More

Accessing Array Elements in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 735 Views

When accessing individual elements from an array in Perl, you must prefix the variable with a dollar sign ($) and then append the element index within the square brackets after the name of the variable. For example −Example#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; print "$days[0]"; print "$days[1]"; print "$days[2]"; print "$days[6]"; print "$days[-1]"; print "$days[-7]";OutputThis will produce the following result −Mon Tue Wed Sun Sun MonArray indices start from zero, so to access the first element you need to give 0 as indices. You can also give a negative index, in which case you select the ...

Read More

Find the value of ln(N!) using Recursion using C++.

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

Suppose we have a number N, our task is to find ln(N!) using recursion. ln() is basically log base e. To solve this we can use this formula −$$\ln\lgroup N!\rgroup=\ln\lgroup N*\lgroup N-1\rgroup *\lgroup N-2\rgroup *\dotsm*2*1\rgroup=\ln\lgroup N\rgroup+\ln\lgroup N+1\rgroup+\dotsm+\ln\lgroup 1\rgroup$$Example#include #include using namespace std; double factLog(int n) {    if (n

Read More

Perl Sequential Number Arrays

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 866 Views

Perl offers a shortcut for sequential numbers and letters. Rather than typing out each element when counting to 100 for example, we can do something like as follows −Example#!/usr/bin/perl @var_10 = (1..10); @var_20 = (10..20); @var_abc = (a..z); print "@var_10"; # Prints number from 1 to 10 print "@var_20"; # Prints number from 10 to 20 print "@var_abc"; # Prints number from a to zHere double dot (..) is called range operator. This will produce the following result −1 2 3 4 5 6 7 8 9 10 10 11 12 13 14 15 16 17 18 19 20 a b c d e f g h i j k l m n o p q r s t u v w x y z

Read More

Find the sum of digits of a number at even and odd places in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose, we have an integer N, We have to find the sum of the odd place digits and the even place digits. So if the number is like 153654, then odd_sum = 9, and even_sum = 15.To solve this, we can extract all digits from last digit, if the original number has odd number of digits, then the last digit must be odd positioned, else it will be even positioned. After process a digit, we can invert the state from odd to even and vice versa.Example#include using namespace std; bool isOdd(int x){    if(x % 2 == 0)    return ...

Read More

Array Size in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 4K+ Views

The size of an array in Perl can be determined using the scalar context on the array - the returned value will be the number of elements in the array −@array = (1, 2, 3); print "Size: ", scalar @array, "";The value returned will always be the physical size of the array, not the number of valid elements. You can demonstrate this, and the difference between scalar @array and $#array, using this fragment is as follows −Example#!/usr/bin/perl @array = (1, 2, 3); $array[50] = 4; $size = @array; $max_index = $#array; print "Size: $size"; print "Max Index: $max_index";OutputThis will ...

Read More

Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.

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

Consider we have an array with size n. This array is sorted. There is one element whose frequency is greater than or equal to n/2, where n is the number of elements in the array. So if the array is like [3, 4, 5, 5, 5], then the output will be 5.If we closely observe these type of array, we can easily notice that the number whose frequency is greater than or equal to n/2, will be present at index n/2 also. So the element can be found at position n/2ExampleSource Code: #include using namespace std; int higherFreq(int arr[], int ...

Read More

Adding and Removing Elements in Perl Array

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 3K+ Views

Perl provides a number of useful functions to add and remove elements in an array. You may have a question what is a function? So far you have used the print function to print various values. Similarly, there are various other functions or sometimes called subroutines, which can be used for various other functionalities.Sr.No.Types & Description1push @ARRAY, LISTPushes the values of the list onto the end of the array.2pop @ARRAYPops off and returns the last value of the array.3shift @ARRAYShifts the first value of the array off and returns it, shortening the array by 1 and moving everything down.4unshift @ARRAY, ...

Read More

Slicing Array Elements in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ Views

You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.Example#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; @weekdays = @days[3, 4, 5]; print "@weekdays";OutputThis will produce the following result −Thu Fri SatThe specification for a slice must have a list of valid indices, either positive or negative, each separated by a comma. For speed, you can also use the .. range operator −Example#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; @weekdays = @days[3..5]; print "@weekdays";OutputThis will produce the ...

Read More
Showing 28571–28580 of 61,299 articles
Advertisements