Server Side Programming Articles - Page 1901 of 2650

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

442 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

505 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

308 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 * in place of characters for reading passwords in C

sudhir sharma
Updated on 03-Feb-2020 10:20:11

646 Views

In this problem, we are given a string password. Our task is to print * in place of characters of the password.Let’s take an example to understand the problem,Input: password Output ********To solve this problem, we will traverse the password we have entered and print * in place of characters of the password.ExampleThe below program will show the implementation of our solution Live Demo#include #include int main() {    char password[50] = "password";    int length = strlen(password);    printf("Password : ");    for(int i = 0; i

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

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

435 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

251 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

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

Word Search in Python

SaiKrishna Tavva
Updated on 14-Oct-2024 14:15:08

6K+ Views

In Python word search refers to determining if a given word exists in the grid, this can be done using various approaches like DFS method and backtracking algorithm, etc, and the given word can be formed by sequentially connecting adjacent cells both horizontally or vertically. Step Involved The steps involved in performing word search in Python are as follows. Consider the input board(2D grid) Define the class ... Read More

Subsets in Python

Arnab Chakraborty
Updated on 04-May-2020 06:14:43

1K+ 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. So if the set is like [1, 2, 3], then the power set will be [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]Let us see the steps −We will solve this using recursive approach. So if the recursive method name is called solve(), and this takes the set of numbers (nums), temporary set (temp), res and indexThe solve() function will work like below −if index = length of nums, then create ... Read More

Advertisements