Found 7347 Articles for C++

Majority Element in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:37:37

1K+ Views

Suppose we have an array; we have to check whether given number x is the majority element of that array or not. The array is sorted. One element is said to be the majority element when it appears n/2 times in the array. Suppose an array is like {1, 2, 3, 3, 3, 3, 6}, x = 3, here the answer is true as 3 is the majority element of the array. There are four 3s. The size of the array is 7, so we can see 4 > 7/2.We can count the occurrences of x in the array, and ... Read More

Pascal's Triangle in C++

AmitDiwan
Updated on 08-Jan-2020 10:15:49

6K+ Views

Pascal’s triangle is an array of binomial coefficients. The top row is numbered as n=0, and in each row are numbered from the left beginning with k = 0. Each number is found by adding two numbers which are residing in the previous row and exactly top of the current cell. It is also being formed by finding (𝑛𝑘) for row number n and column number k.Suppose the input is 10, then the output will be like −               1               1 1             ... Read More

Climbing Stairs in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:04:36

513 Views

There are n stairs. One person will go to 1st to nth stairs. Maximum how many stairs he/she can cross in one step is also given. With this information, we have to find possible ways to go to the nth stairs. Let us consider one can cross a maximum two stairs in each step. So we can find recursive relations to solve this problem. One can move to nth stair, either from (n-1)th stair or from (n-2)th stair. So ways(n) = ways(n-1) + ways(n-2).Suppose the number of stairs, say 10, the maximum number of stairs that can be jumped in ... Read More

Valid Parentheses in C++

Arnab Chakraborty
Updated on 28-Apr-2020 08:40:23

5K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.The task is simple; we will use the stack to do this. We should follow these steps to get the solution −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from ... Read More

Convert all lowercase characters to uppercase whose ASCII value is co-prime with k in C++

Ayush Gupta
Updated on 06-Jan-2020 12:24:57

85 Views

In this tutorial, we will be discussing a program to convert all lowercase characters to uppercase whose ASCII value is co-prime with k.For this we will be provided with a string and an integer value k. Our task is to traverse through the given string and change to uppercase all those characters whose ASCII value is co-prime with the given integer k.Example Live Demo#include using namespace std; //modifying the given string void convert_string(string s, int k){    int l = s.length();    for (int i = 0; i < l; i++) {       int ascii = (int)s[i];   ... Read More

Convert a tree to forest of even nodes in C++

Ayush Gupta
Updated on 06-Jan-2020 12:14:35

168 Views

In this tutorial, we will be discussing a program to convert a tree to a forest of even nodes.For this we will be provided with a binary tree of say N nodes. Our task is to calculate the maximum number of edges that can be removed to get forest of even nodes.Example Live Demo#include #define N 12 using namespace std; //returning the number of nodes of subtree //having the root node int depth_search(vector tree[N], int visit[N], int *ans, int node){    int num = 0, temp = 0;    //marking nodes as visited    visit[node] = 1;    for (int i ... Read More

Convert a string to hexadecimal ASCII values in C++

Ayush Gupta
Updated on 06-Jan-2020 12:10:54

2K+ Views

In this tutorial, we will be discussing a program to convert a string to hexadecimal ASCII values.For this we will be provided with a string of characters. Our task is to print that particular given string into its hexadecimal equivalent.Example Live Demo#include #include //converting string to hexadecimal void convert_hexa(char* input, char* output){    int loop=0;    int i=0;    while(input[loop] != '\0'){       sprintf((char*)(output+i), "%02X", input[loop]);       loop+=1;       i+=2;    }    //marking the end of the string    output[i++] = '\0'; } int main(){    char ascii_str[] = "tutorials point";   ... Read More

Convert a String into a square matrix grid of characters in C++

Ayush Gupta
Updated on 06-Jan-2020 12:06:36

258 Views

In this tutorial, we will be discussing a program to convert a string into a square matrix grid of characters.For this we will be provided with a string of characters. Our task is to print that particular string in the format of a matrix grid having a certain number of rows and columns.Example Live Demo#include using namespace std; //converting the string in grid format void convert_grid(string str){    int l = str.length();    int k = 0, row, column;    row = floor(sqrt(l));    column = ceil(sqrt(l));    if (row * column < l)       row = column; ... Read More

Convert a sentence into its equivalent mobile numeric keypad sequence in C++

Ayush Gupta
Updated on 06-Jan-2020 12:02:35

616 Views

In this tutorial, we will be discussing a program to convert a sentence into its equivalent mobile numeric keypad sequence.For this we will be provided with a string of alphabetical characters. Our task is to print the numeric equivalent of the string i.e the numerical sequence of the keys to type that particular string.Example Live Demo#include using namespace std; //computing the numerical sequence string calc_sequence(string arr[], string input){    string output = "";    //length of input string    int n = input.length();    for (int i=0; i

Convert a number of length N such that it contains any one digit at least 'K' times in C++

Ayush Gupta
Updated on 06-Jan-2020 11:57:57

69 Views

In this tutorial, we will be discussing a program to convert a number of length N such that it contains any one digit at least ‘K’ times.For this we will be provided with a number of given length N. Our task is to convert the digits in the given number such that any one digit gets repeated at least ‘K’ times. Also, you have to calculate the cost of this operation which is the absolute difference between the two and finally print the minimum cost.Example Live Demo#include using namespace std; //calculating the minimum value and final number int get_final(int n, ... Read More

Advertisements