Programming Articles

Page 1427 of 2547

String to Integer (atoi) in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 796 Views

Suppose we have to design a module, that first discards as many whitespace characters as necessary until the first non-whitespace character is reached. After that, starting from this character, it takes an optional initial plus sign or minus sign followed by as many numerical digits, and interprets them as a numerical value.When the first sequence of non-whitespace characters in str is not a valid integral number, or when no such sequence exists because either str is empty or it contains only whitespaces, no conversion will be performed.So if the input is like “-45”, the output will be -45.To solve this, ...

Read More

Priority Queue using doubly linked list in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 847 Views

We are given with the data and the priority as an integer value and the task is to create a doubly linked list as per the priority given and display the result.Queue is a FIFO data structure in which the element which is inserted first is the first one to get removed. A Priority Queue is a type of queue in which elements can be inserted or deleted depending upon the priority. It can be implemented using queue, stack or linked list data structure. Priority queue is implemented by following these rules −Data or element with the highest priority will ...

Read More

Check If It Is a Straight Line in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 646 Views

Suppose we have a list of data-points consisting of (x, y) coordinates, we have to check whether the data-points are forming straight line or not. So if the points are like [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)], then they are forming straight line.To solve this, we will take the differences between each consecutive datapoints, and find the slope. For the first one find the slope. For all other points check whether the slope is same or not. if they are same, then simply return true, otherwise falseExampleLet us see the following implementation to get ...

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 260 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

Probability of getting a sum on throwing 2 Dices N times in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 409 Views

We are given with the sum and the number of times the pair of dice is thrown as an input and the task is to determine the probability of getting the given sum on throwing a pair of dice N times.Probability is the chances of getting the desired output from the set of data available. The range of probability lie between 0 and 1 where an integer 0 shows the chances of impossibility and 1 indicates certainty.ExampleInput-: sum = 12, N = 1 Output-: Probability = 1/36 Explanation-: if a pair of dice is thrown once then the combinations will ...

Read More

Find Positive Integer Solution for a Given Equation in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 344 Views

Suppose we have a function f that takes two parameters (x, y). We have to return all pairs of x and y, for which f(x, y) = z. The z is given as input, and x, y are positive integers. The function is constantly increasing function. So f(x, y) < f(x + 1, y) and f(x, y) < f(x, y + 1).To solve this we will perform straight-forward approach. Take i in range 1 to 1000, and j in range 1 to 1000, for all combinations of i, j, if f(i, j) = 0, then return true, otherwise false.Consider the ...

Read More

Cells with Odd Values in a Matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 420 Views

Suppose there are n and m which are the dimensions of a matrix. These are initialized by zeros. And indices are given where indices[i] = [ri, ci]. For each pair of [ri, ci] we have to increment all cells in row ri and column ci by 1. The output will be the number of cells with odd values in the matrix after applying the increment to all indices.To solve this, we will follow these steps −Initialize odd := 0, and x := row count of the matrixcreate a matrix matfor i in range 0 to xr = input[i, 0], c ...

Read More

Convert all substrings of length 'k' from base 'b' to decimal in C++

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

In this tutorial, we will be discussing a program to convert all substrings of length ‘k’ from base ‘b’ to decimal.For this we will be provided with a string of some certain length. Our task is to take the substrings from the given string of size ‘k’ and get it converted into the decimal numbers from being in base ‘b’.Example#include using namespace std; //converting the substrings to decimals int convert_substrings(string str, int k, int b){    for (int i=0; i + k = 0; i--){          sum = sum + ((sub.at(i) - '0') * pow(b, counter));          counter++;       }       cout

Read More

Convert an arbitrary Binary Tree to a tree that holds Children Sum Property in C++

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

In this tutorial, we will be discussing a program to convert an arbitrary binary tree to a tree that holds children sum property.For this we will be provided with a binary tree. Our task is to convert it into the binary tree that follows the children sum property. But the restriction is that we can only increment the values present in the nodes, neither can change the structure of the tree or decrement values in the node.Example#include #include using namespace std; //node structure for binary tree class node{    public:    int data;    node* left;    node* right;   ...

Read More

Convert an Array to a Circular Doubly Linked List in C++

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

In this tutorial, we will be discussing a program to convert an array to a circular doubly linked list.For this we will be provided with an array. Our task is to take the elements of the array and get it converted into a circular doubly linked list.Example#include using namespace std; //node structure for doubly linked list struct node{    int data;    struct node *next;    struct node *prev; }; //node creation struct node* getNode(){    return ((struct node *)malloc(sizeof(struct node))); } //printing the list int print_list(struct node *temp){    struct node *t = temp;    if(temp == NULL)   ...

Read More
Showing 14261–14270 of 25,466 articles
Advertisements