Programming Articles

Page 1415 of 2547

Regular expression "[X?+] " Metacharacter Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 241 Views

The Possessive Quantifier [X?+] matches the X present once or not present at all.Examplepackage com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PossesiveQuantifierDemo {    private static final String REGEX = "T?+";    private static final String INPUT = "abcdTatW";    public static void main(String[] args) {       // create a pattern       Pattern pattern = Pattern.compile(REGEX);       // get a matcher object       Matcher matcher = pattern.matcher(INPUT);       while(matcher.find()) {          //Prints the start index of the match.          System.out.println("Match String start(): "+matcher.start());   ...

Read More

Mirror of n-ary Tree in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 272 Views

Problem statementGiven a Tree where every node contains variable number of children, convert the tree to its mirrorExampleIf n-ary tree is −Then it’s mirror is −Example#include using namespace std; struct node {    int data;    vectorchild; }; node *newNode(int x) {    node *temp = new node;    temp->data = x;    return temp; } void mirrorTree(node * root) {    if (root == NULL) {       return;    }    int n = root->child.size();    if (n < 2) {       return;    }    for (int i = 0; i < n; ...

Read More

Print all safe primes below N in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 195 Views

In this problem, we are given an integer N and we have to print all safe prime number whose values are less than N.A safe prime number is a prime number which can be represented as [(2*p)- 1] where p is also a prime number.Examples − 5[(2*2) +1] , 7[(2*3)+1].Let’s take a few examples to understand the problem better −Input: N = 12 Output: 5 7 11.To solve this problem, we will find all the prime numbers less than N(for this we will use Sieve of Eratosthenes). And check if the prime number is a safe prime number or not ...

Read More

Converting Roman Numerals to Decimal lying between 1 to 3999 in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 434 Views

In this tutorial, we will be discussing a program to converting roman numerals to decimal lying between 1 to 3999.For this we will be provided with a random roman numeral. Our task is to convert the given roman numeral into its decimal equivalent.Example#include using namespace std; //calculating the decimal value int value(char r){    if (r == 'I')    return 1;    if (r == 'V')    return 5;    if (r == 'X')    return 10;    if (r == 'L')    return 50;    if (r == 'C')    return 100;    if (r == 'D')    return ...

Read More

Missing even and odd elements from the given arrays in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 356 Views

Problem statementGiven two integer arrays even [] and odd [] which contains consecutive even and odd elements respectively with one element missing from each of the arrays. The task is to find the missing elements.ExampleIf even[] = {10, 8, 6, 16, 12} and odd[] = {3, 9, 13, 7, 11} then missing number from even array is 14 and from odd array is 5.AlgorithmStore the minimum and the maximum even elements from the even[] array in variables minEven and maxEvenSum of first N even numbers is N * (N + 1). Calculate sum of even numbers from 2 to minEven ...

Read More

Missing Permutations in a list in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 178 Views

Problem statementGiven a list of permutations of any word. Find the missing permutation from the list of permutations.ExampleIf permutation is = { “ABC”, “ACB”, “BAC”, “BCA”} then missing permutations are {“CBA” and “CAB”}AlgorithmCreate a set of all given stringsAnd one more set of all permutationsReturn difference between two setsExample#include using namespace std; void findMissingPermutation(string givenPermutation[], size_t permutationSize) {    vector permutations;    string input = givenPermutation[0];    permutations.push_back(input);    while (true) {       string p = permutations.back();       next_permutation(p.begin(), p.end());       if (p == permutations.front())          break;       ...

Read More

Print common characters of two Strings in alphabetical order in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 815 Views

In this programming problem, we are given two strings. And we have to find all the characters of the string that are common in both the string and we have to print these common characters in alphabetical order. And print ‘NO COMMON CHARACTERS ’ found if no common letters arise. Given that the string does not contain all lower case alphabets.Let’s take an example −Input : string1 : adsfhslf    string2 : fsrakf Output : affsExplanation − Between the two strings there are a, f, s. Hence the lexicographical output is ‘afs’.Input : string1 : abcde    string2 : glhyte ...

Read More

Print all root to leaf paths with there relative positions in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 202 Views

In this problem, we are given a binary tree. And we have to print all the paths from the root to the leaf of the tree. Also, add underscore “_” to show the relative positions of the nodes.Let’s take an example to understand the topic better −Input −Output −_ _ 3 _ 9 1 _3 9 _7 3 _ 4 _ _ 2 3 9 4 1 7 6 2 3 _ 4 6To solve this problem, we will use the concept of the vertical order of the elements of the tree.Based on this, we will print the path from ...

Read More

Convert to Strictly increasing integer array with minimum changes in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 252 Views

In this tutorial, we will be discussing a program to convert to strictly increasing integer array with minimum changes.For this we will be provided with an array. Our task is to change the elements of the array to be in strictly increasing order by minimum number of changes in the elements.Example#include using namespace std; //calculating number of changes required int remove_min(int arr[], int n){    int LIS[n], len = 0;    for (int i = 0; i < n; i++)    LIS[i] = 1;    for (int i = 1; i < n; i++) {       for (int j = 0; j < i; j++) {          if (arr[i] > arr[j] && (i-j)

Read More

Max sum of M non-overlapping subarrays of size K in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 358 Views

Problem statementGiven an array and two numbers M and K. We need to find sum of max M subarrays of size K (non-overlapping) in the array. (Order of array remains unchanged). K is the size of subarrays and M is the count of subarray. It may be assumed that size of array is more than m*k. If total array size is not multiple of k, then we can take partial last array.ExampleIf Given array is = {2, 10, 7, 18, 5, 33, 0}. N = 7, M = 3 and K = 1 then output will be 61 as subset ...

Read More
Showing 14141–14150 of 25,466 articles
Advertisements