Found 7347 Articles for C++

C++ Program for Range sum queries without updates?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

223 Views

Here we will see how to get the sum of elements from index i to index j in an array. This is basically the range query. The task is easy by just running one loop from index i to j, and calculate the sum. But we have to care about that this kind of range query will be executed multiple times. So if we use the mentioned method, it will take much time. To solve this problem using more efficient way we can get the cumulative sum at first, then the range sum can be found in constant time. Let ... Read More

C++ Program for Largest K digit number divisible by X?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

82 Views

In this problem we will try to find largest K-digit number, that will be divisible by X. To do this task we will take the largest K digit number by this formula ((10^k) – 1). Then check whether the number is divisible by X or not, if not, we will get the exact number by using this formula.𝑚𝑎𝑥−(𝑚𝑎𝑥 𝑚𝑜𝑑 𝑋)One example is like a 5-digit number, that is divisible by 29. So the largest 5-digit number is 99999. This is not divisible by 29. Now by applying the formula we will get −99999−(99999 𝑚𝑜𝑑 29)=99999−7=99992The number 99992 is divisible by ... Read More

C++ Program for Gnome Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

345 Views

Here we will see how the gnome sort works. This is another sorting algorithm. In this approach if the list is already sorted it will take O(n) time. So best case time complexity is O(n). But average case and worst case complexity is O(n^2). Now let us see the algorithm to get the idea about this sorting technique.AlgorithmgnomeSort(arr, n)begin    index := 0    while index < n, do       if index is 0, then          index := index + 1       end if       if arr[index] >= arr[index -1], then ... Read More

C++ Program for Comb Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

273 Views

The basic idea of comb sort and the bubble sort is same. In other words, comb sort is an improvement on the bubble sort. In the bubble sorting technique, the items are compared with the next item in each phase. But for the comb sort, the items are sorted in a specific gap. After completing each phase, the gap is decreased. The decreasing factor or the shrink factor for this sort is 1.3. It means that after completing each phase the gap is divided by 1.3. Time Complexity is O(n log n) for best case. O(n2/2nP) (p is number of ... Read More

C++ Program for Cocktail Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

504 Views

The Cocktail sort is another variation of bubble sort. In the bubble sort technique, it always searches from left to right, and finds the largest element at the end, in the second phase it finds the second largest element at the second last position. This sorting technique traverses in both directions alternatively. Let us see the algorithm to understand the idea.Algorithmcocktail(array, n)Begin    flag := true    start := 0, end := n-1    while flag is set, do       flag := false       for i in range start to end-1, do         ... Read More

5 Different methods to find length of a string in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

11K+ Views

Here we will see five different ways to get the string lengths in C++. In C++ we can use the traditional character array string, and C++ also has String class. In different area there are different methods of calculating the string lengths.The C++ String class has length() and size() function. These can be used to get the length of a string type object. To get the length of the traditional C like strings, we can use the strlen() function. That is present under the cstring header file. Another two approaches are straight forward. One by using the while loop, and ... Read More

C/C++ Program for Largest Sum Contiguous Subarray?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

349 Views

An array of integers is given. We have to find sum of all elements which are contiguous. Whose sum is largest, that will be sent as output.Using dynamic programming we will store the maximum sum up to current term. It will help to find sum for contiguous elements in the array.Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of the Subarray is : 7AlgorithmmaxSum(array, n)Input − The main array, the size of the array.Output − maximum sum.Begin    tempMax := array[0]    currentMax = tempMax    for i := 1 to n-1, ... Read More

BFS using STL for competitive coding in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

534 Views

The Breadth First Search (BFS) traversal is an algorithm, which is used to visit all of the nodes of a given graph. In this traversal algorithm one node is selected and then all of the adjacent nodes are visited one by one. After completing all of the adjacent vertices, it moves further to check another vertices and checks its adjacent vertices again.In The competitive coding, we have to solve problems very quickly. We will use the STL (Standard Library of C++) to implement this algorithm, we need to use the Queue data structure. All the adjacent vertices are added into ... Read More

atan() function for complex number in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

111 Views

Here we will see the atan() method for the complex numbers. The complex numbers can be used using complex header file. In that header file the atan() function is also present. This is complex version of normal atan() function. This is used to find complex arc tan of a complex number.This function takes a complex number as input parameter, and returns the arc tan as output. Let us see one example to get the idea.Example Live Demo#include #include using namespace std; int main() {    complex c1(5, 2);    //atan() function for complex number    cout

asin() function for complex number in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

104 Views

Here we will see the asin() method for the complex numbers. The complex numbers can be used using complex header file. In that header file the asin () function is also present. This is complex version of normal asin () function. This is used to find complex arc sine of a complex number.This function takes a complex number as input parameter, and returns the arc sine as output. Let us see one example to get the idea.Example Live Demo#include #include using namespace std; int main() {    complex c1(5, 2);    //asin() function for complex number    cout

Advertisements