Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 495 of 2109
Program to find length of longest set of 1s by flipping k bits in Python
In this problem, we have a binary list containing only 0s and 1s, and we can flip at most k zeros to ones. Our goal is to find the length of the longest contiguous subarray that contains all 1s after performing these flips. For example, if the input is nums = [0, 1, 1, 0, 0, 1, 1] and k = 2, we can flip the two middle 0s to get [0, 1, 1, 1, 1, 1, 1], resulting in a longest sequence of 6 consecutive 1s. Algorithm Approach We'll use the sliding window technique to solve ...
Read MoreProgram to find length of longest strictly increasing then decreasing sublist in Python
Suppose we have a list of numbers called nums. We have to find the length of the longest sublist (minimum length 3) whose values are strictly increasing and then strictly decreasing. So, if the input is like nums = [8, 2, 4, 6, 3, 1], then the output will be 5, as the sublist [2, 4, 6, 3, 1] is strictly increasing then decreasing. Algorithm To solve this, we will follow these steps − Initialize variables: i = 0, n = size of array, res = -infinity ...
Read MoreProgram to find length of longest sign alternating subsequence from a list of numbers in Python
Suppose we have a list of numbers called nums, we have to find the length of longest subsequence that flips sign on each consecutive number. A sign alternating subsequence means each consecutive pair of numbers has opposite signs (positive followed by negative, or negative followed by positive). So, if the input is like nums = [1, 3, -6, 4, -3], then the output will be 4, as we can pick [1, -6, 4, -3]. Algorithm To solve this, we will follow these steps − pos := 0, neg := 0 ...
Read MoreProgram to find length of longest interval from a list of intervals in Python
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 MoreProgram to find length of longest Fibonacci subsequence from a given list in Python
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 MoreProgram to find longest number of 1s after swapping one pair of bits in Python
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 MoreProgram to find union of two given linked lists in Python
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 MoreProgram to convert linked list to zig-zag binary tree in Python
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 MoreProgram to arrange linked list nodes based on the value k in Python
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 MoreProgram to find folded list from a given linked list in Python
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