Programming Articles

Page 1384 of 2547

Smallest Range II in C++

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

Suppose we have an array A of integers, for each integer A[i] we have to choose either x = -K or x = K, and add x to A[i] (only once). So after this process, we have some array B. We have to find the smallest possible difference between the maximum value of B and the minimum value of B. So if the input is A = [0, 10], K = 2, then the output will be 6, as B = [2, 8].To solve this, we will follow these steps −set ret := 0, n := size of array Asort ...

Read More

Remove Sub-Folders from the Filesystem in C++

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

Suppose we have a list of folders, we have to remove all sub-folders in those folders and return in any order the folders after removing. Here if a folder[i] is located within another folder[j], it is denoted as subfolder of it. The paths will be like folder1/subfolder2/… etc.Suppose the input is like["/myfolder", "/myfolder/secondfolder", "/another/document", "/another/document/extrafolder", "/another/final"], then the output will be: ["/myfolder", "/another/final", "/another/document"]To solve this, we will follow these steps −sort the folder array based on the length of the pathscreate one map m, and another array ansfor i in range 0 to size of path array – 1s ...

Read More

Online Election in C++

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

Suppose in an election, the i-th vote was cast for persons[i] at time times[i]. Now, we have to implement the following query function: TopVotedCandidate.q(int t) this will find the number of the person that was leading the election at time t. Votes cast at time t will count towards our query. If there is a tie, the most recent vote (among tied candidates) wins.So if we initialize this with TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]), then call q() like: q(3), q(12), q(25), q(15), q(24), q(8), then the result will be [0, 1, ...

Read More

Policy-based data structures in g++

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

g++ compiler is a C++ compiler for GNU in Linux.The g++ compiler also adds support for some special data structures that are not in C++ programming language standard library. These are known as policy-based data structures.The policy-based data structures provide the programmer a high-performance, semantic safety and flexibility as compared to the standard data structures of C++ std library.To include these data structures to your program, the following lines should be added, #include using namespace __gnu_pbds;ExampleLet’s see a program to see how these policy-based data structures work.#include #include #include #include using namespace __gnu_pbds; using namespace ...

Read More

match_results empty() in C++ STL

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

In this article we will be discussing the working, syntax and examples of match_results::empty() function in C++ STL.What is a match_results in C++ STL?std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.What is match_results::empty()?match_results::empty() function is an inbuilt function in C++ STL, which is defined in header file. empty() checks whether the smatch object which is associated is empty or having some match values in it. empty() returns true if the match object is ...

Read More

match_results length() in C++ STL

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

In this article we will be discussing the working, syntax and examples of match_results::length() function in C++ STL.What is a match_results in C++ STL?std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.What is match_results::length()?match_results::length() function is an inbuilt function in C++ STL, which is defined in header file. length() is used to check the length of the n-th match in the match_results object associated with it. length() accepts a parameter which is the match ...

Read More

Policemen catch thieves in C++

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

In this problem, we are given an array of n elements. Each element of the array has either a policeman or a thief, one thief can be caught by one police and we have to find the maximum number of thieves that can be caught by the police if a policeman can catch a thief k units away from him.Let’s take an example to understand the problem, Input −array = {T, P, P, P, T , T, T} K = 2.Output − 3Explanation − here, each policeman will catch a thief, P at index 1 will catch T at index ...

Read More

match_results operator= in C++

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

In this article we will be discussing the working, syntax and examples of match_results operator ‘=’ in C++ STL.What is a match_results in C++ STL?std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.What is match_results operator ‘=’Match_results operator = is an equality operator which is used to assign the value to a match_results. Operator = is used to copy or move the elements from one match_results object to another.Syntaxmatch_results1 = (match_results2);ParametersAnother match_results object whose data ...

Read More

Flip String to Monotone Increasing in C++

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

Suppose a string of '0's and '1's is given. That string will be monotonic increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.). We have a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Find the minimum number of flips to make S monotone increasing. So if the input is like “010110”, then the output will be 2. By flipping we can get “011111” or “000111”.To solve this, we will follow these steps −n := size ...

Read More

match_results operator[] in C++ STL

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

In this article we will be discussing the working, syntax and examples of match_results operator ‘[ ]’ in C++ STL.What is a match_results in C++ STL?std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.What is match_results operator ‘[ ]’Match_results operator [] is a reference operator which is used to directly refer to i-th position of a match_results. Operator [] returns i-th match position of the object associated. This operator comes in handy when we have ...

Read More
Showing 13831–13840 of 25,466 articles
Advertisements