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 2187 of 3366
294 Views
In this tutorial, we will be discussing a program to print a Hut pattern.For this, we will be provided with the width of the hut to be printed (say N). Our task is to print a hut structure of the given width using stars and a gate inside the hut using line characters.Example Live Demo#include using namespace std; //printing the given hut structure int print_hut(int n){ int i, j, t; if (n % 2 == 0) { n++; } for (i = 0; i = n / 5) || (j >= n / 5 && j < n - n / 5 && i == 0) || (j == 0 && i >= n / 5) || (j == t && i > n / 5) || (i
263 Views
Problem statementGiven an array of N elements and two integers A, B which belongs to the given array. Create a Binary Search Tree by inserting element from arr[0] to arr[n-1]. The task is to find the maximum element in the path from A to B.ExampleIf array is {24, 23, 15, 36, 19, 41, 25, 35} the we can form BST as follows −If we consider A = 19 and B = 41 then maximum element between these two nodes is 41AlgorithmFind Lowest Common Ancestor(LCA) of node A and B.Find maximum node between LCA and A. Let us call it as ... Read More
430 Views
Problem statementGiven an undirected tree which has even number of vertices, we need to remove the maximum number of edges from this tree such that each connected component of the resultant forest has an even number of vertices.ExampleIn above shown tree, we can remove at max 2 edges 0-2 and 0-4 shown in red such that each connected component will have even number of vertices.AlgorithmDo DFS from any starting node as tree is connectedInitialize count of nodes in subtree rooted under current node as 0Do following recursively for every subtree of current node −If size of current subtree is even, ... Read More
138 Views
Problem statementWe have given a positive number n, and we have to find a 3*3 matrix which can be formed with combination of 0 or n and has maximum determinants.ExampleIf n = 15 then we can create matrix as follows −{{15, 15, 0}{0, 15, 15}{15, 0, 0}}For any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3. Hence answer is −2 * (15)3 = 6750AlgorithmFor any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3ExampleLet us now see an example − Live Demo#include using namespace std; int getMaxDeterminant(int ... Read More
135 Views
Problem statementGiven a range [L, R], the task is to find a pair (X, Y) such that L ≤ X < Y ≤ R and X & Y is maximum among all the possible pairs then print the bitwise AND of the found pair.ExampleIf L = 1 and R = 10 then maximum bitwise AND value is 8 which can be formed as follows −1000 # Binary representation of 8 Bitwise AND 1001 # Binary representation of 9 ---- 1000 # Final resultAlgorithmIterate from L to R and check the bitwise AND for every possible pair and print the maximum ... Read More
226 Views
Problem statementGiven an array, we partition a row of numbers A into at most K adjacent (non-empty) groups, then the score is the sum of the average of each group. What is the maximum score that can be scored?ExampleIf input array is {9, 2, 5, 3, 10} then we can partition array as follows −{9} {2, 5, 3} and {10} then average sum of this is −9 + (2 + 5 + 3)/ 3 + 10 = 22.33AlgorithmWe can use memorization technique to solve this problem −Let memo[i][k] be the best score portioning A[i to n-1] into at most K ... Read More
278 Views
Problem statementGiven an array arr[] and two integers X and Y. The task is to find a sub-array of size of at least X and at most Y with the maximum averageExampleIf input array is {2, 10, 15, 7, 8, 4} and x = 3 and Y = 3 then we can obtain maximum average 12.5 as follows −(10 + 15) / 2 = 12.5AlgorithmIterate over every sub-array of size starting from X to size Y and find the maximum average among all such sub-arrays.To reduce the time complexity, we can use prefix sum array to get the sum of ... Read More
205 Views
Problem statementGiven two same sized arrays A[] and B[]. Task is to form a third array of same size. The result array should have maximum n elements from both array. It should have chosen elements of A[] first, then chosen elements of B[] in same order as they appear in original arrays. If there are common elements, then only one element should be present in res[] and priority should be given to A[]ExampleIf input arrays are −arr1[] = {9, 17, 2, 25, 6} arr2[] = {17, 4, 8, 10, 1} then final array is: {9, 17, 25, 8, 10}Please note ... Read More
323 Views
Problem statementGiven four sides of quadrilateral a, b, c, d, find the maximum area of the quadrilateral possible from the given sides.AlgorithmWe can use below Brahmagupta’s formula to solve this problem −√(s-a)(s-b)(s-c)(s-d)In above formula s is semi-perimeter. It is calculated as follows −S = (a + b + c + d) / 2ExampleLet us now see an example − Live Demo#include using namespace std; double getMaxArea(double a, double b, double c, double d) { double s = (a + b + c + d) / 2; double area = (s - a) * (s - b) * (s ... Read More
695 Views
Problem statementGiven an array of n positive elements. we need to find the maximum bitwise AND value generated by any pair of element from the array.ExampleIf input array is {10, 12, 15, 18} then maximum value of bitwise AND is 12.AlgorithmThe result of bitwise AND operations on single bit is maximum when both bits are 1. Considering this property −Start from the MSB and check whether we have minimum of two elements of array having set valueIf yes, then that MSB will be part of our solution and be added to result otherwise we will discard that bitSimilarly, iterating from ... Read More