Programming Articles - Page 1843 of 3366

Find a pair from the given array with maximum nCr value in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:17:14

209 Views

ConceptWith respect of given an array arr[] of n positive integers, the task is to determineelements arr[i] and arr[j] from the array such that arr[i]Carr[j] is at most possible. With respect of more than 1 valid pairs, print any one of them.Input arr[] = {4, 1, 2}Output 4 2 4C1 = 4 4C2 = 4 2C1 = 4 (4, 2) is the only pairs with maximum nCr.MethodnCr is treated as a monotonic increasing function, that is n+1Cr > nCr. We can apply this fact to get close to our answer; we will select the max n among all the given integers. In ... Read More

Minimum removals from array to make GCD Greater in C++

Arnab Chakraborty
Updated on 23-Jul-2020 07:09:21

303 Views

ConceptWith respect of given N numbers, the target is to determine the minimum removal of numbers such that GCD of the remaining numbers is larger than initial GCD of N numbers. If it is impossible to increase the GCD, print “NO”.Input b[] = {1, 2, 4}Output1After removing the first element, then the new GCD is 2, which is larger than the initialGCD i.e., 1.Input b[] = {6, 9, 15, 30}Output 3The initial gcd is 3, after removing 6 and 9 to obtain a gcd of 15 which is larger than 3. We can also remove 9 and 15 to get a gcd of ... Read More

Minimum edges required to add to make Euler Circuit in C++

Arnab Chakraborty
Updated on 23-Jul-2020 06:48:55

289 Views

ConceptWith respect of a given undirected graph of b nodes and a edges, the job is to determine minimum edges needed to build Euler Circuit in the given graph.Input b = 3, a = 2 Edges[] = {{1, 2}, {2, 3}}Output 1By connecting 1 to 3, we can build a Euler Circuit.MethodWith respect of a Euler Circuit to exist in the graph we need that every node should haveeven degree because then there exists an edge that can be applied to exit the node after entering it.Now, there can be two cases −Existence of one connected component in the graphWith respect of ... Read More

Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in C++

Arnab Chakraborty
Updated on 23-Jul-2020 06:45:17

239 Views

ConceptGiven A x B Chessboard, the task is to calculate the Maximum numbers of cuts that we can build in the Chessboard so that the Chessboard is not divided into 2 parts.Examples The examples are given below −Input A = 2, B = 4Output Number of maximum cuts = 3Input A = 2, B = 2Output Number of maximum cuts = 1MethodFor A = 2, B = 2 ,we can only build 1 cut (mark in red). If we build 1 more cut then the chessboard will divide into 2 piecesFor A = 2, B = 4 ,we can makes 3 cuts (marks in red). ... Read More

Minimum Cost to cut a board into squares in C++

Arnab Chakraborty
Updated on 23-Jul-2020 06:42:24

416 Views

ConceptSuppose a board of length p and width q is given, we require to break this board into p*q squares such that cost of breaking is least. For this board, cutting cost for each edge will be given. In a nutshell, we require selecting such a sequence of cutting such that cost is minimized.Examples  With respect of above board optimal way to cut into square is −Total minimum cost in above case is 65. It is computed evaluated implementing following steps.Initial Value : Total_cost = 0 Total_cost = Total_cost + edge_cost * total_pieces Cost 5 Horizontal cut Cost = 0 + ... Read More

Minimum Cost Path with Left, Right, Bottom and Up moves allowed in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:28:25

449 Views

Suppose we have a 2D array. Where each cell of which consists number cost which represents a cost to visit through that cell, we have to find a path from top-left cell to bottom-right cell by which total cost consumed is minimum.So, if the input is like32101661319111448158710111141751234891254221141100331124221then the output will be 340 as (32 + 11 + 14 + 48 + 66 + 13 + 19 + 7 + 34 + 12 + 21 + 42 + 21) = 340To solve this, we will follow these steps −Define cell with x, y coordinate and distance parameter.Define an array matrix of ... Read More

Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:17:27

517 Views

ConceptWith respect of given three sorted arrays A, B, and C of not necessarily same sizes, compute the lowest i.e. minimum absolute difference between the maximum and minimum number of any triplet A[i], B[j], C[k] such that they are under arrays A, B and C respectively, i.e., minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])).Input−A : [ 2, 5, 6, 9, 11 ] B : [ 7, 10, 16 ] C : [ 3, 4, 7, 7 ]Output−1ExplanationWhen we select A[i] = 6 , B[j] = 7, C[k] = 7, we get the minimum difference as max(A[i], B[j], C[k]) - ... Read More

Merge k sorted arrays of different sizes in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:12:12

621 Views

Suppose we have different k sorted arrays. We have to merge these arrays and display the sorted result.So, if the input is like k = 3 and arrays are {2, 4}, {3, 5, 7}, {1, 10, 11, 12} , then the output will be 1 2 3 4 5 7 10 11 12To solve this, we will follow these steps −define one type of pair with first element is an integer and second element is another pair of integers, name it as ppi.Define an array opdefine one priority queue qfor initialize i := 0, when i < size of arr, ... Read More

Maximum Consecutive Increasing Path Length in Binary Tree in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:09:46

265 Views

Suppose we have a binary tree; we have to calculate the length of the longest path which consists of nodes with consecutive values in increasing order. Every node will be treated as a path of length 1.So, if the input is likethen the output will be 3 as (11, 12, 13) is maximum consecutive path.To solve this, we will follow these steps −Define a function solve(), this will take root, prev_data, prev_length, if not root is non-zero, then −return prev_lengthcur_data := val of rootif cur_data is same as prev_data + 1, then −return maximum of solve(left of root, cur_data, prev_length+1) ... Read More

Unique element in an array where all elements occur k times except one in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:05:14

444 Views

we have an array A. A has all elements occurring m times, but one element occurs only once. We have to find that unique element.So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.To solve this, we will follow these steps −INT_SIZE := 8 * size of an integer type variableDefine an array count of size: INT_SIZE. and fill with 0for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do −for initialize j := 0, when j < size, update (increase ... Read More

Advertisements