Server Side Programming Articles

Page 1413 of 2109

Convert the array such that the GCD of the array becomes 1 in C++

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

In this tutorial, we will be discussing a program to convert the array such that the GCD of the array becomes one.For this we will be provided with an array and a positive integer k. Our task is to convert the array elements such that the GCD of elements is 1 while only dividing the array elements by k any number of times until the element is less than k.Example#include using namespace std; //calculating the GCD of array int calculate_gcd(int* arr, int n){    int gcd = arr[0];    for (int i = 1; i < n; i++)   ...

Read More

Convert the ASCII value sentence to its equivalent string in C++

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

In this tutorial, we will be discussing a program to convert the ASCII value sentence to its equivalent string.For this we will be provided with a string containing the ASCII codes. Our task is to convert the given string into the equivalent characters and print it back.Example#include using namespace std; //converting the ASCII sequence into //character string void convert_ASCII(string str, int len){    int num = 0;    for (int i = 0; i < len; i++) {       //appending the current digit       num = num * 10 + (str[i] - '0');       //checking if number is within range       if (num >= 32 && num

Read More

Convert the string into palindrome string by changing only one character in C++

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

In this tutorial, we will be discussing a program to convert the string into palindrome string by changing only one character.For this we will be provided with a string. Our task is to convert the given string into a palindrome by changing only one character.Example#include using namespace std; //checking if conversion to palindrome //is possible bool if_palindrome(string str){    int n = str.length();    //counting number of characters    //to be changed    int count = 0;    for (int i = 0; i < n/2; ++i)       if (str[i] != str[n - i - 1])          ++count;    return (count

Read More

Convert the undirected graph into directed graph such that there is no path of length greater than 1 in C++

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

In this tutorial, we will be discussing a program to convert the undirected graph into a directed graph such that there is no path of length greater than 1.For this we will be provided with an undirected graph. Our task is to convert that graph into a direct one given no path has a length greater than 1.Example#include using namespace std; #define N 100005 //storing the graph vector gr[N]; //storing colour of each vertex int colour[N]; vector edges; bool bip; //adding edges to the graph void add_edge(int x, int y){    gr[x].push_back(y);    gr[y].push_back(x);    edges.push_back(make_pair(x, y)); } //checking ...

Read More

Print all pairs of anagrams in a given array of strings in C++

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

In this problem, we are given an array of strings and we have to print all pairs of anagrams of that given array.Anagrams are strings that are formed by rearranging the character of another string. Like − hello and lolheLet’s take an example to understand the problem −Input: array = {“hello”, “hrdef”, “from”, “lohel”, “morf”}. Output: [hello, lohel] , [from , morf]To solve this problem, we will use the nesting of loops. We need two nested loops, the outer loop will traverse over the array and select elements. The nested loop will check each of the string and check if ...

Read More

Print all pairs in an unsorted array with equal sum in C++

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

In this problem, we have an unsorted array and we have to print all pairs within this array that have an equal sum.Let’s take an example to understand the problem −Input: array = [12, 13, 20, 5] Output: [12, 13] and [20, 5] have sum 25.To solve this problem, we will have to find pairs of the same sum. For this, we will check pairs for the same sum. And to avoid duplicate pairs, we will use the map.For this, we will need two maps, one to store all sum pairs and their sum and others to store all sum ...

Read More

Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++

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

In this problem, we are given a set of N numbers and a number X. And we have to print all numbers from the array whose set of prime factors is a subset of the set of prime factors of X.Let’s take an example to understand the problemInput: X= 30 , array = {2, 3, 6, 10, 12} Output : 2 3 6To solve this problem, we have to traverse elements of the array. And divide this element with gcd of (element, x). Repeat division till the gcd becomes 1. And print the remaining number.Example#include using namespace std; void ...

Read More

Print all numbers less than N with at-most 2 unique digits in C++

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

In this problem, we are given an integer N and we have printed all the number less than N with at-most 2 unique digits i.e. maximum 2 different digits can be used to create the number.Let’s take an example to understand the problem −Input: N = 17 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16To solve this problem, we will be generating all numbers that have only two unique digits. Our number generating process starts from 0 and ends when our number is equal to or greater than N. For two ...

Read More

Print all nodes in a binary tree having K leaves in C++

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

In this problem, we are given a binary tree and an integer K and we have to print all nodes of the binary tree that have K leaves in their child subtree.The binary tree is a special tree whose each node has at max two nodes (one/two/none).The leaf node of a binary tree is the node at end of the tree.Let’s take an example to understand the problem −K = 2Output − {S}To solve this problem, we will do traversal (postorder) for the tree. Now, we will see each left subtree and right subtree if the sum of leaves is ...

Read More

Print all n-digit strictly increasing numbers in C++

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

In this problem, we are given a number N and we have to print all n-digit numbers whose digits are strickly increasing from MSB to LSB i.e. the number at LSB (left) should be smaller than the number at right.Let’s take an example to understand the problem −Input − n = 2Output −01 02 03 04 05 06 07 08 09 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47 48 49 56 57 58 59 67 68 69 78 79 89.Explanation − as you ...

Read More
Showing 14121–14130 of 21,090 articles
Advertisements