Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2523 of 3363
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
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
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
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
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
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
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
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
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
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