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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Program to find length of longest consecutive sublist with unique elements in Python
Suppose we have a list of numbers called nums, where all elements are unique. We have to find the length of the longest sublist that contains consecutive elements. So, if the input is like nums = [3, 6, 7, 5, 4, 9], then the output will be 5, because the sublist [3, 6, 7, 5, 4] contains all consecutive elements from 3 to 7. Algorithm Approach To solve this, we will follow these steps − Initialize ret := 0 to store the maximum length For each starting ...
Read MoreProgram to find longest consecutive run of 1s in binary form of n in Python
Suppose we have a non-negative value n, we have to find the length of the longest consecutive run of 1s in its binary representation. So, if the input is like n = 1469, then the output will be 4, because binary representation of 1469 is "10110111101", so there are four consecutive 1s. Approach To solve this, we will follow these steps ? count := 0 while n is not same as 0, do n := n AND (n after shifting one bit to the left) count := count + 1 return count How It Works The algorithm uses bit manipulation where n & (n
Read MoreProgram to find length of longest substring with 1s in a binary string after one 0-flip in Python
Suppose we have a binary string s. We are allowed to flip at most one "0" to "1", we have to find the length of the longest contiguous substring of 1s. So, if the input is like s = "1010110001", then the output will be 4, as if we flip the zero present at index 3, then we get the string "1011110001", here length of the longest substring of 1s is 4. Algorithm To solve this, we will follow these steps ? n := size of s ans := ...
Read MoreProgram to find largest size to truncate logs to store them completely in database in Python
Suppose we have a list of numbers called logs and another value limit. Each element in logs[i] represents the size of logs generated by the i-th user. The limit represents the total size of logs we can store in our database. We need to find the largest x such that if we truncate every log in logs to be at most size x, the sum of the truncated log sizes is at most limit. If no log needs to be truncated, then simply return the largest log size. So, if the input is like logs = [500, 200, 10000, ...
Read MoreProgram to find minimum length of first split of an array with smaller elements than other list in Python
Suppose we have a list of numbers nums, we want to split the list into two parts part1 and part2 such that every element in part1 is less than or equal to every element in part2. We have to find the smallest length of part1 that is possible (not 0 length). So, if the input is like nums = [3, 1, 2, 5, 4], then the output will be 3, because we can split the list like part1 = [3, 1, 2] and part2 = [5, 4]. Algorithm Approach To solve this problem, we follow these steps ...
Read MorePython Pandas - Extract the Number of days for each element from TimeDeltaIndex
To extract the number of days for each element from a TimedeltaIndex object, use the TimedeltaIndex.days property. This property returns an Int64Index containing only the days component from each timedelta. Creating a TimedeltaIndex First, import pandas and create a TimedeltaIndex with various time duration formats − import pandas as pd # Create a TimeDeltaIndex object with different time formats tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', ...
Read MoreProgram to count number of on lights flipped by n people in Python
Suppose we have n toggle switches in a room and n people present in that room. They flip switches according to a specific pattern: Person 1 comes and flips all switches (1, 2, 3, 4, ...) Person 2 comes and flips switches that are multiples of 2 (2, 4, 6, 8, ...) Person i comes and flips switches that are multiples of i We need to find how many switches will be in the ON position after all n people have finished flipping. Understanding the Problem ...
Read MoreProgram to find lexicographically smallest lowercase string of length k and distance n in Python
Suppose we have two numbers n and k. We have to find the lexicographically smallest lowercase string of size k and distance n. The distance is the sum of the letter number in alphabet. For example, 'a' has letter number 1, 'b' has 2, 'y' has 25, 'z' has 26 and so on. So, if the input is like n = 15 k = 3, then the output will be "aam", because "aam" is the lexicographically smallest string of length 3 whose distance is 1 + 1 + 13 = 15. Algorithm To solve this, we will ...
Read MorePython Pandas - Create a DataFrame from DateTimeIndex but override the name of the resulting column
To create a DataFrame from a DateTimeIndex, use the datetimeindex.to_frame() method. The name parameter allows you to override the column name in the resulting DataFrame. Creating a DateTimeIndex First, let's create a DateTimeIndex with timezone information ? import pandas as pd # Create a DateTimeIndex with period 5, frequency 40 seconds, and timezone datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') print("DateTimeIndex...") print(datetimeindex) ...
Read MorePython Pandas - Create a DataFrame from DateTimeIndex ignoring the index
To create a DataFrame from DateTimeIndex ignoring the index, use the DateTimeIndex.to_frame() method. Set the parameter index to False to ignore the index and create a regular DataFrame column instead. Syntax DateTimeIndex.to_frame(index=True, name=None) Parameters index − Boolean value. If True (default), uses DateTimeIndex as DataFrame index. If False, creates a regular column. name − Column name for the DataFrame. If None, uses the DateTimeIndex name or defaults to 0. Creating DateTimeIndex First, let's create a DateTimeIndex with timezone and frequency ? import pandas as pd # Create DatetimeIndex ...
Read More