Dev Prakash Sharma

Dev Prakash Sharma

414 Articles Published

Articles by Dev Prakash Sharma

Page 39 of 42

Update the bit in the given position or Index of a number using C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 2K+ Views

In the given problem, we have to update the bit of the given index of a number. To update the number, we can use Bit Manipulation operation on a given number. For example, Input-1 −N= 25 bit= 1 position= 2Output −29Explanation − Since the given input 25 can be written in binary as ‘11001’ whereas the position index is ‘2’ and the bit is ‘1’. After replacing the digits at the given position the output will be ‘11101’ which is equivalent to ‘29’.Approach to solve this problemIn the given position or index of a number, the task is to update ...

Read More

static_assert in C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 460 Views

static_assert is a function which is useful for programmers to print the error in the screen after compiling the program without messing up with the output too much.Earlier in C++11 and C++14, static_assert had different functionality which means we have to write our own message while defining the static_assert. However, In C++ 17 static_assert can be invoked without passing the message.It is compatible with other asserts libraries functions like BOOST_STATIC_ASSERT as well.Syntax{    auto __range= For-range-Intializer;    auto __begin= begin-expression;    auto __end= end-expression;    for(; __begin!= __end; ++__begin){       range-declaration= *__begin;       statement    } ...

Read More

Square of a sorted array in C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 2K+ Views

In the given array of sorted integers, the task is to print the squares of every array element and print the array in sorted order. For example, Input-1 −arr[ ] = { -3, -1, 0, 1, 4, 6 };Output −{0, 1, 1, 9, 16, 36}Explanation − The squares of each of the elements of the given array [-3, -1, 0, 1, 4, 6 ] is [0, 1, 1, 9, 16, 36 ].Input-2 −arr[ ]= { 0, 1, 2, 8, 9 }Output −{0, 1, 4, 64, 81}Explanation − The squares of each of the elements of the given array [ 0, ...

Read More

Sort an arrays of 0’s, 1’s and 2’s using Java

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 483 Views

Given an array of 0, 1 and 2 sort the element in an order such that all the zeros come first before 1 and all the 2’s in the end. We have to sort all the elements of the array in-place.We can solve this problem using DNF (Dutch National Flag) Sorting Algorithm. For example, Input-1 −arr[ ]= {2, 0, 0, 1, 2, 1 }Output −0 0 1 1 2 2Explanation − Sorting the given array of elements containing 0, 1 and 2 using the DNF Sorting Algorithm, it will print the output as {0, 0, 1, 1, 2, 2}.Input-2 −arr[ ...

Read More

Sort an arrays of 0’s, 1’s and 2’s using C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 1K+ Views

Given an array of 0, 1, and 2, sort the elements in an order such that all the zeros come first before 1 and all the 2’s in the end. We have to sort all the elements of the array in-place.We can solve this problem using DNF (Dutch National Flag) Sorting Algorithm. For example, Input-1 −arr[ ]= {2, 0, 0, 1, 2, 1 }Output −0 0 1 1 2 2Explanation − Sorting the given array of elements containing 0, 1 and 2 using DNF Sorting Algorithm, it will print the output as {0, 0, 1, 1, 2, 2}.Input-2 −arr[ ]= ...

Read More

Significance of Lambda Function in C/C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 4K+ Views

Lambda Function − Lambda are functions is an inline function that doesn’t require any implementation outside the scope of the main program.Lambda Functions can also be used as a value by the variable to store. Lambda can be referred to as an object that can be called by the function (called functors).Whenever the compiler encounters a definition of the lambda function, it generally creates a custom object for the lambda.A lambda function has more functionality than a normal function, for example, it has a capturing method to capture the used variables. However, the captured variable is treated as the member ...

Read More

Search an element in the given singly Linked List using C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 683 Views

Given a singly linked list, the task is to search a particular element in the linked list. If the element is found, then print “present” otherwise “Not present”. For example, Input-1 −1→ 2→ 3→ 4→ 5→ 6Searching for ‘7’Output −Not PresentExplanation − In the given singly linked list the element ‘7’ is not present, hence we will return the output as “Not Present”.Input-2 −1→ 2→ 3→ 4→ 5Searching for ‘2’Output −PresentExplanation − Since in the given Singly linked list the element ‘2’ is present thus we will return the output as “Present”.Approach to solve this problemThere are two approaches to ...

Read More

Reverse Vowels of a string in C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 2K+ Views

Given a string, the task is to reverse all the vowels present in the given string. For example, Input-1 −a = “tutor”Output −toturExplanation − Reversing the string “tutor” will generate the output as “totur.Input-2 −a = “mathematics”Output −mithametacsExplanation − Reversing the string “mathematics” will generate the output as “mithametacs”.Approach to solve this problemGiven a string, we have to reverse all the vowels present in it. There are several approaches to solve this particular problem but we have to solve this in linear time O(n).Thus, the better method to solve this problem is to use the Two-Pointer approach in which we ...

Read More

Write a program in C++ to replace all the 0’s with 5 in a given number

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 1K+ Views

Given an integer N, the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example, Input-1 −N= 1007Output −1557Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.Input-2 −N = 00105Output −155Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.Approach to Solve this ProblemTo replace all the ...

Read More

Write a program in C++ to remove duplicates from a given array of prime numbers

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 05-Feb-2021 394 Views

Let us suppose we have given an array N size which consists of all the prime numbers. The task is to find the duplicates in the given array and remove them. For example, Input-1 −N = 8 arr[ ] = { 2 ,2 ,2 ,3 ,3 ,3 ,5 ,7 }Output −2 3 5 7Explanation − In the given array of prime numbers there are some duplicates of ‘2’ and ‘3’ after removing the duplicates the output will be 2 3 5 7.Input-2 −N = 5 arr[ ] = { 3, 2, 7, 5, 5}Output −3 2 7 5Explanation − In ...

Read More
Showing 381–390 of 414 articles
Advertisements