Programming Articles - Page 2523 of 3363

C++ Program for Range sum queries without updates?

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

365 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

175 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

529 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

386 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

663 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

218 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

260 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

229 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

Absolute difference between the first X and last X Digits of N?

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

470 Views

Here we will see how to get the differences between first and last X digits of a number N. The number and X are given. To solve this problem, we have to find the length of the number, then cut the last x digits using modulus operator. After that cut all digits from the number except first x digits. Then get the difference, and return the result. Let the number is N = 568424. The X is 2 so first two digits are 56, and last two digits are 24. The difference is (56 - 24) = 32.AlgorithmdiffFirstLastDigits(N, X)begin ย  ... Read More

Absolute difference between sum and product of roots of a quartic equation?

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

347 Views

In this section we will see how to get the absolute difference between the sum of the roots and the products of the root of a quartic equation?The quartic equation is like ๐‘Ž๐‘ฅ4+๐‘๐‘ฅ3+๐‘๐‘ฅ2+๐‘‘๐‘ฅ+๐‘’We can solve the equation and then try to get the product and sum of the roots by some normal process, but that takes much time and that approach is not so efficient. In this kind of equation, we have two formulae. The Sum of roots are always โˆ’๐‘โˆ•๐‘Ž and the product of roots are always ๐‘’โˆ•๐‘Ž . So we have to find only the value of โˆฃโˆ’๐‘โˆ•๐‘Žโˆ’ ... Read More

Advertisements