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
Programming Articles
Page 231 of 2547
Program to find expected growth of virus after time t in Python
Suppose there is a dangerous virus that grows rapidly. The probability of virus cells growing by a factor x is 0.5, and the probability of growing by a factor y is also 0.5. Starting with a single virus cell, we need to calculate the expected number of virus cells after time t. If the result is large, we return it modulo 10^9+7. The expected growth factor at each time step is (x + y) / 2, since each factor has probability 0.5. Problem Analysis At each time step, the virus can grow by factor x or y ...
Read MoreProgram to find number of solutions for given equations with four parameters in Python
Suppose we have four numbers a, b, c and d. We need to find the number of pairs (x, y) that satisfy the equation: x² + y² = (x*a) + (y*b) where x is in range [1, c] and y is in range [1, d]. For example, if a = 2, b = 3, c = 2, d = 4, then the output will be 1 because one valid pair is (1, 1). Mathematical Approach We can rearrange the equation x² + y² = x*a + y*b as a quadratic in y: y² - b*y + ...
Read MoreProgram to find last digit of the given sequence for given n in Python
We need to find the last digit of a sequence S for a given value n. The sequence S is defined by a mathematical formula involving powers of 2. Understanding the Problem The sequence formula is complex, but we can simplify it to find only the last digit. For n = 2, we calculate specific terms and sum them to get 126, where the last digit is 6. Algorithm Steps To solve this problem efficiently ? Initialize total = 0 and temp = 1 While temp ≤ n, add (2^temp mod 10) to total ...
Read MoreProgram to find position of first event number of line l of a triangle of numbers in Python
Suppose we are generating a number triangle where each element in a row is the sum of three numbers above it ? 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1 1 4 10 16 19 16 10 4 1 ...
Read MoreProgram to find sum of the 2 power sum of all subarray sums of a given array in Python
Given an array, we need to find all possible non-empty subarrays, calculate the sum of each subarray, then compute the sum of 2 raised to the power of each subarray sum. The result should be returned modulo (109 + 7). For an array with n elements, there are (2n - 1) non-empty subarrays. For each subarray sum Si, we calculate 2Si and sum all these values to get P = 2S1 + 2S2 + 2S3 + ... + 2S(2n-1). Example Walkthrough For the array A = [2, 2, 3], the non-empty subarrays and their sums are ? ...
Read MoreProgram to check we can reach at position n by jumping or not in Python
Suppose there is a number line from 1 to n. At first we are at position 0, jump one step to go 1, then jump two places to reach at position 3, then jump three positions to reach at 6 and so on. We have to check whether maintaining this pattern, we can reach at position n or not. So, if the input is like n = 21, then the output will be True, because 1+2+3+4+5+6 = 21 Approach To solve this, we will follow these steps − Calculate j := (1 + square root of (1+8*n)) / 2 If |j - int part of j|
Read MoreProgram to find sum of the costs of all simple undirected graphs with n nodes in Python
Suppose we have an undirected graph G with n nodes. The cost of a simple undirected graph is the sum of the costs of its nodes, where the cost of a node is Dk (D is its degree). Given n and k values, we need to find the sum of the costs of all possible simple undirected graphs with n nodes. The result may be very large, so we return the result modulo 1005060097. Example Walkthrough If n = 3 and k = 2, there are eight simple graphs with 3 nodes: One graph with ...
Read MoreProgram to find number of elements in all permutation which are following given conditions in Python
Suppose we have a set A where all elements from 1 to n are present. And P(A) represents all permutations of elements present in A. We have to find number of elements in P(A) which satisfies the given conditions: For all i in range [1, n], A[i] is not same as i There exists a set of k indices {i1, i2, ... ik} such that A[ij] = ij+1 for all j < k and A[ik] = i1 (cyclic) So, if the input is like n = 3 k = 2, then the output will be 0. ...
Read MoreProgram to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
Suppose we have a string with n number of A's and 2n number of B's. We need to find the number of arrangements possible such that in each prefix and each suffix, the number of B's is greater than or equal to the number of A's. This is a classic problem in combinatorics known as the ballot problem or Catalan number variant. The constraint ensures that at any point while reading from left to right (or right to left), we never have more A's than B's. Problem Understanding For n = 2, we have 2 A's and ...
Read MoreProgram to find lexicographically smallest string to move from start to destination in Python
Suppose we are at position (0, 0) in the Cartesian plane and want to reach point (x, y) using only horizontal (H) and vertical (V) moves of single unit. There are multiple possible paths to reach the destination, each comprising H and V moves. We need to find the lexicographically kth smallest path. For example, to go from (0, 0) to (2, 2), one possible path is "HVVH". Since 'H' comes before 'V' lexicographically, paths starting with more H's appear earlier in lexicographical order. Algorithm The solution uses a greedy approach with combinatorial counting ? ...
Read More