Found 7197 Articles for C++

Minimum height of a triangle with given base and area in C++

Narendra Kumar
Updated on 22-Nov-2019 09:49:26

282 Views

DescriptionGiven two integers a and b, find the smallest possible height such that a triangle of atleast area ‘a’ and base ‘b’ can be formed.ExampleIf a = 16 and b = 4 then minimum height would be 8AlgorithmArea of triangle can be calculate using below formula −area = ½ * height * baseUsing above formula, height can be calculated as −height = (2 * area) / baseSo Minimum height is the ceil() of the height obtained using above formula.Example#include #include using namespace std; float minHeight(int area, int base) {    return ceil((2 * area) / base); } int main() {    int area = 16, base = 4;    cout

C / C++ Program for Dijkstra's shortest path algorithm

sudhir sharma
Updated on 08-Nov-2023 00:13:33

27K+ Views

We are given a graph with a source vertex in the graph. And we have to find the shortest path from the source vertex to all other vertices of the graph. The Dijikstra's algorithm is a greedy algorithm to find the shortest path from the source vertex of the graph to the root node of the graph. Algorithm Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree. Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source vertex so that it is ... Read More

Binomial Coefficient in C++

sudhir sharma
Updated on 22-Nov-2019 09:22:57

7K+ Views

Binomial coefficient denoted as c(n, k) or ncr is defined as coefficient of xk in the binomial expansion of (1+X)n.The Binomial coefficient also gives the value of the number of ways in which k items are chosen from among n objects i.e. k-combinations of n-element set. The order of selection of items not considered.Here, we are given two parameters n and k and we have to return the value of binomial coefficient nck .ExampleInput : n = 8 and k = 3 Output : 56There can be multiple solutions to this problem, General SolutionThere is a method to calculate the value ... Read More

Binary Search a String in C++

Ravi Ranjan
Updated on 06-Aug-2025 19:01:28

4K+ Views

The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element. In this article, we are given a sorted array of strings, and our task is to search for the given string using a binary search algorithm. ... Read More

Binary representation of previous number in C++

sudhir sharma
Updated on 09-Jul-2020 11:40:57

302 Views

In this problem, we are given the binary representation of a number and we have to find the binary representation of the previous number i.e. the number that is resulted after subtracting one from the given number.Binary representation of a number is changing the base of the number to base 2 and representing the number using only 0 or 1.For example, Binary representation of 23 is 10111.So, here we would be given a number, let's say n in binary form. And we have to find the binary representation of n-1.To solve this problem, we need to know the basics of ... Read More

Binary representation of next number in C++

sudhir sharma
Updated on 22-Nov-2019 09:05:48

535 Views

In this problem, we are given the binary representation of a number and we have to find the binary representation of the next number i.e. the number that is resulted after adding one to the given number.Binary representation of a number is changing the base of the number to base 2 and representing the number using only 0 or 1.For example, Binary representation of 14 is 1110.So, here we would be given a number, let's say n in binary form. And we have to find the binary representation of n+1.To solve this problem, we need to know the basics of ... Read More

Binary Insertion Sort in C++

sudhir sharma
Updated on 22-Nov-2019 09:02:40

4K+ Views

Binary Insertion sort is a special type up of Insertion sort which uses binary search algorithm to find out the correct position of the inserted element in the array.Insertion sort is sorting technique that works by finding the correct position of the element in the array and then inserting it into its correct position.Binary search is searching technique that works by finding the middle of the array for finding the element.As the complexity of binary search is of logarithmic order, the searching algorithm’s time complexity will also decrease to of logarithmic order.Implementation of binary Insertion sort. this program is a ... Read More

Biggest number by arranging numbers in certain order in C++

sudhir sharma
Updated on 22-Nov-2019 07:45:31

181 Views

In this problem, we are given an array of numbers and we have to find the largest value that can be made by changing them in a certain way. the condition for the arrangement is, the order of even numbers and odd numbers shall remain the same i.e. the order of all even numbers cannot be changed.let's take an example to understand the concept better, Input : {17, 80, 99, 27, 14 , 22} Output: 801799271422 Explanation: the order of Even and Odd numbers is : Even : 80 14 22 Odd : 17 99 27Here 99 is the biggest ... Read More

BFS for Disconnected Graph in C++

sudhir sharma
Updated on 22-Nov-2019 07:37:46

944 Views

Disconnected graph is a Graph in which one or more nodes are not the endpoints of the graph i.e. they are not connected.A disconnected graph…Now, the Simple BFS is applicable only when the graph is connected i.e. all vertices of the graph are accessible from one node of the graph. in the above disconnected graph technique is not possible as a few laws are not accessible so the following changed program would be better for performing breadth first search in a disconnected graph.Example#include using namespace std; void insertnode(vector adj[], int u, int v) {    adj[u].push_back(v); } void breathFirstSearch(int u, ... Read More

Best meeting point in 2D binary array in C++

sudhir sharma
Updated on 22-Nov-2019 07:32:49

223 Views

In this problem, we are given a 2D binary array i.e. it has values that are either 0 or 1, where 1 is marked as a home of a person of the group. And people of the group want to meet. So, they need to minimise the total distance travelled by them for meeting at a common point. A valid meeting point can be anywhere but not at anyone home.For find minimum distance a formula is created, this is named as manhattan distance, where distance −(p1, p2) = |p2.x| + |p2.y - p1.y|.Let’s take an example, to make the concept ... Read More

Advertisements