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 2133 of 2650
242 Views
Suppose we have an array A, from that array, we have to choose two pairs (a, b) and (c, d), such that ab = cd. Let the array A = [3, 4, 7, 1, 2, 9, 8]. The output pairs are (4, 2) and (1, 8). To solve this, we will follow these steps −For i := 0 to n-1, dofor j := i + 1 to n-1, doget product = arr[i] * arr[j]if product is not present in the hash table, then Hash[product] := (i, j)if product is present in the hash table, then print previous and current elements.Example Live ... Read More
194 Views
Suppose we have an array A, from that array, we have to get all pairs (a, b) such that the a%b = k. Suppose the array is A = [2, 3, 4, 5, 7], and k = 3, then pairs are (7, 4), (3, 4), (3, 5), (3, 7).To solve this, we will traverse the list and check whether the given condition is satisfying or not.Example Live Demo#include using namespace std; bool displayPairs(int arr[], int n, int k) { bool pairAvilable = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i] % arr[j] == k) { cout
1K+ Views
Here we will see how to print all factorial numbers less than or equal to n, a number N is said to be factorial number if it is a factorial of a positive number. So some factorial numbers are 1, 2, 6, 24, 120.To print factorial numbers, we do not need to find the factorial directly. Starting from i = 1, print factorial*i. Initially factorial is 1. Let us see the code for better understanding.Example Live Demo#include using namespace std; void getFactorialNumbers(int n) { int fact = 1; int i = 2; while(fact
205 Views
Consider we have a binary tree. We have to find if there are some duplicate subtrees in the tree or not. Suppose we have a binary tree like below −There are two identical subtrees of size 2. In each subtree D, BD and BE both are also duplicate subtrees We can solve this problem by using tree serialization and hashing process. We will store the inorder traversal of subtrees in the hash table. We will insert opening and closing parenthesis for empty nodes.Example Live Demo#include #include #include #include using namespace std; const char MARKER = '$'; struct ... Read More
142 Views
Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always.This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row.Example#include using namespace std; void makeSymmetricMatrix(int n) { int matrix[n][n]; for(int i = 0; i
163 Views
Here we have an array A with some elements. Our task is to find the subset where the geometric mean is maximum. Suppose A = [1, 5, 7, 2, 0], then the subset with greatest geometric mean will be [5, 7].To solve this, we will follow one trick, we will not find the mean, as we know that the largest two elements will form the greatest geometric mean, so the largest two elements will be returned as subset.Example#include using namespace std; void largestGeoMeanSubset(int arr[], int n) { if (n < 2) { cout max) ... Read More
688 Views
Suppose we have n different points in K dimension space, the value of n is in range (2, 105), and value of k in range (1 to 5). We have to determine the point such that the sum of Manhattan distance from resultant point to n points is minimized.The Manhattan distance between two points P1(x1, y1) and P2(x2, y2), is |x1 – x2| + |y1 – y2|. Suppose dimension is 3, and there are three points like (1, 1, 1), (2, 2, 2), (3, 3, 3), then the output will be (2, 2, 2).To solve this problem, we have to ... Read More
413 Views
Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the difference between x and y is same as given difference d. Suppose a list of elements are like A = [10, 15, 26, 30, 40, 70], and given difference is 30, then the pair will be (10, 40) and (30, 70)To solve this problem, we will assume that the array is sorted, then starting from left we will take two pointers to point elements, initially first one ‘i’ will point to the first element, ... Read More
205 Views
Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the product of x and y is maximum. The array may contain positive or negative elements. Suppose an array is like: A = [-1, -4, -3, 0, 2, -5], then the pair will be (-4, -5) as product is maximum.To solve this problem, we have to keep track four numbers, the positive_max, positive_second_max, negative_max, negative_second_max. At the end if the (positive_max * positive_second_max) is greater than (negative_max * negative_second_max), then return positive pairs, otherwise return ... Read More
411 Views
Consider we have two arrays with different number of elements. We have to find a pair of elements (x, y), where x is present in the first array, and y is present at the second array. The pair will be chosen such that after swapping the elements between these two arrays, the sum of these two arrays will be same.Suppose first array A is holding [4, 1, 2, 2, 1, 1] and B is holding [3, 3, 6, 3], now the sum of A is 11 and sum of B is 15, we will take a pair like (1, 3), ... Read More