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
Python Articles
Page 331 of 855
Program to find length of longest fibonacci subsequence in Python
A Fibonacci-like subsequence is a sequence where each element (starting from the third) is the sum of the two preceding elements. Given a strictly increasing array, we need to find the length of the longest Fibonacci-like subsequence. Definition A sequence X₁, X₂, ..., Xₙ is Fibonacci-like if: n >= 3 Xᵢ + Xᵢ₊₁ = Xᵢ₊₂ for all valid i Algorithm We'll use dynamic programming with a dictionary to track subsequence lengths ? Convert array to set for O(1) lookups Use a Counter to store lengths of subsequences ending at each pair ...
Read MoreProgram to count number of nice subarrays in Python
Suppose we have an array called nums and another value k. We have to find the number of nice subarrays. A subarray is said to be nice if it contains exactly k odd numbers. So, if the input is like nums = [1, 1, 2, 1, 1], k = 3, then the output will be 2 because there are two subarrays [1, 1, 2, 1] and [1, 2, 1, 1] that contain exactly 3 odd numbers each. Algorithm To solve this problem, we will follow these steps − Create a list odd_indices to store indices ...
Read MoreProgram to find minimum absolute sum difference in Python
Suppose we have two positive valued arrays nums1 and nums2 of the same size. The absolute sum difference of these two arrays is the sum of |nums1[i] - nums2[i]| for each 0 [2, 2, 6], or Replace the element at index 1 with the element at index 2: [2, 8, 6] => [2, 6, 6]. Both of them get a sum difference of |2-3| + (|2-4| or |6-4|) + |6-6| = 3. Algorithm To solve this, we will follow these steps ? If nums1 is ...
Read MoreProgram to find total similarities of a string and its substrings in Python
In Python, finding the total similarities of a string with all its suffixes is a string processing problem that can be efficiently solved using the Z-algorithm. The similarity between two strings is defined as the length of the longest common prefix. For example, if we have the string "pqpqpp", its suffixes are "pqpqpp", "qpqpp", "pqpp", "qpp", "pp", and "p". The similarities with the original string are 6, 0, 3, 0, 1, and 1 respectively, giving a total sum of 11. Understanding the Z-Algorithm The Z-algorithm efficiently computes the longest common prefix between a string and each of ...
Read MoreProgram to count nice pairs in an array in Python
Suppose we have an array called nums with non-negative values. We have to find the number of nice pairs of indices present in the array. If the answer is too large, then return answer mod 10^9+7. Here a pair of indices (i, j) is said to be nice when they satisfy all of these conditions: 0 ≤ i < j < size of nums nums[i] + rev(nums[j]) is same as nums[j] + rev(nums[i]) Note: Here rev() reverses only the positive part of an integer, so if rev(564) is there, it means 465, but if rev(540) is ...
Read MoreProgram to convert hour minutes' time to text format in Python
Converting time from numerical format (hours and minutes) to text format is a common programming task. This involves translating numbers into words and applying English time conventions like "quarter past", "half past", and "to" for times after 30 minutes. Understanding the Time Format Rules The conversion follows these English time conventions − 8:00 : eight o'clock 8:01 : one minute past eight 8:10 : ten minutes past eight 8:15 : quarter past eight 8:30 : half past eight 8:40 : twenty minutes to nine 8:45 : quarter to nine 8:47 : thirteen minutes to nine ...
Read MoreProgram to check whether two sentences are similar or not in Python
Two sentences are considered similar when we can transform one sentence into another by adding words at the beginning, end, or both. This means one sentence can be a subsequence of another with additional words only at the edges. For example, if we have "we live at city Kolkata" and "city Kolkata", they are similar because we can add "we live at " to the beginning of the second sentence to get the first one. Algorithm To check if two sentences are similar, we follow these steps − Split both sentences into word lists Ensure ...
Read MoreProgram to find minimum remove required to make valid parentheses in Python
When working with parentheses validation, we often need to remove the minimum number of invalid parentheses to make a string valid. A string with parentheses is considered valid when every opening parenthesis has a corresponding closing parenthesis in the correct order. Problem Statement Given a string containing parentheses '(' and ')' along with lowercase English characters, we need to remove the minimum number of parentheses to make the string valid. A valid parentheses string satisfies these criteria: The string is empty or contains only lowercase characters, or The string can be written as AB (A concatenated ...
Read MoreProgram to find numbers with same consecutive differences in Python
Given a number N (length of digits) and K (absolute difference), we need to find all N-digit numbers where the absolute difference between every two consecutive digits equals K. Numbers cannot have leading zeros except for the single digit 0. For example, if N = 4 and K = 7, valid numbers include 1818 (|1-8|=7, |8-1|=7, |1-8|=7) and 2929, but 0707 is invalid due to the leading zero. Algorithm Approach We use a breadth-first search (BFS) approach with a queue ? If N = 1, return all single digits [0, 1, 2, ..., 9] Initialize ...
Read MoreProgram to find how many swaps needed to sort an array in Python
Finding the minimum number of swaps needed to sort an array is a classic problem. We need to consider both ascending and descending order, then return the minimum swaps required. So, if the input is like nums = [2, 5, 6, 3, 4], then the output will be 2 because initially nums has [2, 5, 6, 3, 4]. If we swap numbers 6 and 4, the array will be [2, 5, 4, 3, 6]. Then, if we swap the numbers 5 and 3, the array will be [2, 3, 4, 5, 6]. So 2 swaps are needed to make ...
Read More