Found 7197 Articles for C++

C++ program to represent the Fraction of Two Numbers in the String Format

Prateek Jangid
Updated on 10-Aug-2022 10:04:34

1K+ Views

We are given two integer numerators and a denominator. We need to represent the fraction of these two integers in string format. If a certain decimal is repeating, we need a bracket to show its repeating sequence. Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Determine the integral quotient (absolute part before to the decimal point) before determining the fractional portion. Insert the remainder (numerator % denominator) in a map with the key being the remainder and the value being the index position at which this remainder occurs to see if ... Read More

C++ program to replace an element makes array elements consecutive

Prateek Jangid
Updated on 10-Aug-2022 10:01:57

584 Views

We are given an array of elements. We need to find if it is possible to change the value of any one element to make the array elements consecutive. If not possible, return -1 ; otherwise, the element needs to be changed. Let's suppose we have an array {4, 3, 9, 5, 6} and we have to sort this given array. Then start from the smallest and largest element checking the number of mismatches. If the number of mismatches is more than 1 on both sides of the array, the answer is -1. Otherwise, it is possible to get the ... Read More

C++ program to search an element in a sorted rotated array

Ravi Ranjan
Updated on 06-Jun-2025 19:16:12

1K+ Views

A sorted and rotated array is an array that is sorted in ascending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated. The array can be said to be split into two halves and each half is a sorted array. For example: {5, 6, 7, 1, 2, 3} is a sorted and rotated array and {5, 6, 7, 8, 2, 5, 4, 5} is not a sorted and rotated array. In this article, we have an array of integers. Our task is ... Read More

Searching an Element in Doubly Circular Linked List using C++

Prateek Jangid
Updated on 10-Aug-2022 09:56:26

281 Views

Given a double circular linked list and key, we have to search the key in the linked list and give a proper message if found. Suppose we have a linked list with the specific characters, and we have to search for an element in it. So let's start with the following linked list − 5 8 9 2 4 We will use 4 as a key to finding the solution to the given problem. A double-linked list has no fixed head, so we will start from any node and then mark that node as ... Read More

C++ program to find Second Smallest Element in a Linked List

Prateek Jangid
Updated on 10-Aug-2022 09:54:16

358 Views

An ordered collection of data elements, each with a link to its next element (and sometimes its predecessor), Suppose there is a linked list, then we need to find the second smallest element. The below are the following scenarios. Let’s assume some simple input and output scenarios Let’s assume this scenario that we are having a linked list contains elements in it are "8->4->6->2->9, ". Then after iterating entire linked list, the second smallest element will be 8. Input = 8->4->6->2->9 Output = 8 Programing way of implementing the linked list Node* head = new Node(8); head->next = new ... Read More

C++ program to find Second most repeated word in a sequence

Prateek Jangid
Updated on 10-Aug-2022 09:52:54

445 Views

We are given an array of words, and we need to find the word whose frequency is the second largest in the array. Let’s assume some simple input and output scenarios Let’s assume we are having an array which consists of elements like [“point, ” “world, ” “articles, ” “articles, ” “articles, ” “world, ” “world, ” “world, ” “point”]. The frequency of words are − “point”: 2 “world”: 4 “articles”: 3 // This will be the second most repeated word in the array. So the second most repeated word is “articles, ” and our output is “articles.” Let’s ... Read More

Smallest integer > 1 which divides every element of the given array: Using C++

Prateek Jangid
Updated on 10-Aug-2022 09:51:06

249 Views

In this article, we are given integers in an array, and we must find the smallest number which is greater than 1 that divides all the elements in the array. For example, let us consider a sample array [30, 90, 15, 45, 165]. vector arr = {30, 90, 15, 45, 165}; result = solve(arr); Now we can find the array's GCD(greatest common divisor). If it comes out to be 1 that means that only 1 can divide the whole array, and we can return -1 or "Not possible." If it's an integer, then this integer divides the whole ... Read More

C++ program to Replace Nodes with Duplicates in Linked List

Prateek Jangid
Updated on 10-Aug-2022 09:49:07

404 Views

In this article, we are given a linked list that contains elements from 1 to n and duplicates. Elements 1 to n will always be there with duplicates from [1..n]. We need to replace every duplicate element with n+1, n+2, and so on. Lets consider an example 1→2→2→4→5→3→6→6 Next n = 42. So every duplicate is replaced with n+1, n+2, and so on. The next 42 is replaced with 47, and the next 46 is replaced with 48, leaving the first instance as it is. First of all, we need to construct a binary tree in the main method as ... Read More

C++ program to Replace Every Matrix Element with Maximum of GCD of Row or Column

Prateek Jangid
Updated on 10-Aug-2022 09:47:24

450 Views

In this method, we need to replace every element in the given matrix with the maximum Greatest Common Divisor (GCD) of that row and column. Let us look at some input scenarios − Suppose we are given a 2D matrix of dimensions m*n is; Input: [[3, 2, 1, 4] [7, 6, 2, 8] [14, 20, 25, 17]]; In the above matrix, row 1 gcd(3, 2, 1, 4) = 1 and Column 2 gcd(3, 7, 14) = 1. So element 2 (1st row and 2nd column) becomes the maximum (1, 1) = 1. So on for all elements and our ... Read More

Replace every element with the least greater element on its right using C++

Prateek Jangid
Updated on 10-Aug-2022 09:27:51

241 Views

To find the least greater element on the right of an array of integers, we need to consider an array of integers. For each element, we need to find an element that is greater than the current element and is the least from all the candidate elements we have. Suppose we have elements such as [5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88]. vector arr = {5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88}; solve(arr); We can list our requirements and then think over the requirements to find a solution. We need a ... Read More

Advertisements