Dev Prakash Sharma

Dev Prakash Sharma

414 Articles Published

Articles by Dev Prakash Sharma

Page 29 of 42

Balanced Binary Tree in Python

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 5K+ Views

In a binary tree, each node contains two children, i.e left child and right child. Let us suppose we have a binary tree and we need to check if the tree is balanced or not. A Binary tree is said to be balanced if the difference of height of left subtree and right subtree is less than or equal to 1. Problem Examples Example 1 - Balanced Tree 1 2 ...

Read More

Write a program in Python to replace all the 0's with 5 in a given number

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 801 Views

Given an integer N, the task is to replace all the 0's that appear in the number with '5'. However, leading zeros are ignored as they don't affect the number's value. For example, if N = 1007, the output should be 1557. Example Cases Input-1 − N = 1007 Output − 1557 Explanation − The given number has 2 zeros which when replaced by '5' results in the output as 1557. Input-2 − N = 105 Output − 155 Explanation − ...

Read More

Majority Element in Python

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 3K+ Views

The majority element is an element that appears more than half the times in an array. For example, in an array of size 8, a majority element must appear at least 5 times. Python provides several approaches to find the majority element efficiently. Problem Definition Given an array of integers, find the element that appears more than n/2 times, where n is the array size. If no such element exists, return -1. Example 1 ? Input: [2, 1, 1, 2, 2, 2] Output: 2 Explanation: Element 2 appears 4 times out of 6, which is ...

Read More

Write a program in Python to count the number of digits in a given number N

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 8K+ Views

Let's suppose we have given a number N. The task is to find the total number of digits present in the number. For example, Input-1 − N = 891452 Output − 6 Explanation − Since the given number 891452 contains 6 digits, we will return '6' in this case. Input-2 − N = 74515 Output − 5 Explanation − Since the given number 74515 contains 5 digits, we will print the output as 5. The Approach Used to Solve This Problem ...

Read More

Count Good Meals in Python

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 409 Views

A good meal contains exactly two different food items with a sum of deliciousness equal to a power of two. You can pick any two different foods to make a good meal. Given an array of integers arr where arr[i] is the deliciousness of the ith item of food, we need to return the number of different good meals you can make from this list. Examples Input-1 − arr = [1, 3, 5, 7, 9] Output − 4 Explanation − The good meals are (1, 3), (1, 7), (3, ...

Read More

Birthday Paradox in Python

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 25-Mar-2026 1K+ Views

The Birthday Paradox is a famous probability problem that asks: "How many people need to be in a room before there's a 50% chance that two people share the same birthday?" The counterintuitive answer is just 23 people! This paradox demonstrates how our intuition about probability can be misleading. Understanding the Mathematics The probability that two people have different birthdays is 364/365, which equals (1 - 1/365) in a non-leap year. For multiple people, the probability that all have different birthdays is: P(different) = 1 × (1-1/365) × (1-2/365) × (1-3/365) × ... Therefore, ...

Read More

Write a program in JavaScript to check if two strings are anagrams of each other or not

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 15-Mar-2026 23K+ Views

Given two strings 'a' and string 'b', we have to check if they are anagrams of each other or not and return True/False. Two strings are anagrams if they contain the same characters with the same frequency, just rearranged. Input-1 − String a = "india" String b = "nidia" Output − True Explanation − Since the given string 'b' contains all the characters in the string 'a' with the same frequency, we will return True. Input-2 − String a = "hackathon" String b = "achcthoon" Output ...

Read More

Significance of Lambda Function in C/C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 15-Mar-2026 4K+ Views

Lambda Function − Lambda functions are anonymous inline functions that don't require any implementation outside the scope where they are defined. They provide a concise way to write small functions directly at the point of use. Lambda functions can also be stored in variables and treated as objects that can be called (called functors). When the compiler encounters a lambda function definition, it creates a custom object for that lambda. Note: Lambda functions are a C++11 feature and are NOT available in C. They are part of the C++ standard, not C. In C programming, you would use ...

Read More

Nth Catalan Number in Go Lang

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 302 Views

Catalan Numbers are sequences of natural numbers that gives the number of BST (Binary Search Tree) possible with n Values. So, the Catalan number is a full binary tree with n+1 leaves.Some applications of Catalan Numbers are counting the pairs of nested parentheses, valid mountain ranges etc.For n = 5, C = (C(0) * C(4)) + (C(1) * C(3)) + (C(2) * C(2)) + (C(3) * C(1)) + (C(4)* C(0))Thus, we can see that Catalan numbers are in the form of a recursive relation, i.e., for the nth term, the Catalan number Cn is,             ...

Read More

Write a program in C++ to replace all the 0's with 5 in a given number

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 2K+ Views

Given an integer N, the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example, Input-1 −N= 1007Output −1557Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.Input-2 −N = 00105Output −155Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.Approach to Solve this ProblemTo replace all the ...

Read More
Showing 281–290 of 414 articles
« Prev 1 27 28 29 30 31 42 Next »
Advertisements