Server Side Programming Articles

Page 518 of 2109

Program to find how many years it will take to reach t amount in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 286 Views

Suppose we have some parameters P, O, E, T. If we have P dollars in principal that we want to invest in the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars. Problem Understanding The investment follows an alternating pattern: Year 1, 3, 5... − Apply E percent interest Year 2, 4, 6... − Apply O percent interest We need to calculate how many years it takes for the principal ...

Read More

Program to count k length substring that occurs more than once in the given string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 279 Views

Sometimes we need to count how many k-length substrings appear more than once in a given string. This is useful for pattern analysis and string processing tasks. So, if the input is like s = "xxxyyy", k = 2, then the output will be 2 because substrings "xx" and "yy" each occur more than once. Algorithm Steps To solve this, we will follow these steps − Create a list to store all k-length substrings For i in range 0 to size of s - k, do Extract substring of s [from index i to ...

Read More

Program to find sum of digits until it is one digit number in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 4K+ Views

Sometimes we need to keep adding the digits of a number repeatedly until we get a single digit. This process is called finding the digital root of a number. So, if the input is like 9625, then the output will be 4 because: 9 + 6 + 2 + 5 = 22 2 + 2 = 4 Using Recursive Approach We can solve this by recursively summing the digits until we get a single digit ? import math class Solution: def solve(self, n): ...

Read More

Program to find duplicate elements and delete last occurrence of them in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 414 Views

When working with lists in Python, sometimes we need to find duplicate elements and remove their last occurrences. This problem requires tracking the frequency of each element and identifying when we encounter the final duplicate. So, if the input is like [10, 30, 40, 10, 30, 50], then the output will be [10, 30, 40, 50]. Algorithm Steps To solve this problem, we follow these steps: Count the frequency of each element in the list Track how many times we've seen each element while iterating When the seen count equals the total frequency and it's ...

Read More

Program to count number of elements present in a set of elements with recursive indexing in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 321 Views

Recursive indexing involves creating a chain of elements by repeatedly using values as indices. Given a list A and starting index k, we build a set {A[k], A[A[k]], A[A[A[k]]], ...} until we reach an out-of-bounds index or detect a cycle. If there's a cycle (we encounter the same value twice), we return -1. Otherwise, we return the size of the unique elements collected. Example Walkthrough For A = [1, 2, 3, 4, 5, 6, 7] and k = 1: A[1] = 2, add 2 to set A[2] = 3, add 3 to set A[3] = ...

Read More

Program to find the index of first Recurring Character in the given string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 420 Views

In string processing, we often need to find the first recurring character — the first character that appears again in the string. This problem requires us to track which characters we've seen and return the index where we encounter a character for the second time. So, if the input is like "abcade", then the output will be 3, as 'a' appears first at index 0 and recurs at index 3. Approach To solve this, we will follow these steps ? Create a set to store characters we've already seen ...

Read More

Program to check two rectangular overlaps or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 4K+ Views

Suppose we have a rectangle that is represented as a list with four elements [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Two rectangles overlap when the area of their intersection is positive. So, two rectangles that only touch at the corner or edges do not overlap. So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True. Visual Representation R1 R2 Overlap X-axis Y-axis 0 1 2 3 0 1 2 Algorithm To solve this, we will follow these steps − if R1[0]>=R2[2] or R1[2]

Read More

Program to check if the given list has Pythagorean Triplets or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

A Pythagorean triplet is a set of three numbers a, b, and c such that a² + b² = c². Given a list of numbers, we need to check whether there exist three numbers that form a Pythagorean triplet. So, if the input is like [10, 2, 8, 5, 6], then the output will be True, as 8² + 6² = 64 + 36 = 100 = 10². Algorithm To solve this problem efficiently, we will follow these steps − Create a list of squares of all numbers in descending order ...

Read More

Program to find all prime factors of a given number in sorted order in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 858 Views

Prime factorization is the process of breaking down a number into its prime factors. A prime factor is a prime number that divides the given number exactly. For example, the prime factors of 42 are 2, 3, and 7 because 42 = 2 × 3 × 7. So, if the input is 42, then the output will be [2, 3, 7]. Algorithm To find all prime factors efficiently, we follow these steps: Create an empty result list Handle factor 2 separately (divide by 2 until odd) Check odd numbers from 3 to √n If any ...

Read More

Program to find minimum total cost for equalizing list elements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 338 Views

Suppose we have two lists of numbers called nums and costs. We can increase or decrease nums[i] for cost costs[i]. We want to make all elements in nums equal with minimum total cost. So, if the input is like nums = [3, 2, 4] and costs = [1, 10, 2], then the output will be 5. We can decrease 3 to 2 for cost 1, then decrease 4 to 2 (twice) for cost 2 each, totaling 1 + 2 + 2 = 5. Approach This problem uses ternary search to find the optimal target value. The cost ...

Read More
Showing 5171–5180 of 21,090 articles
« Prev 1 516 517 518 519 520 2109 Next »
Advertisements