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 291 of 2109
Program to find longest substring of all vowels in order in Python
A beautiful substring contains all five vowels (a, e, i, o, u) in alphabetical order, with each vowel appearing at least once. We need to find the longest such substring in a given string. Problem Definition A string is beautiful if it satisfies these conditions − Each of the 5 vowels must appear at least once in it Letters must be sorted in alphabetical sequence For example, in string "aaioaaaaeiiouuooaauu", the substring "aaaaeiiouu" is beautiful and has length 10. Algorithm We use a two-pointer sliding window approach − Initialize vowels list ...
Read MoreProgram to find maximum ice cream bars in Python
Suppose we have an array costs with n elements, where costs[i] is the price of the ith ice cream bar in coins. We initially have c number coins to spend, and we want to buy as many ice cream bars as possible. We have to find the maximum number of ice cream bars we can buy with c coins. So, if the input is like costs = [3, 1, 4, 5, 2], c = 10, then the output will be 4 because we can buy ice cream bars at indices 1, 4, 0, 2 (after sorting: costs [1, 2, ...
Read MoreProgram to find maximum XOR for each query in Python
Suppose we have a presorted array called nums of size n and a value m. We want to perform the following query n times: Search for a non-negative value k < 2^m such that XOR of all elements in nums and k is maximized. So k is the answer to the ith query. Remove the last element from the current array nums. We have to find an array answer, where answer[i] is the answer to the ith query. Example Walkthrough If the input is like nums ...
Read MoreProgram 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 More