K-th Smallest and Largest Element Using STL in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:58:23

853 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.Initialise a empty ordered set.Iterate over the array and insert each element to the array.Iterate over the set from 0 to k - 1.Return the value.ExampleLet's see the code. Live Demo#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) {    set set;    for (int i = 0; i < n; i++) {       set.insert(arr[i]);    }    auto it = set.begin();    for (int ... Read More

K-th Smallest and Largest Element in Unsorted Array in C++

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

231 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.ExampleLet's see the code. 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[] = { 45, 32, 22, 23, 12 }, n = 5, k = 3;    cout

Plot a Time Series in Python

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

3K+ Views

To plot a time series in Python using matplotlib, we can take the following steps −Create x and y points, using numpy.Plot the created x and y points using the plot() method.To display the figure, use the show() method.Exampleimport matplotlib.pyplot as plt import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)]) y = np.random.randint(100, size=x.shape) plt.plot(x, y) plt.show()Output

K-th Least Element in a Min-Heap in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:37:03

532 Views

In this tutorial, we are going to write a program that finds the k-th least element from the min-heap.We will use priority queue to solve the problem. Let's see the steps to complete the program.Initialise the min-heap with correct values.Create a priority queue and insert the root node of the min-heap.Write a loop that iterates k - 1 times.Pop the least 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

K-th Boom Number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:36:39

233 Views

In this tutorial, we are going to write a program that finds the k-th boom number.The number that contains only 2 and 3 are called boom number.Let's see the steps to solve the above problem.Initialise the value of k.Initialise a queue of strings.Push the empty string to the queue.Initialise a counter variable to 0.Write a loop that iterates till counter is less than or equal to the given k.Get the front of the queue.Pop the element from the queue.Store the front of the queue in a variable.Push the number after appending 2 to the front.Increment the counter and check whether ... Read More

K-th Smallest Element After Removing Integers from Natural Numbers in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:36:15

257 Views

In this tutorial, we are going to write a program that finds out the smallest element after removing some integers from the natural numbers.We have given an array of elements and k value. Remove all the elements from natural numbers that are present in the given array. And then find the k-th smallest number from the remaining natural numbers.Let's see the steps to solve the problem.Initialise the array and k.Initialise an array and initialise all the elements with 0 except the elements present in the given array.Write a loop that iterates till the size of the given array.Decrement the value ... Read More

Plot Multiple Columns of Pandas DataFrame on Bar Chart in Matplotlib

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

643 Views

To plot multiple columns of a Pandas dataframe on the bar chart in matplotlib, we can take the following steps −Make a dictionary of different keys, between 1 to 10 range.Make a dataframe using Pandas dataframe.Create a bar plot, using the plot() method with kind=”bar”.To display the figure, use the show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True d = {    'y=1/x': [1 / i for i in range(1, 10)],    'y=x': [i for i in range(1, 10)],    'y=x^2': [i * i for i in range(1, 10)],    'y=x^3': [i ... Read More

K-th Missing Element in Sorted Array in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:35:55

297 Views

In this tutorial, we are going to write a program that finds out the k-th missing element in the given sorted array.Find the k-th number that is missing from min to max in the given unsorted array. Let's see the steps to solve the problem.Initialise the sorted array.Initialise two variables difference and count with k.Iterate over the array.If the current element is not equal to the next element.Find the difference between the two numbers.If the difference is greater than or equal to k, then return current element plus count.Else subtract difference from the count.Return -1.ExampleLet's see the code. Live Demo#include ... Read More

K-th Missing Element in an Unsorted Array in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:35:02

176 Views

In this tutorial, we are going to write a program that finds out the k-th missing element in the given unsorted array.Find the k-th number that is missing from min to max in the given unsorted array. Let's see the steps to solve the problem.Initialise the unsorted array.Insert all the elements into a set.Find the max and min elements from the array.Write a loop that iterates from min to max and maintain a variable for the count.If the current element is present in the set, then increment the count.If the count is equal to k, then return i.ExampleLet's see the ... Read More

Hide Ticks Label in Python While Keeping Ticks in Place

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:34:58

988 Views

To hide ticks label and keep the ticks in place, we can take the following steps −Initialize x1 and x10 variables to get the x and y points, using numpy.Plot points x and y using the plot() method.Using xticks method, get or set the current tick locations and labels of the X-axis. Pass no arguments to return the current values without modifying them. So, pass the range(x1, x10) to get ticks but pass an empty list to hide the labels.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] ... Read More

Advertisements