Program to check we can rearrange array to make difference between each pair of elements same in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:08:19

275 Views

Suppose we have a list called nums, we have to check whether we can rearrange the order of nums in such a way that the difference between every pair of consecutive two numbers is same. This is essentially checking if the array can form an arithmetic progression. So, if the input is like nums = [8, 2, 6, 4], then the output will be True, because if we rearrange nums like [2, 4, 6, 8], then the difference between every two pair of consecutive numbers is 2. Algorithm To solve this, we will follow these steps − N := size of nums if N

Program to add one to a number that is shown as a digit list in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:07:59

809 Views

Suppose we have an array called nums, containing decimal digits of a number. For example, [2, 5, 6] represents 256. We have to add 1 to this number and return the list in the same format as before. So, if the input is like nums = [2, 6, 9], then the output will be [2, 7, 0] (269 + 1 = 270). Algorithm Steps To solve this, we will follow these steps − i := size of nums - 1 while i >= 0, do if nums[i] + 1 = 0: if nums[i] + 1 = 0: if nums[i] + 1

Program to count number of 5-star reviews required to reach threshold percentage in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:07:44

897 Views

Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent. So, if the input is like reviews = [[3, 4], [1, 2], [4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% ... Read More

Program to count number of similar substrings for each query in Python

Arnab Chakraborty
Updated on 26-Mar-2026 15:07:23

444 Views

Finding similar substrings based on character pattern matching is a common string processing problem. Two substrings are similar if they have the same length and maintain the same character relationship pattern. This means if characters at positions i and j are equal in one substring, they must be equal in the other substring too. Understanding Similar Substrings Two strings are similar if they follow these rules − They are of same length For each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] ... Read More

Program to create a lexically minimal string from two strings in python

Arnab Chakraborty
Updated on 26-Mar-2026 15:06:58

227 Views

Creating a lexically minimal string from two strings involves comparing suffixes and selecting characters that produce the smallest lexicographical result. We compare remaining portions of both strings and choose the character from the string with the lexicographically smaller suffix. So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALS. Algorithm Steps If we compare the two strings step-by-step ? TUTORIALS vs POINT TUTORIALS vs OINT → P (choose from input_2) TUTORIALS vs INT → O (choose from input_2) TUTORIALS vs NT → I (choose from input_2) ... Read More

Program to find out the number of shifts required to sort an array using insertion sort in python

Arnab Chakraborty
Updated on 26-Mar-2026 15:06:37

1K+ Views

Insertion sort moves elements one position at a time to place them in their correct sorted position. Each movement is called a shift. We need to count the total number of shifts required to sort an array using insertion sort. The key insight is that the number of shifts for each element equals the number of larger elements that appear before it in the array. This is called the inversion count. Example Walkthrough Given array [4, 5, 3, 1, 2], let's trace the insertion sort process ? Step 1: [4, 5, 3, 1, 2] → ... Read More

When is plt.Show() required to show a plot and when is it not?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 15:06:16

3K+ Views

When working with Matplotlib plots, understanding when to use plt.show() is crucial. The requirement depends on your environment — whether you're in an interactive Python session, a Jupyter notebook, or running a script. Interactive vs Non-Interactive Environments In interactive environments like Jupyter notebooks or IPython, plots often display automatically without calling plt.show(). In non-interactive environments like regular Python scripts, you must call plt.show() to display the plot. Basic Example Without plt.show() In Jupyter notebooks, this code displays the plot automatically: import matplotlib.pyplot as plt import numpy as np x = np.linspace(-5, 5, 100) ... Read More

How to remove the axis tick marks on a Seaborn heatmap?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 15:05:57

6K+ Views

To remove the axis tick marks on a Seaborn heatmap, you can use the tick_params() method to customize the appearance of ticks and tick labels. This is useful when you want a cleaner visualization without the small lines indicating tick positions. Basic Heatmap with Tick Marks First, let's create a basic heatmap to see the default tick marks − import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Create sample data data = np.random.rand(4, 4) # Create heatmap plt.figure(figsize=(6, 4)) ax = sns.heatmap(data, annot=True, cmap='viridis') plt.title('Heatmap with Default Tick Marks') ... Read More

Make logically shading region for a curve in matplotlib

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 15:05:38

256 Views

To make logically shading region for a curve in matplotlib, we can use BrokenBarHCollection.span_where() to create conditional shading based on the curve's values. This technique is useful for highlighting regions where a function satisfies certain conditions. Steps Set the figure size and adjust the padding between and around the subplots. Create t, s1 and s2 data points using numpy. Create a figure and a set of subplots. Plot t and s1 data points; add a horizontal line across the axis. Create a collection of horizontal bars spanning yrange with a sequence of xranges. Add a Collection to ... Read More

Saving a 3D-plot in a PDF 3D with Python

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 15:05:17

5K+ Views

To save a 3D plot in a PDF with Python, you can use matplotlib's savefig() method. This approach creates standard 2D PDF files containing the 3D visualization, which is suitable for most documentation and sharing purposes. Steps Set the figure size and adjust the padding between and around the subplots. Create a new figure or activate an existing figure. Add an 'ax' to the figure as part of a subplot arrangement with 3D projection. Create u, v, x, y and z data points using numpy. Plot a 3D wireframe or surface. Set the title and labels of ... Read More

Advertisements