Largest Even Number Possible by One Swap in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:21:14

735 Views

In this tutorial, we are going to write a program that finds the largest possible even number with a single swap of digits.Let's see the steps to solve the problem.Initialise the number in string format.Iterate over the given number.Find the even digit that is less than or equal to the last digit of the number.Break the loop if you find the desired even digit.If the even digit doesn't exist, then return the given number.Swap the last digit with the even digit you found in the above step.Return the numberExample Live Demo#include using namespace std; string getLargestEvenNumber(string number, int n) { ... Read More

Largest Even Digit Number Not Greater Than N in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:20:05

190 Views

In this tutorial, we are going to write a program that finds the largest number whose digits are all even and not greater than the given n.Let's see the steps to solve the problem.Initialise the number n.Write a loop from i = n .Check whether the digits of current number are all even or not.If the above condition satisfies, then print the number.Else decrement the i.ExampleLet's see the code. Live Demo#include using namespace std; int allDigitsEven(int n) {    while (n) {       if ((n % 10) % 2){          return 0;       ... Read More

Largest Even and Odd N-Digit Numbers in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:19:28

541 Views

In this tutorial, we are going to write a program that finds the largest even and odd number of n digits number.Let's see the steps to solve the problem.Initialise the number n.The largest odd number is pow(10, n) - 1.The largest even number is odd - 1.ExampleLet's see the code. Live Demo#include using namespace std; void findEvenAndOddNumbers(int n) {    int odd = pow(10, n) - 1;    int even = odd - 1;    cout

Larger of A, B or B, A in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:18:53

335 Views

In this tutorial, we are going to write a program that finds out the larger among the ab and baIt's a straightforward problem. Let's see the steps to solve it.Initialise the values of a and b.Take the log of both the values.Compute the values of $b\:\log\:a$ and $a\:\log\:b$Compare the both values.If $a\:\log\:b$ is greater than $b\:\log\:a$, then print bais greater.If $b\:\log\:a$ is greater than $a\:\log\:b$, then print abis greater.Else print both are equal.ExampleLet's see the code. Live Demo#include using namespace std; int main() {    int a = 4, b = 7;    long double x = (long double) a ... Read More

Lagrange's Interpolation in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:16:03

2K+ Views

In this tutorial, we are going to write a program that finds the result for lagranges's interpolation formula.You don't have write any logic for the program. Just convert the formula into code. Let's see the code.Example Live Demo#include using namespace std; struct Data {    int x, y; }; double interpolate(Data function[], int xi, int n) {    double result = 0;    for (int i = 0; i < n; i++) {       double term = function[i].y;       for (int j = 0; j < n; j++) {          if (j != i) ... Read More

Set Y-axis Range for 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

Lagrange's Four Square Theorem in C++

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

418 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 and Largest in a Small Range Unsorted Array in C++

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

139 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

248 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

304 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

Advertisements