Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 333 of 377

How to traverse a C++ set in reverse direction?

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 2K+ Views

Suppose we have a set, then we have to traverse the set in reverse direction. So if the set is like S = [10, 15, 26, 30, 35, 40, 48, 87, 98], then the output will be: 98 87 48 40 35 30 26 15 10.To traverse in reverse order, we can use the reverse_iterator. Here we will use the rbegin() and rend() function to get the begin and end of the reverse iterator.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {10, 15, 26, 30, 35, 40, 48, 87, 98};    set my_set(arr, arr + sizeof(arr) / sizeof(arr[0]));    set::iterator it;    cout

Read More

How to reverse an Array using STL in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 1K+ Views

Here we will see how to reverse an array using STL functions in C++. So if the array is like A = [10, 20, 30, 40, 50, 60], then the output will be B = [60, 50, 40, 30, 20, 10]. To reverse we have one function called reverse() present in the header file . The code is like below −Example Live Demo#include #include using namespace std; int main() {    int arr[] = {10, 20, 30, 40, 50, 60};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Read More

How to find the sum of elements of an Array using STL in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 435 Views

Here we will see how to find the sum of all element of an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then sum will be: 405. So here we have to use the accumulate() function to solve this problem. This function description is present inside header file.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Read More

How to find the minimum and maximum element of an Array using STL in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 2K+ Views

Here we will see how to find the maximum and minimum element from an array. So if the array is like [12, 45, 74, 32, 66, 96, 21, 32, 27], then max element is 96, and min element is 12. We can use the max_element() function and min_element() function, present in algorithm.h header file to get the maximum and minimum elements respectively.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Read More

How to delete last element from a map in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 688 Views

Here we will see how to delete the last element from C++ STL map. The map is the hash table based data type, it has key and value. We can use the prev() method to get last element, and erase() function to delete as follows.Example Live Demo#include #include using namespace std; int main() {    map my_map;    my_map["first"] = 10;    my_map["second"] = 20;    my_map["third"] = 30;    cout

Read More

How to delete last element from a List in C++ STL

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 638 Views

Suppose we have one STL list in C++. There are few elements. We have to delete the last element from that list. So if the elements are like [10, 41, 54, 20, 23, 69, 84, 75], then last element is 75. We will see the C++ code to delete last element from list.Example Live Demo#include #include using namespace std; void display(list my_list){    for (auto it = my_list.begin(); it != my_list.end(); ++it)    cout

Read More

Check if a key is present in a C++ map or unordered_map

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 564 Views

In C++ the Maps and unordered maps are hash tables. They use some keys and their respective key values. Here we will see how to check whether a given key is present in the hash table or not. The code will be like below −Example Live Demo#include #include using namespace std; string isPresent(map m, string key) {    if (m.find(key) == m.end())    return "Not Present";    return "Present"; } int main() {    map my_map;    my_map["first"] = 4;    my_map["second"] = 6;    my_map["third"] = 6;    string check1 = "fifth", check2 = "third";    cout

Read More

Find the number of players who roll the dice when the dice output sequence is given in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 167 Views

Suppose we have a string S and a number X. There are M different players who roll the dice. one player keeps on rolling the dice until he gets a number other than X. Here in the string S, S[i] represents the number at ith roll of a dice. We have to find the value of M. One constraint is that the last character in S will never be X. So for example, if string is “3662123” and X = 6, the output will be 5. This can be described as follows −First player rolls and got 3Second player rolls, ...

Read More

Find the Number of Maximum Product Quadruples in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 262 Views

Suppose we have one integer array with n elements. We have to find the maximum product of a quadruple in the array. So if the array is like [3, 5, 20, 6, 10], then final product is 6000, and elements in quadruple is 10, 5, 6, 20To solve this, we will follow these steps −Sort the array in ascending orderSuppose x be the product of last four elements, y be the product of first four elements, and z be the product of first two and second two elementsReturn the max of x, y and z.Example Live Demo#include #include using namespace std; ...

Read More

Find the node whose absolute difference with X gives maximum value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 132 Views

Suppose we have a tree, and the weights of all the nodes and an integer x. We have to find the node i, such that |weight[i] - x| is minimum. If the graph is like below, and x = 15Output will be 3. Now for different nodes, it will be like belowNode 1, |5 – 15| = 10Node 2, |10 – 15| = 5Node 3, |11 – 15| = 4Node 4, |8 – 15| = 7Node 5, |6 – 15| = 9The idea is simple. We will perform the DFS on the tree, and keep track of the node, whose ...

Read More
Showing 3321–3330 of 3,768 articles
« Prev 1 331 332 333 334 335 377 Next »
Advertisements