Found 7347 Articles for C++

Maximum equlibrium sum in an array in C++

Narendra Kumar
Updated on 10-Jan-2020 07:10:53

351 Views

Problem statementGiven an array arr[]. Find maximum value of prefix sum which is also suffix sum for index i in arr[].ExampleIf input array is −Arr[] = {1, 2, 3, 5, 3, 2, 1} then output is 11 as −Prefix sum = arr[0..3] = 1 + 2 + 3 + 5 = 11 andSuffix sum = arr[3..6] = 5 + 3 + 2 + 1 = 11AlgorithmTraverse the array and store prefix sum for each index in array presum[], in which presum[i] stores sum of subarray arr[0..i]Traverse array again and store suffix sum in another array suffsum[], in which suffsum[i] stores ... Read More

Maximum element in a very large array using pthreads in C++

Narendra Kumar
Updated on 10-Jan-2020 07:07:59

377 Views

Problem statementGiven a very large array of integers, find maximum within the array using multithreadingExampleIf input array is {10, 14, -10, 8, 25, 46, 85, 1673, 63, 65, 93, 101, 125, 50, 73, 548} thenmaximum element in this array is 1673AlgorithmLet us call array size as total_elementsCreate N threadsEach thread will process (total_elementes/N) array elements and will find maximum element from it.Finally compute the maximum from the maximum value reported by each thread.Example#include #include #include #include #define MAX 10 #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) typedef struct struct_max {    int start;    int end;    int ... Read More

Maximum element in a sorted and rotated array in C++

Narendra Kumar
Updated on 10-Jan-2020 07:03:19

375 Views

DescriptionGiven a sorted array of distinct elements which is rotated at some unknown point, the task is to find the maximum element in it.ExampleIf input array is {30, 40, 50, 10, 20} then maximum element is 50.AlgorithmThe maximum element is the only element whose next is smaller than it. If there is no next smaller element, then there is no rotation i.e. last element is the maximumWe check this condition for middle element by comparing it with elements at mid – 1 and mid + 1. If maximum element is with elements at mid – 1 and mid + 1. ... Read More

Missing Number In Arithmetic Progression using C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:13:44

213 Views

Suppose we have an array that represents elements of arithmetic progression in order. One element is missing. We have to find the missing element. So if arr = [2, 4, 8, 10, 12, 14], output is 6, as 6 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the difference between the middle and next to the middle is the same as diff or not. If not, then the missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the ... Read More

Day of the Week in C++

Arnab Chakraborty
Updated on 19-Feb-2021 07:24:45

6K+ Views

Suppose we have a date (day, month and year). From this date, we have to find the day of the week of that given date. To solve this we will use Zeller’s Algorithm. The formula to find weekday using Zeller’s Algorithm is here𝑤=$$\lgroup d+\lfloor \frac{13(m+1)}{5} \rfloor+y+\lfloor\frac{y}{4} \rfloor+\lfloor\frac{c}{4} \rfloor+5c \rgroup mod 7$$The formula is containing some variables; They are −d − The day of the date.m − It is the month code. For March to December, it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then the given ... Read More

N-th Tribonacci Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:07:03

295 Views

Suppose we have a value n, we have to generate n-th Tribonacci number. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}We can solve them by following this algorithm −Algorithm• first := 0, second := 1, third := 1 • for i in range n – 3, do    o next := first ... Read More

Range Sum Query - Immutables in C++

Arnab Chakraborty
Updated on 28-Apr-2020 10:50:14

106 Views

Suppose we have an array of integers. We have to find the sum of the elements present from index i to j. Two things we have to keep in mind that the array will be immutable, so elements will not be altered, and there will be multiple such queries. So we have to care about the execution time for a large number of queries. Suppose the array is like A = [5, 8, 3, 6, 1, 2, 5], then if query is (A, 0, 3), then it will be 5 + 8 + 3 + 6 = 22.To solve this, ... Read More

Ugly Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 10:39:27

796 Views

Ugly numbers are those numbers whose prime factors are 2, 3 or 5. From 1 to 15, there are 11 ugly numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15. The numbers 7, 11, 13 are not ugly because they are prime. The number 14 is not ugly because in its prime factor the 7 will come. So suppose we want to check the 10th ugly number. The value will be 12.Let us see the following algorithm to get an idea −AlgorithmgetUglyNumbers(n)Input − The number of terms.Output − Find nth Ugly numbers.Begin    define array named uglyNum ... Read More

Factorial Trailing Zeroes in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:41:55

167 Views

Here we will see how to calculate the number of trailing 0s for the result of factorial of any number. So if the n = 5, then 5! = 120. There is only one trailing 0. For 20! it will be 4 zeros as 20! = 2432902008176640000.The easiest approach is just calculating the factorial and count the 0s. But this approach fails for a large value of n. So we will follow another approach. The trailing zeros will be there if the prime factors are 2 and 5. If we count the 2s and 5s we can get the result. ... Read More

Excel Sheet Column Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:40:10

666 Views

We know that the excel column numbers are alphabetic. It starts from A, and after Z, it will AA, AB, to ZZ, then again AAA, AAB, to ZZZ and so on. So column 1 is A, column 27 is Z. Here we will see how to get the column letter if a number of columns is given. So if the column number is 80, then it will be CB.Suppose we have a number n, and its value is 28, then we need to take a reminder with 26. If the remainder is 0, then the number is 26, 52 and ... Read More

Advertisements