Found 7197 Articles for C++

Working with Array and Vectors using STL in C++

sudhir sharma
Updated on 17-Jul-2020 10:56:51

387 Views

Array and vectors are very important data structures in competitive programming for solving problems. And the STL (Standard Template Library) in c++ programming provide some functions to perform operations of arrays and vectors.Let’s see some of these functions in action, Finding sum, Min and Max of the array/vector − In STL there are function that helps you to find the sum, max, and min of the array/vector. Functions with there function, Finding sumaccumulate(startIndex, endIndex, initialSum)Greatest element of the array/vecto*max_element(startIndex, endIndex)Smallest element of array/vector*min_element(startIndex, endIndex)Program to perform operations on array −Example Live Demo#include using namespace std; int main(){    int array[] ... Read More

Write a C program that displays contents of a given file like 'more' utility in Linux

sudhir sharma
Updated on 17-Jul-2020 10:48:40

260 Views

Here, we will write a C program that will display the contents of a file page by page as displayed in Linux using the more command.This program will show a specific number of lines on the screen first and then wait for the user to hit enter to move to the next page i.e. next set of n lines.For displaying the content of the file like this we will open the file and print its contents. And maintain a counter for new lines in the file. When this counter reaches n, we will read a key pressed by the user ... Read More

Write a function that counts the number of times a given int occurs in a Linked List in C++

sudhir sharma
Updated on 15-Jul-2020 06:26:14

460 Views

In this problem, we are given a linked list. Our task is to create a function that will be able to count the number of times a given number occurs in the linked list.Let’s take an example to understand the problem, InputLinked list = 10-> 50 -> 10 -> 20 -> 100 -> 10, int = 10Output3Explaination − the number 10 occurs 3 times in the linked list.The solution to this problem is simple, just traverse the linked list and increment a counter the current node value is equal to the given number.The looping over the nodes of the linked ... Read More

Write a function that generates one of 3 numbers according to given probabilities in C++

sudhir sharma
Updated on 15-Jul-2020 06:23:43

363 Views

In this problem, we have to create a function that will generate three numbers based on the given probability.For this, we will use the built-in random number generator function which is rand(a, b) which generates random numbers within the range [a, b] with equal probability.Our task is to return only three numbers A, B, C which have the probability of occurrence as P(A), P(B), P(C) respectively and according to definition of probability P(A) + P(B) + P(C) = 1.To create our function using rand(a, b). we will use its feature that the probability of occurrence of any number from a ... Read More

Write a function to delete a Linked List in C++ Programming

sudhir sharma
Updated on 15-Jul-2020 06:21:31

683 Views

Here, we will create a function that will delete all elements of a linked list one by one.In c/c++, there is no specific function to perform this task but in java, the automatic garbage collection is done to ease deleting linked list.Now, let’s see the implementation of this program, Example Live Demo#include using namespace std; class Node{    public:    int data;    Node* next; }; void deleteLinkedList(Node** head_ref){    Node* current = *head_ref;    Node* next;    while (current != NULL){       coutnext = (*head_ref);    (*head_ref) = new_node; } int main(){    Node* head = NULL; ... Read More

Handshakes That Don't Cross in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:48:50

326 Views

Suppose we have an even number of people n that stand around a circle and each person shakes hands with someone else, so that there will be n / 2 handshakes total. We have to find the number of ways these handshakes could occur such that none of the handshakes cross. The answers may be very large so return the answer mod 10^9 + 7.So, if the input is like n = 2, then the output will be 1To solve this, we will follow these steps −m := 10^9 + 7Define an array dp of size (n+1)dp[0] := 1for initialize i := 0, when i

Palindrome Removal on C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:46:04

339 Views

Suppose we have an integer array called arr, now in one move we can select a palindromic subarray from index i to j where i

Divide Chocolate in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:43:46

384 Views

Suppose we have one chocolate bar that consists of some chunks. In each chunk it has its own sweetness given by a list called sweetness. If we want to share the chocolate among K friends so we start cutting the chocolate bar into K+1 piece using K cuts, now each piece consists of some consecutive chunks. If we take out the piece with the minimum total sweetness and give the other pieces to our friends. We have to find the maximum total sweetness of the piece we can get by cutting the chocolate bar optimally.So, if the input is like ... Read More

Valid Palindrome III in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:40:52

477 Views

Suppose we have a string s and another number k; we have to check whether the given string is a K-Palindrome or not.A string is said to be K-Palindrome if it can be transformed into a palindrome by removing at most k characters from it.So, if the input is like s = "abcdeca", k = 2, then the output will be true as by removing 'b' and 'e' characters, it will be palindrome.To solve this, we will follow these steps −Define a function lcs(), this will take s, t, n := size of sadd one blank space before sadd one ... Read More

Minimum Time to Build Blocks in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:38:21

194 Views

Suppose we have a list of blocks, if we have blocks[i] = t, this means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker. Single worker can either split into two workers or build a block then go home. These two decisions take some time. The time cost of spliting one worker into two workers is given as a number called split.So, if the input is like blocks = [1, 2], and split = 5, then the output will be 7 as we can split the worker ... Read More

Advertisements