Found 7197 Articles for C++

QuickSort using Random Pivoting

Divya Sahni
Updated on 04-Sep-2023 09:37:30

3K+ Views

Quick Sort is a Divide and Conquer algorithm. In this algorithm, we elect a pivot element and then partition the array around the pivot element. The two partitions are such that one part contains elements all lower than the pivot element and the other part contains elements all higher than the pivot element. Similarly, each part is further partitioned around a pivot selected in each part and this process is executed until one single element is reached. Choosing a Pivot A pivot can be chosen in an array as follows − A random pivot. Rightmost or leftmost element as ... Read More

Max occurring divisor in an interval

Divya Sahni
Updated on 25-Jul-2023 12:06:49

192 Views

Let x and y be two numbers. In this case, x is said to be a divisor of y if when y is divided by x it returns zero remainder. The maximum occurring divisor in an interval is the number that is a divisor of the maximum number of elements of that interval. Problem Statement Given an interval [a, b]. Find the maximum occurring divisor in the range including both a and b, except ‘1’. In case all divisors have equal occurrence, return 1. Sample Example 1 Input [2, 5] Output 2 Explanation − Divisors of 2 = ... Read More

Ramanujan–Nagell Conjecture

Divya Sahni
Updated on 25-Jul-2023 11:38:12

180 Views

Ramanujan-Nagell Equation is an example of the exponential Diophantine equation. The diophantine equation is a polynomial equation with integer coefficients of two or more unknowns. Only integral solutions are required for the Diophantine equation. Ramanujan-Nagell Equation is an equation between a square number and a number that is seven less than the power of 2, where the power of 2 can only be a natural number. Ramanujan conjectured that the diophantine equation 2y - 7 = x2 has positive integral solutions and was later proved by Nagell. $$\mathrm{2y−7=x^2\:has\:x\epsilon\:Z_+:x=1, 3, 5, 11, 181}$$ Triangular Number − It counts objects arranged in ... Read More

Comparison between Tarjan’s and Kosaraju’s Algorithm

Way2Class
Updated on 21-Jul-2023 18:43:16

784 Views

Tarjan’s algorithm is to locate strongly linked components in a directed graph, Robert Tarjan created the graph traversal technique known as Tarjan's algorithm in 1972. Without going over previously processed nodes, it effectively locates and handles each highly related component using a depth-first search strategy and a stack data structure. The algorithm is often employed in computer science and graph theory and has several uses, including algorithm creation, network analysis, and data mining. Kosaraju’s algorithm consists of two passes over the graph. In the first pass, the graph is traversed in reverse order and a "finish time" is assigned ... Read More

Longest substring having K distinct vowels

Way2Class
Updated on 21-Jul-2023 18:01:45

441 Views

In this article, we will explore the problem of finding the longest substring in a given string that contains K distinct vowels. The problem can be solved using different algorithms in C++. This problem is commonly encountered in the field of computer science, particularly in text processing and natural language processing tasks. It tests one's ability to manipulate strings and handle edge cases. Syntax In the realm of C++, the class std::string epitomizes a string datatype. This versatile entity enables storage and manipulation of character sequences. The template class std::vector embodies a dynamic array, granting the ability to resize arrays ... Read More

Reverse String according to the number of words

Way2Class
Updated on 21-Jul-2023 17:58:41

324 Views

String manipulation is an essential skill in programming, as it helps us process and analyze text data efficiently. C++ provides a rich set of string manipulation functions and objects, making it easier to work with text data. In this article, we will discuss how to reverse a string according to the number of words in C++. Approaches Approach 1 − Using stringstreams and vectors Approach 2 − Using substrings and string manipulation functions Syntax String object in C++: The std::string class is a part of the C++ Standard Library and provides various string manipulation functions. String manipulation functions: ... Read More

Replace every character of string by character whose ASCII value is K times more than it

Way2Class
Updated on 21-Jul-2023 17:56:36

632 Views

In the realm of C++ programming, it is possible to replace every character of a specified string with a symbol whose ASCII value is elevated by a factor of K compared to the original character. This can be accomplished through the implementation of a straightforward algorithmic technique. This piece delves into the syntax, the algorithm, and two distinct methods to address the problem, complete with code illustrations in C++. Syntax To substitute every character in a string with a character whose ASCII value is multiplied by K, the syntax is as follows − string replace(string str, int K); Here, ... Read More

Implement Multi Stack (K stacks) using only one Data Structure

Way2Class
Updated on 21-Jul-2023 17:54:03

3K+ Views

A dynamic multi-stack is a remarkable data structure that possesses the capacity to store elements in numerous stacks, with an ever-changing quantity of stacks. It can be a daunting task to implement K stacks utilizing only one data structure. In this instructional guide, we shall investigate two distinct techniques to execute dynamic multi-stack (K stacks) using C++. The initial technique employs an array to stock the elements, along with two additional arrays to monitor the topmost and following indices of the stacks. The secondary technique employs a vector of nodes to stock the elements, along with a vector to keep ... Read More

Length of Longest Increasing Subsequences (LIS) using Segment Tree

Way2Class
Updated on 21-Jul-2023 17:45:08

562 Views

Segment Tree is a versatile data structure designed for answering range queries and performing updates on arrays in logarithmic time complexity, where each node stores information related to a specific range of elements in the array. In the context of the Longest Increasing Subsequence (LIS) problem, which requires determining the length of the longest subsequence of a given sequence in which the elements are sorted in increasing order, Segment Trees can be utilized to efficiently compute the Length of Longest Increasing Subsequences in an array. This approach significantly reduces the time complexity compared to traditional methods and has ... Read More

Classification of Algorithms with Examples

Way2Class
Updated on 21-Jul-2023 17:39:00

697 Views

Classification of algorithms helps in selecting the most suitable one for a specific task, enabling developers to optimize their code and achieve better performance. In computer science, algorithms are sets of well-defined instructions used to solve problems or perform specific tasks. The efficiency and effectiveness of these algorithms are crucial in determining the overall performance of a program. In this article, we will discuss two common approaches for classifying algorithms, namely, based on their time complexity and based on their design technique. Syntax The syntax of the main function used in the codes for both approaches − int ... Read More

Advertisements