Programming Articles

Page 499 of 2547

Program to find length of longest interval from a list of intervals in Python

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

Suppose we have a list of intervals where each interval is in form [start, end]. We have to find the longest interval that we can make by merging any number of overlapping intervals. So, if the input is like [[1, 6], [4, 9], [5, 6], [11, 14], [16, 20]], then the output will be 9, as after merging, we have the interval [1, 9] of length 9. Algorithm Steps To solve this, we will follow these steps − Sort the list of intervals by start time Initialize union with the first interval from the intervals ...

Read More

Program to find length of longest Fibonacci subsequence from a given list in Python

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

Given a list of strictly increasing positive numbers, we need to find the length of the longest Fibonacci-like subsequence. A Fibonacci-like subsequence has at least 3 elements where each element equals the sum of the two preceding elements: A[i] = A[i-1] + A[i-2]. For example, if the input is nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], the output will be 6 because we can form the subsequence [1, 2, 3, 5, 8, 13]. Algorithm The approach uses a brute force method with the following steps: Convert ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 597 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 522 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 284 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 199 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 342 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 460 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 437 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
Showing 4981–4990 of 25,466 articles
« Prev 1 497 498 499 500 501 2547 Next »
Advertisements