Found 7347 Articles for C++

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

sudhir sharma
Updated on 22-Jan-2020 09:42:05

207 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 pairs of anagrams in a given array of strings in C++

sudhir sharma
Updated on 22-Jan-2020 09:37:31

361 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

Convert time from 24 hour clock to 12 hour clock format in C++

Ayush Gupta
Updated on 22-Jan-2020 07:49:52

698 Views

In this tutorial, we will be discussing a program to convert time from 24 hour clock to 12 hour clock format.For this we will be provided with certain time in 24 hour format. Our task is to convert it into 12 hour format with the extension of “AM” or “PM”.Example Live Demo#include using namespace std; //converting into 12 hour format void convert_12hour(string str){    int h1 = (int)str[0] - '0';    int h2 = (int)str[1] - '0';    int hh = h1 * 10 + h2;    //finding the extension    string Meridien;    if (hh < 12) {       Meridien = "AM";    }    else       Meridien = "PM";       hh %= 12;    if (hh == 0) {       cout

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

Ayush Gupta
Updated on 22-Jan-2020 07:45:37

140 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 Live Demo#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)); } ... Read More

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

Ayush Gupta
Updated on 22-Jan-2020 07:42:15

289 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 Live Demo#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

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

Ayush Gupta
Updated on 22-Jan-2020 07:35:38

635 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 Live Demo#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

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

Ayush Gupta
Updated on 22-Jan-2020 07:28:31

222 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 Live Demo#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 Ternary Expression to a Binary Tree in C++

Ayush Gupta
Updated on 22-Jan-2020 07:23:09

175 Views

In this tutorial, we will be discussing a program to convert ternary expression to a binary tree.For this we will be provided with a ternary expression. Our task is to convert the given expression in the form of a binary tree depending upon the various paths(choices) possible.Example Live Demo#include using namespace std; //node structure of tree struct Node {    char data;    Node *left, *right; }; //creation of new node Node *newNode(char Data){    Node *new_node = new Node;    new_node->data = Data;    new_node->left = new_node->right = NULL;    return new_node; } //converting ternary expression into binary tree Node ... Read More

Convert String into Binary Sequence in C++

Ayush Gupta
Updated on 22-Jan-2020 07:18:22

849 Views

In this tutorial, we will be discussing a program to convert string into Binary sequence.For this we will be provided with a string of characters. Our task is to convert each of the character into its binary equivalent and print it out spaced between for different characters.Example Live Demo#include using namespace std; //converting into binary equivalent void convert_binary(string s){    int n = s.length();    for (int i = 0; i 0){          (val % 2)? bin.push_back('1') :          bin.push_back('0');          val /= 2;       }       reverse(bin.begin(), bin.end());       cout

Convert singly linked list into XOR linked list in C++

Ayush Gupta
Updated on 22-Jan-2020 07:13:44

175 Views

In this tutorial, we will be discussing a program to convert a singly linked list into XOR linked list.For this we will be provided with a singly linked list. Our task is to take the elements of that list and get it converted into a XOR linked list.Example Live Demo#include using namespace std; //node structure of linked list struct Node {    int data;    struct Node* next; }; //creation of new node Node* newNode(int data){    Node* temp = new Node;    temp->data = data;    temp->next = NULL;    return temp; } //printing singly linked list void print(Node* ... Read More

Advertisements