K-th Greatest Element in a Max Heap in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:34:24

1K+ Views

In this tutorial, we are going to write a program that finds the k-th largest element from the max-heap.We will use priority queue to solve the problem. Let's see the steps to complete the program.Initialise the max-heap with correct values.Create a priority queue and insert the root node of the max-heap.Write a loop that iterates k - 1 times.Pop the greatest element from the queue.Add the left and right nodes of the above node into the priority queue.The greatest element in priority queue is the k-th greatest element now.Return it.ExampleLet's see the code. Live Demo#include using namespace std; struct Heap ... Read More

Get the List of Figures in Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:33:51

1K+ Views

To get the list of figures in matplotlib, we can take the following steps −Using figure() method, create a new figure, or activate an existing figure. Creating x figures, i.e., x=3.To get the list of figures, use the plt.get_fignums() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.figure() plt.figure() plt.figure() print("Number of figures created: ", len(plt.get_fignums())) plt.show()OutputNumber of figures created: 3

Get Color of Most Recent Plotted Line in Python

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:33:07

4K+ Views

To get the color of the most recent plotted line, we can take the following steps −Create x and y points using numpy.Plot the line using x and y, with color red and linewidth 2.To get the color of the line, use the get_color() method, and print it.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 1000) y = np.linspace(10, 20, 1000) line, = plt.plot(x, y, c="red", lw=2) print("Color of the most recent plot line: ", line.get_color()) plt.show()OutputColor of the most ... Read More

Log Scale Tick Label Number Formatting in Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:32:04

756 Views

To set the log scale tick label number on the axes, we can take the following steps −Set x and y axis limits (1 to 100), using ylim and xlim, on both the axes.Using loglog() method, make a plot with log scaling on both the x and y axis.To display the figure, use the plot() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.ylim(1, 100) plt.xlim(1, 100) plt.loglog() plt.show()Output

Plot WAV File Using Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:31:42

6K+ Views

To plot a .wav file using matplotlib, we can take following the steps −To read a .wav file, we can use the read() method.After reading the .wav file, we will get a tuple. At the 0 th index, rate would be there and at the 1st index, array sample data.Use the plot() method to plot the .wav file.Set y and x labels using ylabel and xlabel with “Amplitude” and “Time” label, respectively.To display the figure, use the show() method.Examplefrom scipy.io.wavfile import read import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True input_data = read("my_audio.wav") audio = input_data[1] plt.plot(audio[0:1024]) plt.ylabel("Amplitude") plt.xlabel("Time") plt.show()OutputRead More

Increase Distance Between Title and Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:31:23

5K+ Views

To increase the distance between the title and the plot in matplotlib, we can take the following steps −Create point x using numpy.Create point y using numpy sin.Set the title of the plot. After changing the value y (in argument), we can increase or decrease the distance between the title and the plot.Plot x and y points using the plot() method, where color is red and line width is 2.Ti display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 1000) y = np.sin(x) ttl = ... Read More

Write LaTeX Formula in Plot Legend using Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:29:47

5K+ Views

LaTeX is a typesetting language for producing scientific documents. We use a very small part of the language for writing mathematical notation. Jupyter notebook recognizes LaTeX code written in markdown cells and renders the symbols in the browser using the MathJax JavaScript library.To write a LaTeX formula in the legend of a plot, we can take the following steps −Create data points for x.Create data point for y, i.e., y=sin(x).Plot the curve x and y with LaTex representation.To activate the label, use the legend() method.To display the figure, use the show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Draw Grid Lines Behind Matplotlib Bar Graph

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:29:26

3K+ Views

To draw grid lines behind matplotlib bar graph, we can take the following Steps −Make a list of numbers, i.e., data.Make a bar using the bar() method, by passing data, color='red' and alpha = 0.5. The alpha blending value should be between 0 (transparent) and 1 (opaque).To configure the grid lines, use the grid() method, with color='yellow', linewidth=1, axis='both' and alpha=0.5.To display the figure, show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [3, 5, 9, 15, 12] plt.bar(range(len(data)), data, color='red', alpha=0.5) plt.grid(color='yellow', linewidth=1, axis='both', alpha=0.5) plt.show()OutputRead More

K-th Element of Two Sorted Arrays in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:28:57

169 Views

In this tutorial, we are going to write a program that finds the k-th element from thee merged array of two sorted arrays.Let's see the steps to solve the problem.Initialise the two sorted arrays.Initialise an empty array of size m + n.Merge the two arrays into the new array.Return the k - 1 element from the merged array.ExampleLet's see the code. Live Demo#include using namespace std; int findKthElement(int arr_one[], int arr_two[], int m, int n, int k) {    int sorted_arr[m + n];    int i = 0, j = 0, index = 0;    while (i < m && ... Read More

K-th Digit in a Raised to Power B in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:27:43

334 Views

In this tutorial, we are going to write a program that finds the k-th digit from the right side in the number abIt's a straightforward problem. Let's see the steps to solve it.Initialise the numbers a, b, and k.Find the value of abusing pow method.Write a loop that iterates until power value is less than zero or count is less than k.Get the last digit from the power value.Increment the counter.Check whether k and counter are equal or not.Return the digit if they are equalReturn -1.ExampleLet's see the code. Live Demo#include using namespace std; int getTheDigit(int a, int b, int ... Read More

Advertisements