The article describes a method to calculate the closest power of 2 for the frequency of each digit in a given number. The term "frequencies" refers to the count of occurrences of each unique digit in the number. Problem Statement Determine the occurrence count of each digit in a positive integer N. Then, for each digit, find the nearest power of 2 to its frequency. If there are two nearest powers of 2 for any frequency, print the larger one. Example Input n = 677755 Output 5 -> 2 6 -> 1 7 -> 4 ... Read More
The following article discusses an approach to count the maximum occurrences of a string s2 in another string s1 after they have been concatenated N1 and N2 times respectively. This is an interesting type of pattern searching problem. In this article we have employed a relatively intuitive solution approach. Problem Statement The task is to determine the maximum number of non-overlapping occurrences of string s2 within string s1. The strings are concatenated multiple times: s1 is repeated n1 times, and s2 is repeated n2 times. Example Input s1 = “abc”, s2 = “ac”, n1 = 4, n2 ... Read More
A string is a period of K if and only if it repeats itself every K characters. For example, the string "abcabcabc" is a period of 3, because it repeats itself every 3 characters. The string "abcabc?abc" is not a period of 3, because the character '?' does not repeat itself every 3 characters. Problem Statement Given a string "str" containing N lowercase characters and a positive integer K, the objective is to replace every occurrence of the character '?' within the string "str" with a lowercase alphabet such that the resulting string forms a period of length ... Read More
Lexicographic ordering refers to a method of comparing sequences of elements, akin to the ordering of words in a dictionary. The comparison process involves evaluating the first element of each sequence. If the first element of the first sequence is considered smaller than the first element of the second sequence, the first sequence is considered lexicographically less than the second sequence. Conversely, if the first element of the first sequence is deemed larger than the first element of the second sequence, the first sequence is lexicographically greater than the second sequence. Problem Statement The objective is to generate ... Read More
Any number larger than 1 is said to be prime if it cannot be written as the product of two smaller natural numbers except 1 and the number itself. For instance, 5 is prime since its only product forms, 1 5 and 5 1, both involve 5. Primes play a crucial role in number theory as stated by the prime number theorem, which asserts that any natural number greater than 1 is either a prime itself or can be expressed as a unique product of prime numbers. This theorem highlights the significance of prime numbers in the realm of mathematics. ... Read More
The following article discusses an efficient approach to compute the total time taken to type out a word using a single-row keyboard. This is an interesting problem and has been asked previously in technical interviews. Problem Statement Consider a hypothetical scenario wherein all the keys on a keyboard are placed in a single row. The first key is indexed 0 and the last key is indexed 25. For a given string ‘s’ calculate the time taken to type all the characters of ‘s’ on a special keyboard with layout specified as ‘keyboard_layout’. The time taken ... Read More
Right rotating an array means shifting its elements to the right by a certain number of positions. In a single right rotation, the last element of the array becomes the first element, and the remaining elements are shifted to the right. Problem Statement The goal is to find the Mth element of an array after performing K right rotations, where K and M are non-negative integers and the array contains N elements. Sample Examples Input arr = [12 34 56 21], K = 2, M = 1 Output 56 Explanation Arr after K right ... Read More
Plotly supports to the range on both X and Y axis. Let us understand how to set the range of Y-axis in Plotly. plotly.graph_objects is used to generate figures. It contains a lot of methods to customize charts and render a chart in HTML format. Create a numpy module and generate random ranges for both X and Y axis. Create Figure() method to plot X and Y axis with mode as lines Create update_layout() method and set the Y-axis range. Follow the steps given to set the range of Y-axis in Plotly. Step 1 − Import plotly Import ... Read More
A UNIX timestamp is the total number of seconds that have been counted since the epoch. An epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch. In this article, we going to see how to convert a python date to a UNIX timestamp. Datetime to UNIX timestamp Here we convert a python date to a UNIX timestamp by using the time.mktime() method. In this example, we initially imported the datetime module. ... Read More
Turtle is a special feature of Python. Using Turtle, we can easily draw in a drawing board.First, we import the turtle module. Then create a window, we create a turtle object, and using the turtle() method we can draw on the drawing board. Some turtle method METHOD PARAMETER DESCRIPTION Turtle() None It creates and returns a new turtle object forward() amount It moves the turtle forward by the specified amount backward() amount It moves the turtle backward by the specified amount right() angle It turns the turtle clockwise ... Read More