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
-
Economics & Finance
Articles by Pavitra
Page 3 of 13
Python Program for nth multiple of a number in Fibonacci Series
The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... In this article, we'll find the position of the nth multiple of a given number k in the Fibonacci series. Problem Statement Given a number k and a value n, we need to find the position of the nth multiple of k in the Fibonacci series. For example, if k=4 and n=5, we find the 5th number in the Fibonacci series that is divisible by 4. Understanding the Approach ...
Read MorePython Program for Efficient program to print all prime factors of a given number
In this article, we will learn about an efficient solution to find and print all prime factors of a given number. Prime factors are prime numbers that divide the given number exactly. Problem statement − We are given a number, we need to find all the prime factors of a given number. Algorithm Approach The efficient solution follows these steps: First, divide the number by 2 as much as possible Then check odd numbers from 3 up to √n If any remainder exists greater than 2, it's also a prime factor Example ...
Read MorePython program to find uncommon words from two Strings
In this article, we will learn how to find uncommon words from two strings. Uncommon words are words that appear exactly once across both strings combined. Problem statement − We are given two strings, we need to get the uncommon words from the given strings. Understanding Uncommon Words A word is considered uncommon if it appears exactly once when we combine both strings. For example: String 1: "hello world" String 2: "world python" Uncommon words: "hello", "python" (each appears once) "world" appears twice, so it's not uncommon Using Dictionary to Count Word Frequency ...
Read MorePython program to find number of local variables in a function
In this article, we will learn how to find the number of local variables in a function using Python's built-in code object attributes. Problem statement − We are given a function, and we need to display the number of local variables defined within that function. Understanding Local Variables Local variables are variables that are defined inside a function and can only be accessed within that function's scope. Python stores information about functions in code objects, which include the count of local variables. Using __code__.co_nlocals Every function in Python has a __code__ attribute that contains a ...
Read MorePython Program to Count trailing zeroes in factorial of a number
In this article, we will learn how to count the number of trailing zeros in the factorial of a given number without actually computing the factorial itself. Problem statement − We are given an integer n, we need to count the number of trailing zeros in n! (factorial of n). Understanding the Concept Trailing zeros are produced by factors of 10, and 10 = 2 × 5. In any factorial, there are always more factors of 2 than factors of 5, so we only need to count the factors of 5. ...
Read MorePython Program to Count set bits in an integer
In this article, we will learn how to count set bits (1's) in the binary representation of an integer. A set bit refers to a bit position that contains the value 1. Problem statement − We are given an integer n, we need to count the number of 1's in the binary representation of the number. For example, the number 15 in binary is 1111, which has 4 set bits. Method 1: Using Iterative Approach This approach uses bitwise AND operation to check each bit ? def count_set_bits(n): count = ...
Read MorePython Program to Count number of binary strings without consecutive 1'
In this article, we will learn about counting binary strings of length N that don't contain consecutive 1's. This is a classic dynamic programming problem that follows the Fibonacci pattern. Problem statement − We are given a positive integer N, and we need to count all possible distinct binary strings of length N such that no two consecutive 1's exist in the string. Approach We can solve this using dynamic programming by considering two cases ? a[i] = number of valid strings of length i ending with 0 b[i] = number of valid strings of ...
Read MorePython Program to Count Inversions in an array
In this article, we will learn about counting inversions in an array. An inversion occurs when a larger element appears before a smaller element in an array. Problem statement − We are given an array, we need to count the total number of inversions and display it. Inversion count represents how far an array is from being sorted. For example, in array [2, 3, 8, 6, 1], the pairs (2, 1), (3, 1), (8, 6), (8, 1), and (6, 1) are inversions. What is an Inversion? An inversion is a pair of indices (i, j) where ...
Read MorePython Program for Triangular Matchstick Number
In this article, we will learn how to calculate the total number of matchsticks required to build a triangular pyramid. Given the number of floors X, we need to find the total matchsticks needed to construct the entire pyramid structure. Problem statement − We are given a number X which represents the floor of a matchstick pyramid, we need to display the total number of matchsticks required to form a pyramid of matchsticks with X floors. Understanding the Pattern In a triangular matchstick pyramid: Floor 1 requires 3 matchsticks (forming 1 triangle) Floor 2 requires ...
Read MorePython Program for Subset Sum Problem
In this article, we will learn how to solve the Subset Sum Problem using Python. This is a classic computer science problem where we need to determine if there exists a subset of given numbers that adds up to a target sum. Problem Statement: Given a set of non-negative integers and a target sum, determine if there exists a subset of the given set with a sum equal to the target sum. Naive Recursive Approach The recursive approach explores all possible subsets by either including or excluding each element ? def subset_sum_recursive(numbers, n, target_sum): ...
Read More