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
Server Side Programming Articles - Page 2004 of 2650
405 Views
Suppose we have a number N. We have to find almost prime numbers in range 1 to N. A number is called almost prime when it has exactly two distinct factors. The numbers can have any number of non-prime factors, but should be two prime factors. So if N is 2, then output will be 2. There are two numbers 6 and 10.Here we will use the Sieve of Eratosthenes approach. Please check the following implementation to get better idea.Example Live Demo#include #define N 100005 using namespace std; bool prime[N]; void SieveOfEratosthenes() { for(int i = 0; i
532 Views
Consider we have two strings A and B. The length of A and B are same. In a single shift we can rotate the string B one element. We have to find minimum required shift to get common prefix of maximum length from A and B. So if A = “computerprogramming”, and B = “programminglanguage” So the minimum shift is 8, and prefix is “programming”.Suppose we add the string B at the end of B, so B = B + B, then there is no-need to find prefix of each shift separately. So we have to find the longest prefix ... Read More
370 Views
Suppose, we have an array of n numbers. We have to find all elements in array, which have at least two greater elements. If the array is like A = [2, 8, 7, 1, 5], then the result will be [2, 1, 5]To solve this, we will find second max element, then print all elements which is less than or equal to second max value.Example#include using namespace std; void searchElements(int arr[], int n) { int first_max = INT_MIN, second_max = INT_MIN; for (int i = 0; i < n; i++) { if (arr[i] > first_max) ... Read More
290 Views
Suppose we have an array of n elements. These are the ratings of them. Find the minimum cost to buy all books, with the following condition −Cost of each book would be at-least 1 dollarA book has higher cost than an adjacent (left or right) if rating is more than the adjacent.So for example, if the rating array is like [1, 3, 4, 3, 7, 1], Then the output is 10, As 1 + 2 + 3 + 1 + 2 + 1 = 10To solve this, we have to make two arrays called LtoR, and RtoL, and fill them ... Read More
425 Views
Suppose we have an array of some points in XY plane. We have to find the minimum area of rectangle that can be formed from these points. The side of the rectangle should be parallel to the X and Y axes. If we cannot form the rectangle, then return 0. So if the array of points is like [(1, 1), (1, 3), (3, 1), (3, 3), (2, 2)]. The output will be 4. As the rectangle can be formed using the points (1, 1), (1, 3), (3, 1) and (3, 3).To solve this, give the points by x coordinates, so ... Read More
151 Views
Suppose we have a binary tree. The task is to print maximum of the sum of all nodes in the vertical order traversal. So if the tree is like below −The vertical order traversal is like −4 2 1 + 5 + 6 = 12 3 + 8 = 11 7 9Here the maximum is 12. The approach is simple. We will perform the vertical order traversal, then find the sum and check for maximum.Example#include #include #include #include using namespace std; class Node { public: int key; Node *left, *right; }; Node* getNode(int key){ Node* node ... Read More
158 Views
Suppose we have one theta, or angle value. We have to find one time in hh:mm format, that creates the angle by the hour and minute hands. Suppose the angle is 90°, then the result can be 3:00.As there are 12 hours, so there are 12 possibilities for hours and 60 possibilities for minutes. We will loop through all possible times. If angle for any time is same as given theta, then print that time.Example Live Demo#include #include using namespace std; float angleFromClockHand(int hour, int minute) { float hour_angle = 0.5 * (hour*60 + minute); float minute_angle = 6*minute; ... Read More
208 Views
Suppose we have two integers n and k. We have to find the maximum value of x, such that n! mod (k^x) = 0. So when n = 5, and k = 2, then output will be 3. As n! = 120, now for different values of x, it will be −120 mod 2^0 = 0, 120 mod 2^1 = 0, 120 mod 2^2 = 0, 120 mod 2^3 = 0, 120 mod 2^4 = 8, 120 mod 2^5 = 24, 120 mod 2^6 = 56, 120 mod 2^7 = 120. As the max value of x = 3, the ... Read More
290 Views
Suppose we have two integers N and K. We have to find a permutation of integers from the range [1 to N] such that the number of indices (1 – base indexing) where gcd(P[i], i) > 1 is exactly K. So if N = 4 and K = 3, then output will be [1, 2, 3, 4], as gcd(1, 1) = 1, gcd(2, 2) = 2, gcd(3, 3) = 3, gcd(4, 4) = 4If we observe it carefully, we can find that gcd(i, i+1) = 1, gcd(1, i) = 1 and gcd(i, i) = i. As the GCD of any ... Read More