Programming Articles

Page 345 of 2544

Program to find count of numbers having odd number of divisors in given range in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 449 Views

In this tutorial, we will be discussing a program to find the count of numbers having odd number of divisors in a given range.For this we will be provided with the upper and lower limits of the range. Our task is to calculate and count the number of values having an odd number of divisors.Example#include using namespace std; //counting the number of values //with odd number of divisors int OddDivCount(int a, int b){    int res = 0;    for (int i = a; i

Read More

Atbash cipher in Python

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

Suppose we have a lowercase alphabet string called text. We have to find a new string where every character in text is mapped to its reverse in the alphabet. As an example, a becomes z, b becomes y and so on.So, if the input is like "abcdefg", then the output will be "zyxwvut"To solve this, we will follow these steps −N := ASCII of ('z') + ASCII of ('a')return ans by joining each character from ASCII value (N - ASCII of s) for each character s in textLet us see the following implementation to get better understanding −Exampleclass Solution:   ...

Read More

Program to find covariance in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 770 Views

In this tutorial, we will be discussing a program to find covariance.For this we will be provided with two sets of random variables. Our task is to calculate their covariance i.e, the measure of how much those two values differ together.Example#include using namespace std; //function to find mean float mean(float arr[], int n){    float sum = 0;    for(int i = 0; i < n; i++)    sum = sum + arr[i];    return sum / n; } //finding covariance float covariance(float arr1[], float arr2[], int n){    float sum = 0;    for(int i = 0; i < ...

Read More

Find pairs with given sum such that pair elements lie in different BSTs in Python

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

Suppose we have two given Binary Search Trees and another sum is given; we have to find pairs with respect of given sum so that each pair elements must be in different BSTs.So, if the input is like sum = 12then the output will be [(6, 6), (7, 5), (9, 3)]To solve this, we will follow these steps −Define a function solve() . This will take trav1, trav2, Sumleft := 0right := size of trav2 - 1res := a new listwhile left < size of trav1 and right >= 0, doif trav1[left] + trav2[right] is same as Sum, theninsert (trav1[left], ...

Read More

Construct a linked list from 2D matrix in C++

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

Suppose we have one matrix, we have to convert it to 2d linked list using recursive approach.The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −Define a function make_2d_list(), this will take matrix mat, i, j, m, n, if i and j are not in the matrix boundary, then −return nulltemp := create a new node with value mat[i, j]right of temp := make_2d_list(mat, i, j + 1, m, n)down of temp := make_2d_list(mat, i + 1, j, m, n)return tempExampleLet us see the ...

Read More

How to replace multiple spaces with a single space in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

There are several ways to replace multiple spaces with single space in C#.String.Replace − Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.Replace(String, String, Boolean, CultureInfo)String.Join Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.Regex.Replace −In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.Example using Regex −Exampleusing System; using System.Text.RegularExpressions; namespace DemoApplication{    class Program{       public ...

Read More

Austin Powers in Python

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

Suppose we have a number greater than 0, we have to check whether the number is power of two or not.So, if the input is like 1024, then the output will be True.To solve this, we will follow these steps −while n > 1, don := n / 2return true when n is same as 1, otherwise 0Let us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, n):       while n > 1:          n /= 2       return n == 1 ob = Solution() print(ob.solve(1024))Input1024OutputTrue

Read More

Program to find Cullen Number in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 170 Views

In this tutorial, we will be discussing a program to find Cullen Number.For this we will be provided with an integer. Our task is to find the cullen number at that position using the formula −2n* n + 1Example#include using namespace std; //finding the nth cullen number unsigned get_cullen(unsigned n){    return (1

Read More

Find paths from corner cell to middle cell in maze in C++

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

Suppose we have a square maze filled with numbers; we have to find all paths from a corner cell to the middle cell. Here, we will proceed exactly n steps from a cell in 4 directions Up, Down, Right and Left where n is the value of the cell. Thus, we can move to cell [i+n, j] to [i-n, j], [i, j+n], and [i, j-n] from a cell [i, j] where n is value of cell [i, j].So, if the input is like344473463675662662334325472655123656334301434334321335354326443351375363624345451then the output will be(0, 0)→(0, 3)→(0, 7)→(6, 7)→(6, 3)→(3, 3)→(3, 4)→(5, 4)→(5, 2)→(1, 2)→(1, 7)→(7, 7)→(7, 1)→(2, ...

Read More

Base 3 to integer in Python

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

Suppose we have a string s that is representing a number in base 3 (valid numbers 0, 1, or 2), we have to find its equivalent decimal integer.So, if the input is like "10122", then the output will be 98.To solve this, we will follow these steps −ans := 0for each digit c in s, doans := 3 * ans + creturn ansLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, s):       ans = 0       for c in map(int, s):          ans = 3 * ans + c       return ans ob = Solution() print(ob.solve("10122"))Input"10122"Output98

Read More
Showing 3441–3450 of 25,433 articles
« Prev 1 343 344 345 346 347 2544 Next »
Advertisements