Server Side Programming Articles - Page 1938 of 2650

Print all Semi-Prime Numbers less than or equal to N in C++

sudhir sharma
Updated on 17-Jan-2020 11:01:15

544 Views

In this problem, we are given an integer N. and we have to print all the semiprime numbers that are less than or equal to N.Before solving this problem, let’s understand what is a semi-prime number.A semi-prime number is a number whose value is the product of two distinct prime numbers.Let’s take an example, 21 = 3*7 is a semiprime number.25 = 5*5 is not a semiprime number.Now, let’s take an example of semiprime numbers less than or equal to n.Input: N = 15 Output: 6 10 14 15To solve this problem, we have to take each number less than ... Read More

Print all sequences of given length in C++

sudhir sharma
Updated on 17-Jan-2020 11:02:28

768 Views

In this problem, we are given two integer values, k, and n. And we have to print all the sequences of length k from numbers from 1 to n in sorted order.Let’s take an example to understand the topic −Input:k = 2 ; n = 3 Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3So in this problem, we have to print the sequence as stated above.A simple way to solve this problem is by incrementing integers of the sequence till they get to the max value i.e. n. The ... Read More

Print all sequences starting with n and consecutive difference limited to k in C++

sudhir sharma
Updated on 17-Jan-2020 11:01:29

128 Views

In this problem, we are given three variables n, s, and k and we have to print all the possible sequences that start with the number n and length s having the absolute difference between consecutive elements less than k.Let’s take an example to understand the topic better −Input: n = 3, s = 3 , k = 2 Output: 3 3 3 3 3 4 3 3 2 3 4 4 3 4 5 3 4 3 3 2 2 3 2 3 3 2 1In this problem, we need to obtain the absolute difference less k. For this, ... Read More

Print all subsets of given size of a set in C++

sudhir sharma
Updated on 17-Jan-2020 10:37:49

831 Views

In this problem, we are given an array and we have to print all the subset of a given size r that can be formed using the element of the array.Let’s take an example to understand the topic better −Input: array = {3, 5, 6} r = 2 Output: 3 5 3 6 5 6In this problem, we will have to find all the combinations of the numbers of the array. And exclude those r bit combinations which are already in the set.Example Live Demo#include using namespace std; void printSubset(int arr[], int n, int r, int index, int data[], int ... Read More

Print all substring of a number without any conversion in C++

sudhir sharma
Updated on 17-Jan-2020 10:28:05

223 Views

In this problem, we are given an integer n. And we have to print all substrings of a number that can be formed but converting the string are not allowed i.e. we cannot convert an integer into string or array.Let’s take an example to understand the topic better −Input: number =5678 Output: 5, 56, 567, 5678, 6, 67, 678, 7, 78, 8In order to solve this problem, we will need to use mathematical logic. Here, we will print the most significant bit first, then successive bits are printed.AlgorithmStep1: Take a 10’s power number based on the number of digits. Step2: ... Read More

Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++

sudhir sharma
Updated on 17-Jan-2020 10:14:04

211 Views

In this problem, we are given an array of N elements. And need to return all the sums of the elements are divisible by an integer M.Input : array = {4, 7, 3} ; M = 3 Output : 5+4+3 ; 5+4-3To solve this problem, we need to know the concept of a power set that can be used to find all the possible sums obtained. From this sum, print all those which are divisible by M.AlgorithmStep 1: Iterate overall combinations of ‘+’ and ‘-’ using power set. Step 2: If the sum combination is divisible by M, print them ... Read More

Print all the cycles in an undirected graph in C++

sudhir sharma
Updated on 17-Jan-2020 10:11:49

2K+ Views

In this problem, we are given an undirected graph and we have to print all the cycles that are formed in the graph.Undirected Graph is a graph that is connected together. All the edges of the unidirectional graph are bidirectional. It is also known as an undirected network.Cycle in a graph data structure is a graph in which all vertices form a cycle.Let’s see an example to understand the problem better −Graph-Output-Cycle 1: 2 3 4 5 Cycle 2: 6 7 8For this, we will make use of a few properties of the graph. You need to use graph coloring ... Read More

Print all the levels with odd and even number of nodes in it in C++

sudhir sharma
Updated on 17-Jan-2020 10:09:56

219 Views

In this problem, we are given a tree. And we have to print all the levels with even number of nodes and odd number of nodes in it.Let’s take an example to understand the concept betterOutput −Levels with odd number of nodes: 1, 3, 4 Levels with even number of nodes: 2Explanation − The first level has only 1 element(odd), 2nd level contains two elements(even), 3rd level contains 3 elements(odd) and 4th level contains 1 element(even).Now, to solve this problem. We need to find the count of nodes at each level and print the even-odd levels accordingly.We will follow the ... Read More

Print all the pairs that contains the positive and negative values of an element in C++

sudhir sharma
Updated on 17-Jan-2020 09:58:19

218 Views

In this problem, we are given an array of unique integers. And we have to return all pairs of integers(positive and negative integers) that are present in the array.Let’s take an example to understand the problem better −Input: array = {1 , 4 , 7 , -1, 2, 5, -7} Output: -11 -33An easy way to solve the problem is by using two loops and find the positive-negative pairs. But this solution will be a complex one and will have time complexity of the order n2 where n is the size of the array.But, we have to find a more ... Read More

Print all the palindromic permutations of given string in alphabetic order in C++

sudhir sharma
Updated on 17-Jan-2020 09:57:09

310 Views

In this problem, we are given a string of size n. And we have to print all possible palindromic permutation that can be generated using the characters of the string in alphabetical order. If palindrome is not created using the string print ‘-1’.Let’s take an example to understand the topic better −Input: string = “abcba” Output : abcba bacbaNow, to solve this we need to find all the palindromes possible and then arrange them in alphabetical order(lexicographical order). Or another way could be finding the lexicographically first palindrome that is made from the string. Then find the sequentially next palindrome ... Read More

Advertisements