Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 63 of 377

Program to find ex in an efficient way in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 153 Views

Suppose we have a number n. We have to find $e^{x}$ efficiently, without using library functions. The formula for $e^{x}$ is like$$e^{x} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + ...$$So, if the input is like x = 5, then the output will be 148.4131 because e^x = 1 + 5 + (5^2/2!) + (5^3/3!) + ... = 148.4131...To solve this, we will follow these steps −fact := 1res := 1n := 20 it can be large for precise resultsnume := xfor i in range 1 to n, dores := res + nume/factnume := nume * xfact := fact ...

Read More

Program to count number of common divisors of two numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 664 Views

Suppose we have two numbers a and b. We have to find how many positive integers are there, that are divisors to both a and b.So, if the input is like a = 288 b = 240, then the output will be 10 because the common divisors are [1, 2, 3, 4, 6, 8, 12, 16, 24, 48].To solve this, we will follow these steps −res := 0for i in range 1 to gcd(a, b) + 1, doif (a mod i) is 0 and (b mod i) is 0, thenres := res + 1return resExampleLet us see the following implementation ...

Read More

C++ program to demonstrate function of macros

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 536 Views

Suppose we are given a integer array that contains several integer values. We have to find out the difference between the smallest value and the largest value in the array. To solve this problem, we have to use macros. The inputs are taken from stdin, and the result is printed back to stdout.So, if the input is like array = {120, 589, 324, 221, 234}, then the output will be The answer is : 469The difference between the largest value 589 and the smallest value 120 is 469.To solve this, we will follow these steps −mini := infinitymaxi := negative ...

Read More

Program to count indices pairs for which elements sum is power of 2 in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 377 Views

Suppose we have a list of numbers called nums. We have to find the number of index pairs i, j, where i < j such that nums[i] + nums[j] is equal to 2^k for some 0 >= k.So, if the input is like nums = [1, 2, 6, 3, 5], then the output will be 3, as there are three pairs sum like (6, 2): sum is 8, (5, 3): sum is 8 and (1, 3) sum is 4To solve this, we will follow these steps −res := 0c := a map containing frequencies of each elements present infor each ...

Read More

Program to check we can get a digit pair and any number of digit triplets or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 313 Views

Suppose we have a numeric string s. We have to check whether there is some arrangement where we can have one pair of the same character and the rest of the string form any number of triplets of the same characters.So, if the input is like s = "21133123", then the output will be True, because there are two 2s to form "22" as the pair and "111", "333" as two triplets.To solve this, we will follow these steps −d := a list containing frequencies of each elements present in sfor each k in d, dod[k] := d[k] - 2if ...

Read More

Program to check string is palindrome with lowercase characters or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 900 Views

Suppose we have alphanumeric string s. It can hold both uppercase or lowercase letters. We have to check whether s is a palindrome or not considering only the lowercase alphabet characters.So, if the input is like s = "rLacHEec0a2r8", then the output will be True because the string contains "racecar" in lowercase, which is a palindrome.To solve this, we will follow these steps −x := blank stringfor each character i in s, doif i is in lowercase, thenx := x concatenate ireturn true when x is palindrome, otherwise falseExampleLet us see the following implementation to get better understandingdef solve(s):   ...

Read More

Program to find mutual followers from a relations list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 257 Views

Suppose we have a list called relations. Where each element in relations list relations[i] contains two numbers [ai, bi] it indicates person ai is following bi on a social media platform. We have to find the list of people who follow someone and they follow them back, we have to return it in sorted sequence.So, if the input is like relations = [[0, 2], [2, 3], [2, 0], [1, 0]], then the output will be [0, 2].To solve this, we will follow these steps −ans := a new setseen := a new setfor each pair a and b in relations, ...

Read More

Program to find minimum number of monotonous string groups in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 417 Views

Suppose we have a lowercase string s. We have to find the minimum numbers of contiguous substrings in which s is divided into parts such that each substring is either non-increasing or non-decreasing. So for example, if the string is like "pqqqr" is a non-decreasing string, and "qqqp" is a non-increasing string.So, if the input is like s = "pqrsrqp", then the output will be 2, because we can break s like "pqrs" and "rqp".To solve this, we will follow these steps −if s is empty, thenreturn 0last := s[0]direction := 1count := 1for each char in s, doif char ...

Read More

Program to find minimum value to insert at beginning for all positive prefix sums in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 264 Views

Suppose we have a list of numbers called nums. We have to find the minimum positive value that we can insert at the beginning of nums so that that prefix sums of the resulting list contains numbers that are all larger than 0.So, if the input is like nums = [3, -6, 4, 3], then the output will be 4, because if we insert 4 to the list then we have [4, 3, -6, 4, 3]. Now the prefix sums are then [4, 7, 1, 5, 8], all are larger than 0.To solve this, we will follow these steps −insert ...

Read More

Program to find minimum distance of two given words in a text in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 12-Oct-2021 2K+ Views

Suppose we have three strings text, w1, and w2. The text is a sentence with different words. We have to find the smallest distance between any two occurrences of w1 and w2 in the text, the distance is measured in number of words in between them. If either w1 or w2 is not present in text, return -1.So, if the input is like text = "joy happy power happy joy joy power happy limit" w1 = "power" w2 = "limit", then the output will be 1, as there is only one word "happy" in between the power and limit.To solve ...

Read More
Showing 621–630 of 3,768 articles
« Prev 1 61 62 63 64 65 377 Next »
Advertisements