Found 7347 Articles for C++

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

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

299 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

601 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

78 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 subarrays with 0 sum in C++

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

199 Views

In this problem, we are given an array of integer values and we have to print all those subarrays from this array that have the sum equal to 0.Let’s take an example to understand the topic better, Input: array = [-5, 0, 2, 3, -3, 4, -1] Output: Subarray with sum 0 is from 1 to 4. Subarray with sum 0 is from 5 to 7 Subarray with sum 0 is from 0 to 7To solve this problem, we will check all subarrays possible. And check if the sum of these subarrays is equal to 0 and print them. This ... Read More

Print all subsequences of a string using Iterative Method in C++

sudhir sharma
Updated on 17-Jan-2020 10:51:55

302 Views

In this problem, we are given a string and we have to find the substring from the given string. The substring to be found should start with a vowel and end with constant character.A string is an array of characters.The substring that is to be generated in this problem can be generated by deleting some characters of the string. And without changing the order of the string.Input: ‘abc’ Output: ab, ac, abcTo solve this problem, we will iterate the string and fix vowels and check for the next sequence. Let’s see an algorithm to find a solution −AlgorithmStep 1: Iterate ... Read More

Print all subsequences of a string using ArrayList in C++

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

84 Views

In this problem, we are given a string and we have to print all subsequences of the string. The substring is formed by deleting elements. Also, the order of string should not be altered.Let’s take an example to understand the problem better −Input: string = “xyz” Output: x y xy z xz yz xyzTo solve this problem, we will find all substring starting from freezing the first character of the string and find subsequence accordingly, then going for the next character in string and subsequence.Examplepublic class Main {    public static void printSubString(String sub, String subSeq){       if ... Read More

Print all Subsequences of String which Start with Vowel and End with Consonant in C++

sudhir sharma
Updated on 17-Jan-2020 10:44:00

416 Views

In this problem, we are given a string and we have to find the substring from the given string. The substring to be found should start with a vowel and end with constant character.A string is an array of characters.The substring that is to be generated in this problem can be generated by deleting some characters of the string. And without changing the order of the string.Input: ‘abc’ Output: ab, ac, abcTo solve this problem, we will iterate the string and fix vowels and check for the next sequence. Let’s see an algorithm to find a solution −AlgorithmStep 1: Iterate ... Read More

Print all subsequences of a string in C++

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

256 Views

In this problem, we are given a string and we have to print all the subsequences of the string. The substring generated is created by deleting the elements of the string but the order remains the same(i.e. Order cannot be changed).Let’s take an example to understand the topic better −Input: xyz Output: x, y, z, xy, yz, xz, xyzExplanation − In the above example, we can see the only characters are deleted to create substring. No, rearranging takes place.There can be multiple methods to solve this problem, here we will discuss a few of them to understand methods.One is by ... Read More

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

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

661 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

137 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

Advertisements