Found 7347 Articles for C++

Reverse Linked List II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:24:06

179 Views

Suppose we have a linked list. We have to reverse the nodes from position m to n. We have to do it in one pass. So if the list is [1, 2, 3, 4, 5] and m = 2 and n = 4, then the result will be [1, 4, , 3, 2, 5]Let us see the steps −There will be two methods, the reverseN() and reverseBetween(). The reverseBetween() will work as main method.define one link node pointer called successor as nullThe reverseN will work as follows −if n = 1, then successor := next of head, and return headlast ... Read More

Prime Triplet in C++

sudhir sharma
Updated on 03-Feb-2020 10:33:02

272 Views

In this problem, we are given a number N. Our task is to print all prime triplets less than N.Prime triplet is a set of three prime numbers. That are of the form (p, p+2, p+6) or (p, p+4, p+6). All prime numbers are grouped according to the above triplets as every third prime in the direct pattern is a multiple of 6.Let’s see an example to understand the problemInput: N = 13 Output: 5 7 11To solve this problem, we have to find all prime numbers less than equal to N. And the check for triplets.The code to show ... Read More

Primitive root of a prime number n modulo n in C++

sudhir sharma
Updated on 03-Feb-2020 10:29:48

1K+ Views

In this problem, we are given a prime number N. our task is to print the primitive root of prime number N modulo N.Primitive root of prime number N is an integer x lying between [1, n-1] such that all values of xk (mod n) where k lies in [0, n-2] are unique.Let’s take an example to understand the problem, Input: 13 Output: 2To solve this problem, we have to use mathematical function called Euler’s Totient Function.Euler’s Totient Function is the count of numbers from 1 to n which are relatively prime to the number n.A number i is relatively ... Read More

Subsets II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:23:09

306 Views

Suppose we have a set of numbers; we have to generate all possible subsets of that set. This is also known as power set. We have to keep in mind that the elements may be duplicate. So if the set is like [1, 2, 2], then the power set will be [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]Let us see the steps −Define one array res and another set called xWe will solve this using recursive approach. So if the recursive method name is called solve(), and this takes index, one temporary array, and the array of ... Read More

Primorial of a number in C++

sudhir sharma
Updated on 03-Feb-2020 10:24:24

373 Views

In this problem, we are given a number n. Our task is to print its primorial number.Primorial number (Pn#) is a number that is the product of first n prime numbers.Primorial number is similar to factorial of a number n. The difference is that factorial can be any number but in case of primorial number, all prime numbers are used.Let’s take an example to understand the problem, Input: N = 4 Output 210 Explanation: Primorial number, Pn# = 2 * 3 * 5 * 7 = 210To solve this problem, we have to find the first n prime numbers. Print ... Read More

Remove Duplicates from Sorted List II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:21:19

229 Views

Suppose we have a list of some elements. We have to remove all elements that have occurred more than once. So only the distinct elements will remain in the list. So if the list is like [1, 1, 1, 2, 2, 3, 5, 6, 6, 7, 8], then the output will be [3, 5, 7, 8], all other elements are present more than once.Let us see the steps −Create a dummy node with value -1, prev := NULL, dummyPtr := dummywhile head is not nullif next of head is present or the value of head is not same as the ... Read More

Print Kth least significant bit of a number in C++

sudhir sharma
Updated on 03-Feb-2020 10:17:46

342 Views

In this problem, we are given two numbers n and k. Our task is to print the kth least significant bit of the number n.Let’s take an example to understand the problemInput: n = 12 , k = 3 Output 1 Explanation: Let’s see the binary representation of n: 12 = 1100Now, 3rd least significant bit is 1.To solve this problem we will use the binary bits of the number. And yield the kth bit of the number. For this, we will use binary shifting of the number and left-shift the number (k-1) times. Now on doing end operation of ... Read More

Search in Rotated Sorted Array II in C++

Arnab Chakraborty
Updated on 03-Feb-2020 10:18:57

159 Views

Consider we have an array sorted in ascending order. That is rotated at some pivot unknown to us beforehand. For example, if the array is like [0, 0, 1, 2, 2, 5, 6], this might become [2, 5, 6, 0, 0, 1, 2]. We have a target value to search. If that is found in the array, then return true, otherwise return false. So if the array is like [2, 5, 6, 0, 0, 1, 2], and target is 0, then the output will be 0Let us see the steps −low := 0 and high := size of arraywhile low ... Read More

Remove Duplicates from Sorted Array II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:17:59

1K+ Views

Suppose we have a sorted array nums, we have to remove the duplicates in-place such that duplicates elements will appear at most twice and return the new length. To do this task we cannot take extra space. We have to solve this with O(1) amount of space. For example, if the array is like [0,0,0,1,1,1,1,2,3,3], then the output will be [0,0,1,1,2,3,3], its length is 7Let us see the steps −len := 2 and n := size of arrayif n

Search a 2D Matrix in C++

Arnab Chakraborty
Updated on 04-May-2020 06:13:31

312 Views

Suppose we have write an efficient algorithm to searches for a value in one m x n matrix. This matrix has some properties like below −Each row is sorted from left to rightThe first number of each row is greater than the last integer of the previous row.So if the matrix is like −1357101116202330345053627898And if the target value is 16, then the output will be True.Let us see the steps −n := number of rows, if n is 0, then return false, m := number of columns, if m = 0, then return falselow := 0 and high := n ... Read More

Advertisements