Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 458 of 719
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
445 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
507 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
309 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
436 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
253 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
2K+ 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
438 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
207 Views
In this problem, we are given 2 arrays x[] , y[] such that (x, y) gives a cordinate of a point in 2D plane. Our task is to print all points along with their frequencies of occurence.Let’s take an example to understand the problemInput: x[]={0, 1, 1, 0, 0} ; y[]={1, 2, 2, 2, 1} Output (0, 1) = 2 (1, 2) = 2 (0, 2) = 1To solve this problem, we need to store frequency of occurence of each point. So this we need to use map data-structure. The key of the map is (x[i], y[i]), mapped value is ... Read More
284 Views
Here, we will see the code that will print a 2D matrix in c/c++ programming language without using curly braces.Curly braces are separators in a programming language that are used to define separate code blocks in the program. Without curly braces defining scopes is difficult in c/c++.Let’s see the basic code and sample output to print 2D matrix.Example Live Demo#include using namespace std; int main() { int arr[2][2] = {{12, 67}, {99, 5}}; int n = 2, m = 2; for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ cout