Server Side Programming Articles

Page 367 of 2109

Program to find minimum operations to make array equal using Python

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

Suppose we have a value n, consider an array nums with n elements, where arr[i] = (2*i)+1 for all i. Now in one operation, we can choose two indices x and y where 0 = 0, add (n-j) to ans, decrement q, and increment j by 2 Return ans Example def solve(n): ans = 0 if n == 1: return ans q = (n // 2) - 1 j = 1 ...

Read More

Program to find maximum number of non-overlapping subarrays with sum equals target using Python

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

Given an array of numbers and a target value, we need to find the maximum number of non-overlapping subarrays where each subarray's sum equals the target. For example, if nums = [3, 2, 4, 5, 2, 1, 5] and target = 6, we can find two subarrays: [2, 4] and [1, 5], both with sum 6. Approach Using Prefix Sum and Set The key insight is to use prefix sums and track cumulative sums in a set. When we find a valid subarray, we reset our tracking to avoid overlaps. Algorithm Steps Initialize a ...

Read More

Program to find Kth bit in n-th binary string using Python

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

Suppose we have two positive values n and k. We can construct a binary string S_n using the following rules: S_1 = "0" S_i = S_(i-1) concatenate "1" concatenate reverse(invert(S_(i-1))) for i > 1 Here reverse(x) returns the reversed string x, and invert(x) flips all the bits in x (0 becomes 1, 1 becomes 0). Binary String Generation Pattern Let's see how these strings are constructed: S_1 = "0" S_2 = "011" S_3 = "0111001" ...

Read More

Program to check whether we can convert string in K moves or not using Python

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

In this problem, we need to determine if we can convert string s to string t using at most k moves. In the i-th move, we can select an unused index and shift the character at that position i times forward in the alphabet (with wrapping from 'z' to 'a'). Problem Understanding The key insight is that in move i, we can shift a character by exactly i positions. Since the alphabet wraps around, we need to calculate how many times each required shift distance can be achieved within k moves. Algorithm Steps Check if ...

Read More

Program to find minimum swaps to arrange a binary grid using Python

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

When working with binary matrices, we sometimes need to arrange rows so that all elements above the major diagonal are 0. This can be achieved by swapping adjacent rows. Let's explore how to find the minimum number of swaps required. Problem Understanding Given an n x n binary matrix, we need to swap adjacent rows to ensure all elements above the major diagonal are 0. If no solution exists, we return -1. For example, with this matrix: 010 011 100 We need to rearrange it so that elements above the diagonal are all ...

Read More

Program to find number of good leaf nodes pairs using Python

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

A binary tree contains good leaf node pairs when the shortest path between two different leaf nodes is less than or equal to a given distance d. Consider this binary tree with distance d = 4 ? 1 2 3 4 5 6 8 ...

Read More

Program to make a bulb switcher using binary string using Python

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

Suppose we have n bulbs in a room, numbered from 0 to n-1 and arranged in a row from left to right. Initially, all bulbs are turned off (0-state). We need to achieve a target configuration represented by a binary string where '1' means the bulb is on and '0' means it's off. The bulb switcher has a special flipping operation: Select any bulb index i Flip each bulb from index i to index n-1 (flip all bulbs from position i to the end) We need to find the minimum number of flips required to ...

Read More

Program to find number of good ways to split a string using Python

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

Suppose we have a string s. A split is said to be a good split when we can split s into 2 non-empty strings p and q where their concatenation equals s and the number of distinct letters in p and q are equal. We need to find the number of good splits we can make in s. For example, if the input is s = "xxzxyx", then the output will be 2. We can split it as ("xxz", "xyx") or ("xxzx", "yx") — both are good splits since each part has the same number of distinct characters. ...

Read More

Program to find number of nodes in the sub-tree with the same label using Python

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

In this problem, we have a rooted tree with n nodes numbered from 0 to n-1, where each node has a label (lowercase English letter). We need to find the count of nodes in each node's subtree that have the same label as that node. Problem Understanding Given a tree structure and node labels, we need to return an array where each element represents the count of nodes with the same label in that node's subtree (including the node itself). 0(c) ...

Read More

Program to find path with maximum probability using Python

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

Suppose we have an undirected weighted graph with n nodes (nodes are numbered from 0 onwards). This graph is given as input using an edge list, and for each edge e, it has a probability of success of traversing that edge probability[e]. We also have start and end nodes, and we need to find the path with the maximum probability of success to go from start to end and return its success probability. If we cannot find any path, then return 0. Problem Example Consider the following graph: ...

Read More
Showing 3661–3670 of 21,090 articles
« Prev 1 365 366 367 368 369 2109 Next »
Advertisements