C++ Articles

Page 100 of 597

Divide Chocolate in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 430 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

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 416 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

Palindrome Removal on C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 396 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

Read More

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 296 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
sudhir sharma
Updated on 11-Mar-2026 511 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

Woodall Number in C++

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

In this problem, we are given a number and our task is to check if the number is Woodall number or not.Woodall number is a special type of number which is of the form, Wn = n.2n -1First 5 Woodall numbers are 1, 7, 23, 63, 159Let’s take an example to understand the problem, InputX = 159OutputYesTo solve this problem, we will observe the number, if the number is even then it cannot be Woodall and then check for the number. To check, add the number by 1 and recursively divide the number by 2. after each division count the ...

Read More

Working with Array and Vectors using STL in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 428 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#include using namespace std; int main(){    int array[] = ...

Read More

Word Ladder (Length of shortest chain to reach a target word) in C++

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

In this problem, we are given a dictionary and two words ‘start’ and ‘target’. Our task is to generate a chain (ladder) from start work to target word, the chain is created such that each word differs the other character by only one word and the word should also exist in the dictionary. The target word exists in the dictionary and also the length of all words is the same. The program will return the length of the shortest path from start to target.Let’s take an example to understand the problem, InputDictionary = {‘HEAL’, ‘HATE’, ‘HEAT’, ‘TEAT’, ‘THAT’, ‘WHAT’ , ...

Read More

Ways to write N as sum of two or more positive integers in C++

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

In this problem, we are given an integer n. Our task is to find the total number of ways in can be expressed as sum of two or more positive integers.Let’s take an example to understand the problem, InputN = 4Output5Explanation4 can be written as the sum in these ways, 4, 3+1, 2+2, 2+1+1, 1+1+1+1To solve this problem, we will use Euler’s recurrence formula. For a number n the total number of ways it can be generated p(n) by, Σ∞n=0 p(n)xn = Π∞k=1 (1/(1-xk ))Using this formula, we will derive formula for p(n), p(n) = p(n-1) + p(n-2) - p(n-5) ...

Read More

Word formation using concatenation of two dictionary words in C++

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

In this problem, we are given a dictionary and a word. Our task is to check if the given wors can be formed using the concatenation of two dictionary words.While forming given words repetition of words is not legal.Let’s take an example to understand the problem, Inputdictionary = {“hello”, “tutorials”, “program” , “problem”, “coding”, “point”} word = “tutorialspoint”OutputyesExplanationtutorialspoint is created using tutorials and point.To solve this problem, we will store all elements of the dictionary in a prefix tree commonly known as a trie. And then search for the prefix of the word in the trie, if found split it ...

Read More
Showing 991–1000 of 5,961 articles
« Prev 1 98 99 100 101 102 597 Next »
Advertisements