Found 26504 Articles for Server Side Programming

Lagrange’s four square theorem in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:11:26

403 Views

In this tutorial, we are going to learn about largrange's four square theorem.The lagranges's four square theorem states that every natural number can be written as sum of squares of 4 numbers.The following code finds the 4 numbers which satisfies the above condition for the given number n.ExampleLet's see the code. Live Demo#include using namespace std; void printSquareCombinations(int n) {    for (int i = 0; i * i

kth smallest/largest in a small range unsorted array in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:10:55

130 Views

In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.Let's see the steps to solve the problem.Initialise the array and k.Sort the array using sort method.Return the value from the array with the index k - 1.Let's see the code.Example Live Demo#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) {    sort(arr, arr + n);    return arr[k - 1]; } int main() {    int arr[] = { 3, 5, 23, 4, 15, 16, 87, 99 }, k = 5;    cout

Kth smallest element after every insertion in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:10:29

238 Views

In this tutorial, we are going to find the k-th smallest element after every insertion.We are going to use the min-heap to solve the problem. Let's see the steps to complete the program.Initialise the array with random data.Initialise the priority queue.Till k - 1 there won't be any k-th smallest element. So, print any symbol u like.Write a loop that iterates from k + 1 to n.Print the root of the min-heap.If the element is greater than the root of the min-heap, then pop the root and insert the element.ExampleLet's see the code. Live Demo#include using namespace std; void findKthSmallestElement(int ... Read More

Kth prime number greater than N in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:10:07

292 Views

In this tutorial, we are going to write a program that finds the k-th prime number which is greater than the given number n.Initialise the number n.Find all the prime numbers till 1e6 and store it in a boolean array.Write a loop that iterates from n + 1 to 1e6.If the current number is prime, then decrement k.If k is equal to zero, then return i.Return -1.ExampleLet's see the code. Live Demo#include using namespace std; const int MAX_SIZE = 1e6; bool prime[MAX_SIZE + 1]; void findAllPrimes() {    memset(prime, true, sizeof(prime));    for (int p = 2; p * p ... Read More

How to set the range of Y-axis for a Seaborn boxplot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 13:12:01

10K+ Views

To set the range of Y-axis for a Seaborn boxplot, we can take the following steps −Using set_style() method, set the aesthetic style of the plots.Load the dataset using load_dataset("tips"); need Internet.Using boxplot(), draw a box plot to show distributions with respect to categories.To set the range of Y-axis, use the ylim() method.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.boxplot(x="day", y="total_bill", data=tips) plt.ylim(5, 50) plt.show()OutputRead More

Kth odd number in an array in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:09:45

341 Views

In this tutorial, we are going to write a program that finds the k-th odd number from the given array.Let's see the steps to solve the problem.Initialise the array and k.Iterate over the array.If the current element is odd, then decrement the value of k.If k is 0, then return the current element.Return -1.ExampleLet's see the code. Live Demo#include using namespace std; int findKthOddNumber(int arr[], int n, int k) {    for (int i = 0; i

Writing numerical values on the plot with Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 13:09:22

922 Views

To write numerical values on the plot, we can take the following steps −Create points for x and y using numpy.Create labels using xpoints.Use the scatter() method to scatter the points.Iterate labels, xpoints and ypoints and annotate the plot with label, x and y with different properties.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 xpoints = np.linspace(1, 10, 25) ypoints = np.random.rand(25) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints):    plt.annotate(   ... Read More

How to change the color of a line using radiobuttons in Matplotlib?

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 13:09:01

393 Views

To change the color of a line using radiobutton we can take following steps −Create x and y data points using numpy.Adjust the figure size and padding between and around the subplots.Create a figure and a set of subplots using subplots() method.Plot curve with x and y data points using plot() method.Add an axes to the current figure and make it the current axes, using axes() method.Add a radio button to the current axes.Change the color of the curve with radion button using change_color() method, that can be passed in on_clicked() method.To display the figure use show() method.Exampleimport numpy as ... Read More

How to zoom a portion of an image and insert in the same plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 13:08:33

4K+ Views

To zoom a portion of an image and insert in the same plot, we can take the following steps −Create x and y points, using numpy.To zoom a part of an image, we can make data for x and y points, in that range.Plot x and y points (Step 1), using the plot() method with lw=2, color red and label.Use the legend() method to place text for the plot, Main curve.Create the axes using the axes() method by putting the rectangle’s coordinate.Plot x and y points (Step 2), using the plot() method with lw=1, color='green' and label, i.e., subpart of ... Read More

Adding extra axis ticks using Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 13:08:02

14K+ Views

To add extra ticks in matplotlib, we can take the following Steps −Create x and y points using numpy.Plot x and y points over the plot, where x ticks could be from 1 to 10 (100 data points) on the curve.To add extra ticks, use xticks() method and increase the range of ticks to 1 to 20 from 1 to 10.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, 100) y = np.sin(x) plt.plot(x, y) plt.xticks(range(1, 20)) plt.show()OutputRead More

Advertisements