Found 7197 Articles for C++

C++ Program to Generate a Graph for a Given Fixed Degree Sequence

Ravi Ranjan
Updated on 29-May-2025 19:18:23

338 Views

In this article, we will understand how to generate a graph for a given fixed-degree sequence. The degree of each node is given in the form of an array. The graph generated will be an undirected graph where the degree of each node will correspond to the given degree array. Example The following example generates a graph and adjacency matrix for the given degree sequence: Input: Degree Sequence: {3, 2, 3, 2} => Degree(Node a) = 3, Degree(Node b) = 2, Degree(Node c) = 3, Degree(Node d) = 2 ... Read More

C++ Program to Generate a Random Directed Acyclic Graph (DAG) for a Given Number of Edges

Ravi Ranjan
Updated on 26-May-2025 18:29:29

679 Views

A Directed Acyclic Graph or DAG is a directed graph (a graph where each edge has a direction) that has no cycles present i.e. if we traverse along the direction of the edges then no closed loops are formed along any path. A DAG is always topologically ordered, i.e. for each edge in the graph, the start vertex of the edge(u) occurs earlier in the sequence than the ending vertex(v) of the edge i.e.(u {B C} B -> {Isolated Vertex} C -> {Isolated Vertex} D -> {B E} E -> {C} Steps to Generate Random Directed Acyclic Graph The ... Read More

C++ Program to Find the peak element of an array using Binary Search approach

Ravi Ranjan
Updated on 04-Jun-2025 18:42:56

986 Views

The peak element in an array is an element that is greater than its neighbor elements i.e., its left and right element. If the peak element is the starting element, then it should be greater than the next element (second element). If the peak element is the last element, then it should be greater than its previous value i.e., the second last element. In this article, we have an array of integers. Our task is to use the binary search algorithm to find the peak element present in the given array. Example Here are two examples to understand the ... Read More

C++ Program to Find the Minimum element of an Array using Binary Search approach

Ravi Ranjan
Updated on 04-Jun-2025 16:23:27

291 Views

The binary search works on the divide and conquer principle as it keeps dividing the array into half before searching. For applying the binary search algorithm, the given array should be sorted. Since the array is sorted we do not need to search the minimum element in the array. In this article, the given array has strictly decreasing elements in the left sub-array till it reaches the minimum element and the right sub-array has strictly increasing elements. For example: {40, 30, 20, 10, 25, 35}. In this example, the array is decreasing till it ... Read More

C++ Program to Find Maximum Element in an Array using Binary Search

Ravi Ranjan
Updated on 04-Jun-2025 15:36:00

514 Views

The binary search works on the divide and conquer principle as it keeps dividing the array into half before searching. For applying the binary search algorithm, the given array should be sorted. Since the array is sorted we do not need to search the maximum element in the array. Here, the given array is a Bitonic array. A bitonic array is an array in which the left sub-array has strictly increasing elements till it reaches the peak element and the right sub-array has strictly decreasing elements. For example: {10, 20, 30, 40, 35, 25}. In this example, the array is ... Read More

C++ Program to Find Minimum Element in an Array using Linear Search

Ravi Ranjan
Updated on 15-Apr-2025 15:28:30

984 Views

Linear search is a sequential searching algorithm where we traverse every element within the input array and compare it with the key element to be found. The minimum element refers to the smallest element in the array. In this article, we have an array of integers. Our task is to find the minimum element in that array using linear search. Here are the three approaches you can use to find the minimum element: Using Value Using Index Using Pointer Using Value In this approach, ... Read More

C++ Program to Perform Uniform Binary Search

Ravi Ranjan
Updated on 18-Aug-2025 16:07:46

413 Views

In this article, we have a sorted array of integers. Our task is to perform a uniform binary search to search for a target element. What is Uniform Binary Search? The uniform binary search is an improved version of the binary search algorithm where we use a pre-computed lookup table instead of calculating the middle element every time. The lookup table has elements that are powers of 2, starting from the larger value to 0, to decide the step for next iteration. The time complexity for uniform binary search is O(log n).Consider the following example scenarios to understand the ... Read More

C++ Program to Search Sorted Sequence Using Divide and Conquer with the Aid of Fibonacci Numbers

George John
Updated on 30-Jul-2019 22:30:25

552 Views

In this C++ program we implement a Divide and Conquer approach using Fibonacci numbers. Using Fibonacci numbers, we calculate mid of data array to search the data item. The time complexity of this approach is O(log(n)).AlgorithmBegin    Assign the data to the array in a sorted manner.    Take input of the element to be searched.    Call FibonacciSearch() function.    Calculate the mid value using ‘start+fib[index-2]’ expression.    If the chosen item is equal to the value at mid index, print result and return to main.    If it is lesser than the value at mid index, proceed with ... Read More

C++ Program to Find kth Largest Element in a Sequence

Ravi Ranjan
Updated on 18-Apr-2025 16:55:33

396 Views

In this article, we have an unsorted array. Our task is to find the kth maximum element of that array using C++. Here is an example to understand the meaning of k. If k =2, you can say the second highest value, for k =3, the third highest value. The approaches that we will be using are mentioned below: Using Sorting Using Max Heap Using Min Heap Using Quick Select Using Binary Search Tree Using ... Read More

C++ Program to Search for an Element in a Binary Search Tree

Ravi Ranjan
Updated on 16-Jun-2025 17:41:41

2K+ Views

A Binary Search Tree (BST) is a type of binary tree such that the left subtree has elements smaller than the root element and the right subtree has elements greater than the root element. In this article, our task is to search for an element in the given Binary Search Tree. Characteristics of Binary Search Tree Here are some of the characteristics of the BST: The left subtree of the BST has elements less than the root element. The right subtree of the BST has elements greater than the root ... Read More

Advertisements