Found 7347 Articles for C++

K-th smallest element after removing some integers from natural numbers in C++

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

178 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

k-th missing element in sorted array in C++

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

227 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

135 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

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

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

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

105 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

250 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

k-Rough Number or k-Jagged Number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:26:56

115 Views

In this tutorial, we are going to write a program that checks whether the given number is k-rough or k-jagged number or not.The number whose smallest prime factor is greater than or equal to the given k, it is called k-rough or k-jagged number.Let's see the steps to solve the problem.Initialise the numbers n and k.Find all the prime numbers that are factors of n and store them in a vector.Get the first element from the vector and compare it with k to check whether n is k-rough or k-jagged number or not.ExampleLet's see the code. Live Demo#include using namespace ... Read More

Difference Between Function Overloading and Overriding in C++

Kiran Kumar Panigrahi
Updated on 21-Feb-2023 14:32:06

22K+ Views

In objectoriented programming, there are two important concepts of polymorphism namely, function overloading and function overriding. When two or more functions have the same name but their parameters are different, it is called function overloading. On the other hand, function overriding is one that provides a facility to redefine a function with a name and signature same as the inheriting class. Read this article to learn more about function overloading and overriding in C++ and how they are different from each other. What is Function Overloading? The concept by which we can define different function in a ... Read More

Difference Between & and && in C Programming

Kiran Kumar Panigrahi
Updated on 11-Sep-2023 12:40:35

17K+ Views

The "&" and "&&" operators are both logical AND operators in most programming languages, but they can behave differently in certain circumstances. Both "&" and "&&" are operators used for evaluating conditional statements. The most basic difference between the two is that the "&" operator is a logical as well as a bitwise operator, whereas the "&&" operator is only a logical operator. Read this article to find out more about these two operators and how they are different from each other. Let's start with a basic overview of "&" and "&&" operators. What is "&" Operator? The & operator ... Read More

Difference Between Inline and Macro in C++

AmitDiwan
Updated on 24-Mar-2021 13:24:36

766 Views

In this post, we will understand the difference between inline and macro in C++.InlineIt is a function in C++.It is parsed by the compiler.It can be defined inside or outside the class.It evaluates the argument only once.The compiler may not convert all functions to ‘inline’ function and expand them all.The short functions that are defined inside the class are automatically made as inline functions.An inline function inside a class can access the data members of the class.Inline function can be terminated using curly brackets.It is easy to debug.This is because error checking is done during compilation.It binds all statements in ... Read More

Advertisements