Found 7346 Articles for C++

Check if the given two numbers are friendly pairs or not

Divya Sahni
Updated on 25-Jul-2023 12:25:24

483 Views

Friendly Numbers − According to number theory, friendly numbers are two or more numbers having the same abundancy index. Abundancy Index − Abundancy index of a natural number can be defined as the ratio between the sum of all the divisors of the natural number and the natural number itself. The abundancy of a number n can be expressed as $\mathrm{\frac{\sigma(n)}{n}}$ where $\mathrm{\sigma(n)}$ denotes the divisor function equal to all the divisors of n. For example, the abundancy index of the natural number 30 is, $$\mathrm{\frac{\sigma(30)}{30}=\frac{1+2+3+5+6+10+15+30}{30}=\frac{72}{30}=\frac{12}{5}}$$ A number n is said to be a ‘friendly number’ if there exists a ... Read More

Find the Number of 'X' Total Shapes

Shubham Vora
Updated on 22-Jul-2023 14:50:19

99 Views

In this problem, we need to find the total number of ‘X’ shapes in the given matrix. We can construct the single ‘X’ shape using 1 or more adjacent ‘X’ elements. We can use the DFS (depth−first search) technique to solve the problem. For each ‘X’ element, we can find all adjacent elements using DFS and count it as a single ‘X’ shape. If we find a new ‘X’, we find its adjacent again. Here, we will use the iterative and recursive DFS to find the total number of ‘X” shapes. Problem statement − We have given a matrix[] of ... Read More

Check if any valid sequence is divisible by M

Divya Sahni
Updated on 25-Jul-2023 12:20:14

77 Views

A sequence is a collection of objects, and in our case, it is a collection of integers. The task is to find if a sequence with the usage of the addition and subtraction operator within the elements is divisible by M or not. Problem Statement Given an integer M and an array of integers. Using only addition and subtraction between elements check if there is a valid sequence whose solution is divisible by M. Sample Example 1 Input: M = 2, arr = {1, 2, 5} Output: TRUE Explanation − For the given array a valid sequence {1 ... Read More

XNOR of two numbers

Divya Sahni
Updated on 25-Jul-2023 12:16:12

371 Views

XNOR(Exclusive-NOR) gate is a digital logic gate that takes two inputs and gives one output. Its function is the logical complement of the Exclusive-OR (XOR) gate. The output is TRUE if both inputs are the same and FALSE if the inputs are different. The truth table of the XNOR gate is given below. A B Output 1 1 1 1 0 0 0 1 0 0 0 1 Problem Statement Given two numbers x and y. Find the XNOR of the two numbers. Sample Example 1 Input: x ... Read More

QuickSort using Random Pivoting

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

1K+ 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

114 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

109 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

404 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

164 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

152 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

Advertisements