Programming Articles - Page 2525 of 3366

C++ Program to find sum of even factors of a number?

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

430 Views

In this section we will see how we can get the sum of all even prime factors of a number in an efficient way. There is a number say n = 480, we have to get all factor of this. The prime factors of 480 are 2, 2, 2, 2, 2, 3, 5. The sum of all even factors is 2+2+2+2+2 = 10. To solve this problem, we have to follow this rule −When the number is divisible by 2, add them into the sum, and divide the number by 2 repeatedly.Now the number must be odd. So we will ... Read More

C++ Program for Recursive Bubble Sort?

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

1K+ Views

In this section we will see another approach of famous bubble sort technique. We have used bubble sort in iterative manner. But here we will see recursive approach of the bubble sort. The recursive bubble sort algorithm is look like this.AlgorithmbubbleRec(arr, n)begin    if n = 1, return    for i in range 1 to n-2, do       if arr[i] > arr[i+1], then          exchange arr[i] and arr[i+1]       end if    done    bubbleRec(arr, n-1) endExample Live Demo#include using namespace std; void recBubble(int arr[], int n){    if (n == 1)     ... Read More

C++ Program for Range sum queries without updates?

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

342 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

159 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

513 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

366 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

650 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

Absolute distinct count in a sorted array?

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

205 Views

In this section we will see how to count how many of elements whose absolute values are distinct? Suppose in an array there are few elements like {5, 5, 6, -5, 8, 2, -2, 1}, so there are 8 elements. But there are 5 elements {5, 6, 8, 2, 1} which are distinct. The -5 and 5 are not considered as different, they are same as their absolute value is same.To solve this problem, we will use the Set data-structure. In set duplicate elements are not allowed. And when we are inserting item into the set, we will push only ... Read More

Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array?

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

243 Views

Here we will see how we can find the absolute difference between the sum of all prime numbers and all non-prime numbers of an array. To solve this problem, we have to check whether a number is prime or not. One possible way for primality testing is by checking a number is not divisible by any number between 2 to square root of that number. So this process will take 𝑂(√𝑛) amount of time. Then get the sum and try to find the absolute difference.AlgorithmdiffPrimeNonPrimeSum(arr)begin    sum_p := sum of all prime numbers in arr    sum_np := sum of ... Read More

Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?

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

216 Views

Here we will see how we can find the absolute difference between the product of all prime numbers and all non-prime numbers of an array. To solve this problem, we have to check whether a number is prime or not. One possible way for primality testing is by checking a number is not divisible by any number between 2 to square root of that number. So this process will take 𝑂(√𝑛) amount of time. Then get the product and try to find the absolute difference.AlgorithmdiffPrimeNonPrimeProd(arr)begin    prod_p := product of all prime numbers in arr    prod_np := product of ... Read More

Advertisements