Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 97 of 377

Program to find longest number of 1s after swapping one pair of bits in Python

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

Suppose we have a binary string s. If we can swap at most one pair of characters in the string, we have to find the resulting length of the longest contiguous substring of 1s. So, if the input is like s = "1111011111", then the output will be 9, as we can swap s[4] and s[9] to get 9 consecutive 1s. Algorithm To solve this, we will follow these steps − l := 0, cnt := 0, ans := 0 for r in range 0 to size ...

Read More

Program to find union of two given linked lists in Python

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

Suppose we have two sorted linked lists L1 and L2, we have to return a new sorted linked list that is the union of the two given lists. The union contains all unique elements from both lists in sorted order. So, if the input is like L1 = [10, 20, 30, 40, 50, 60, 70] L2 = [10, 30, 50, 80, 90], then the output will be [10, 20, 30, 40, 50, 60, 70, 80, 90] Algorithm To solve this, we will follow these steps − Define a function solve(). This ...

Read More

Program to convert linked list to zig-zag binary tree in Python

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

When we have a singly linked list, we can convert it to a binary tree using a specific zig-zag pattern. The conversion follows these rules: The head of the linked list becomes the root of the tree Each subsequent node becomes the left child if its value is less than the parent, otherwise it becomes the right child So, if the input is like [2, 1, 3, 4, 0, 5], then the output will be: 2 ...

Read More

Program to arrange linked list nodes based on the value k in Python

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

Suppose we have a singly linked list and a value k. We need to rearrange the nodes so that all nodes with values less than k come first, followed by nodes equal to k, and finally nodes greater than k. The relative ordering within each group must be preserved. For example, if we have a linked list [4, 3, 6, 6, 6, 10, 8] and k = 6, the output will be [4, 3, 6, 6, 6, 10, 8]. Algorithm To solve this problem, we will follow these steps − Create three ...

Read More

Program to find folded list from a given linked list in Python

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

Suppose we have a linked list. We have to take the first half of the linked list and fold over the second half then merge the intersecting nodes by taking their sum. Finally, we have to return the resulting head of the linked list. So, if the input is like [5, 8, 1, 2, 4, 7, 5], then the output will be [2, 5, 15, 10]. Understanding the Folding Process The folding process works as follows: Split the linked list into two halves Reverse the first half ...

Read More

Program to remove last occurrence of a given target from a linked list in Python

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

Suppose we have a singly linked list and a target value, we have to remove the last occurrence of the target from the given list. So, if the input is like [5, 4, 2, 6, 5, 2, 3, 2, 4, 5, 4, 7], target = 5, then the output will be [5, 4, 2, 6, 5, 2, 3, 2, 4, 4, 7] Algorithm To solve this, we will follow these steps − Initialize head := node Set k := null, prev := null ...

Read More

Program to check whether list of points form a straight line or not in Python

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

Suppose we have a list of coordinates in a Cartesian plane, we have to check whether the coordinates form a straight line segment or not. So, if the input is like coordinates = [(5, 5), (8, 8), (9, 9)], then the output will be True, as these points are forming a line segment with a slope 1. Algorithm To solve this, we will follow these steps − (x0, y0) := coordinates[0] (x1, y1) := coordinates[1] for i in range 2 to size of ...

Read More

Program to find lexicographically smallest non-palindromic string in Python

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

Given a palindromic string, we need to change exactly one character to make it the lexicographically smallest non-palindromic string possible. The strategy is to iterate through the first half of the string and replace the first non-'a' character with 'a'. If all characters in the first half are 'a', we change the last character to 'b'. Example Let's see how this works with the string "level" ? def find_smallest_non_palindrome(s): # Check first half for non-'a' characters for i in range(len(s) // 2): ...

Read More

Program to find minimum number of bricks required to make k towers of same height in Python

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

Suppose we have a list of tower heights, and a positive value k. We want to select k towers and make them all the same height by adding more bricks, but using as few bricks as possible. We have to find the minimum number of bricks needed to pick k towers and make them the same height. So, if the input is like heights = [4, 7, 31, 14, 40] k = 3, then the output will be 17, as we can select towers with heights [7, 14, 31] and make them all height 31, requiring (31-7) + (31-14) ...

Read More

Program to find sum of non-adjacent elements in a circular list in python

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

Suppose we have a list of numbers representing a circular list. We need to find the largest sum of non-adjacent elements, where the first and last elements are considered adjacent due to the circular nature. For example, if the input is nums = [10, 3, 4, 8], the output will be 14 by selecting elements 10 and 4. We cannot select 10 and 8 because they are adjacent in the circular arrangement. Algorithm Approach Since the list is circular, we need to handle two cases separately: Case 1: Include the first element but exclude the ...

Read More
Showing 961–970 of 3,768 articles
« Prev 1 95 96 97 98 99 377 Next »
Advertisements