Programming Articles

Page 140 of 2547

Program to find maximum score we can get in jump game in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 781 Views

Suppose we have an array called nums and another value k. We are at index 0. In one move, we can jump at most k steps right without going outside the boundaries of the array. We want to reach the final index of the array. For jumping we get score, that is the sum of all nums[j] for each index j we visited in the array. We have to find the maximum score we can get. So, if the input is like nums = [1, -2, -5, 7, -6, 4] and k = 2, then the output will be ...

Read More

Program to find minimum elements to add to form a given sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 416 Views

When working with arrays that have element constraints, we often need to find the minimum number of elements to add to achieve a target sum. This problem involves an array where each element's absolute value is bounded by a limit, and we need to reach a specific goal sum. Problem Statement Given an array nums and two values limit and goal, where |nums[i]| ≤ limit for all elements, we need to find the minimum number of elements to insert so that the array sum equals the goal. The inserted elements must also satisfy the limit constraint. Example ...

Read More

Program to find maximum erasure value in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 228 Views

Suppose we have an array called nums (with positive values only) and we want to erase a subarray containing unique elements. We will get a score that is the sum of subarray elements. We have to find the maximum score we can get by erasing exactly one subarray. So, if the input is like nums = [6, 3, 2, 3, 6, 3, 2, 3, 6], then the output will be 11, because the optimal subarray is either [6, 3, 2] or [2, 3, 6], so the sum is 11. Algorithm To solve this, we will follow these ...

Read More

Program to find sum of beauty of all substrings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 811 Views

Given a string, we need to find the sum of beauty of all its substrings. The beauty of a string is the difference between the frequencies of the most frequent and least frequent characters. For example, if the string is "abaacc", the frequency of 'a' is 3 and 'b' is 1, so beauty = 3 - 1 = 2. Problem Example If the input string is "xxyzy", the substrings with non-zero beauty are: "xxy" → beauty = 2 - 1 = 1 "xxyz" → beauty = 2 - 1 = 1 "xxyzy" → beauty = ...

Read More

Program to find minimum difference of stone games score in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 297 Views

Suppose we have an array called stones where stones[i] represents the value of the ith stone from the left. Two friends Amal and Bimal are playing a turn-based game with these stones and Amal starts first always. There are n stones arranged in a row. Each player can remove either the leftmost stone or the rightmost stone from the row and get points equal to the sum of the remaining stones' values in the row. The player with the higher score wins. Bimal found that he will always lose this game, so he decided to minimize the score's difference. ...

Read More

Program to check whether number is a sum of powers of three in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 620 Views

Suppose we have a number n, we have to check whether it is possible to represent n as the sum of distinct powers of three or not. An integer y is said to be power of three if there exists an integer x such that y = 3^x. So, if the input is like n = 117, then the output will be True because 117 = 3^4 + 3^3 + 3^2 = 81 + 27 + 9. Algorithm To solve this, we will follow these steps ? for i in range 16 to 0, decrease ...

Read More

Program to partitioning into minimum number of Deci- Binary numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 593 Views

Given a decimal number as a string, we need to find the minimum number of deci-binary numbers whose sum equals the given number. A deci-binary number contains only digits 0 and 1. For example, if the input is "132", the output will be 3 because 132 can be expressed as the sum of three deci-binary numbers: 100 + 11 + 21 = 132. Algorithm The key insight is that the minimum number of deci-binary numbers needed equals the maximum digit in the input number. This is because: Each deci-binary number can contribute at most 1 ...

Read More

Program to find winner of stone game in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 475 Views

Suppose Amal and Bimal are playing a stone game where Amal goes first. The game works as follows − There are n stones in a pile. Each player can take a stone and receive points based on their individual valuation of that stone. We have two arrays A_Values and B_Values where A_Values[i] and B_Values[i] represent how Amal and Bimal value the ith stone respectively. The player with the highest total score wins. If scores are equal, it's a draw. Both players play optimally and know each other's valuations. Return 1 if Amal wins, -1 if Bimal wins, and ...

Read More

Program to find maximum width ramp in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 393 Views

A ramp in an array is a pair of indices (i, j) where i < j and nums[i] ≤ nums[j]. The width of a ramp is the difference (j - i). We need to find the maximum width ramp in the given array. For example, in the array [6, 0, 8, 2, 1, 5], the maximum width ramp is at indices (1, 5) where nums[1] = 0 and nums[5] = 5, giving us a width of 4. Approach The solution uses a value-to-indices mapping approach ? Group indices by their values in a dictionary Process ...

Read More

Program to find sum of absolute differences in a sorted array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 595 Views

Given a sorted array in non-decreasing order, we need to create a result array where each element contains the sum of absolute differences between that element and all other elements in the array. For example, with nums = [5, 7, 12], the result will be [9, 7, 12] because: |5−5| + |5−7| + |5−12| = 0+2+7 = 9 |7−5| + |7−7| + |7−12| = 2+0+5 = 7 |12−5| + |12−7| + |12−12| = 7+5+0 = 12 Naive Approach The straightforward approach calculates absolute differences for each element ? def solve_naive(nums): ...

Read More
Showing 1391–1400 of 25,469 articles
« Prev 1 138 139 140 141 142 2547 Next »
Advertisements