Almost Perfect Number in C++ Almost Perfect Number is a positive integer n for which the sum of all its positive proper divisors (excluding the number itself ) is equal to n-1. (i.e., one less than the number n). It is also known as the least deficient number or slightly defective number. A positive proper divisor is a divisor of a number, excluding the number itself. For example, for n = 6; 1, 2, 3 are positive proper divisors but 6 itself is not. In Mathematics, we say A number n is almost perfect if: σ(n)-n = n-1 by ... Read More
Aliquot sum in C++ Aliquot sum of a positive integer n is the sum of all proper divisors of n. A positive proper divisor is a divisor of a number, excluding the number itself. For example, for n = 6; 1, 2, 3 are positive proper divisors but 6 itself is not. Scenario 1 Input: n = 20 Output: 22 Explanation: Proper divisors of 20 are: 1, 2, 4, 5, 10 Sum of proper divisors are: 1 + 2 + 4 + 5 + 10 = 22 So, aliquot sum of 20 is : 22 Scenario 2 Input: 15 ... Read More
A linked list is a linear data structure where each element is a separate object, commonly referred to as a node. Each node contains two fields: the data and a pointer to the next node in the list. In this article, we'll learn how to delete a node from a singly linked list using Python. Suppose we have a linked list with a few elements. Our task is to delete a specific node, given only access to that node - not the head of the list. For example: Input: 1 → 3 → 5 → 7 → ... Read More
The arithmetic mean deviation or mean absolute deviation is the summation of the absolute differences of each element and the mean of the dataset, then dividing it by the total number of data elements. To calculate the minimum arithmetic mean deviation, we used the median of the dataset in place of the mean. The formula to calculate the minimum arithmetic mean deviation is given as: $$ \text{MMD} = \frac{1}{n} \sum_{i=1}^{n} |x_i - M| = \frac{|x_1 - M| + |x_2 - M| + \cdots + |x_n - M|}{n} $$ Here are some example scenarios to calculate the ... Read More
We are given the root of a binary tree and two integers, val and depth. The root of the binary tree is at depth 1. Our task is to add a new row of nodes with value val at the given depth. If the depth is 1, we have to create a new root node with value val and make the original tree its left child. If the depth is greater than 1, we go to all nodes at depth depth - 1, add new left and right child nodes with value val, and connect the original left and right ... Read More
Check if any Anagram of a String is Palindrome An anagram is a rearrangement of the characters of a word or a phrase to generate a new word, using all the original characters exactly once. For example, thing and night are anagrams of each other. While palindrome is a word or phrase that reads the same forward and backward, like madam, racecar. In this article, we are going to check if any anagram of a string is a palindrome or not, i.e., we are not just checking whether the given string itself is a palindrome, but also whether it can ... Read More
The Binary Search Tree (BST) is a widely used data structure that maintains the elements in a sorted hierarchical order. Each node in the BST follows the specific property: The values in the left subtree are always less than the values of the current node. The values in the right subtree are always greater than the node. In this article, we are going to learn about checking if an array represents the inorder of a BST or not in Python. Checking for inorder of the BST Inorder traversal is the ... Read More
Check if Array Can Be Divided into Pairs with Sum Divisible by K In this article, we are given an array of integers and a number k. The task is to determine whether it is possible to divide the entire array into pairs such that the sum of every pair is divisible by k. For example, if the given array of integers is [2, 4, 1, 3], and a value k = 5. The task is to form pairs (two elements at a time) from the array such that when you add each pair, the result is divisible by ... Read More
Matrices are used to represent and manipulate structured data (such as grids, patterns). While working with matrix manipulation, we may find scenarios where to determine whether all the rows in a given matrix are circular rotations of one another. Circular Rotation in a Matrix A circular rotation of a list (or array) is a transformation where elements are shifted to the left (or right), and the element at the end will be wrapped around to the beginning. For example, if the original row is [1, 2, 3], its circular rotations are: [2, 3, 1] (left-rotated ... Read More
In this article, we will learn how to find a path between two nodes in an undirected graph and implement it in C++. We will represent graph as an adjacency list and use a depth-first search (DFS) algorithm to find the path. We already discussed finding a path between two nodes in a directed graph, check here. Finding Path Between Two Nodes in an Undirected Graph We have an adjacency list representation of an undirected graph adj[] and two nodes src and dest, our task is to write a program that finds the path between the src ... Read More