Found 7197 Articles for C++

Find the Second Largest Element in a Linked List in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:00:00

472 Views

Here we will see the second largest element in the linked list. Suppose there are n different nodes with numerical values. So if the list is like [12, 35, 1, 10, 34, 1], then second largest element will be 34.This process is similar to the finding of second largest element in an array, we will traverse through the list and find second largest element by comparing.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) {    Node* new_node = new Node;    new_node->data = ... Read More

Find the next identical calendar year in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:54:41

240 Views

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ... Read More

Find the fractional (or n/k – th) node in linked list in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:51:30

267 Views

Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.To solve this we have to follow some steps like below −Take two pointers called ... Read More

Find the count of substrings in alphabetic order in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:37:36

150 Views

Suppose we have a string of length n. It contains only uppercase letters. We have to find the number of substrings whose character is occurring in alphabetical order. Minimum size of the substring will be 2. So if the string is like: “REFJHLMNBV”, and substring count is 2, they are “EF” and “MN”.So to solve this, we will follow these steps −Check whether str[i] + 1 is same as the str[i+1], if so, then increase the result by 1, and iterate the string till next character which is out of alphabetic order, otherwise continue.Example Live Demo#include using namespace std; int countSubstr(string ... Read More

Find the count of Strictly decreasing Subarrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:34:05

245 Views

Suppose we have an array A. And we have to find the total number of strictly decreasing subarrays of length > 1. So if A = [100, 3, 1, 15]. So decreasing sequences are [100, 3], [100, 3, 1], [15] So output will be 3. as three subarrays are found.The idea is find subarray of len l and adds l(l – 1)/2 to result.Example Live Demo#include using namespace std; int countSubarrays(int array[], int n) {    int count = 0;    int l = 1;    for (int i = 0; i < n - 1; ++i) {       ... Read More

Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:31:23

109 Views

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.Example Live Demo#include #include using namespace std; long long countNumbers(int n) {    return (long long)(pow(2, n + 1)) - 2; } int main() {    int n = 3;    cout

Find the closest and smaller tidy number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:23:29

142 Views

Suppose we have a number n, we have to find the closest and smaller tidy number of n. So a number is called tidy number, if all of its digits are sorted in non-decreasing order. So if the number is 45000, then the nearest and smaller tidy number will be 44999.To solve this problem, we will traverse the number from end, when the tidy property is violated, then we reduce digit by 1, and make all subsequent digit as 9.Example Live Demo#include using namespace std; string tidyNum(string number) {    for (int i = number.length()-2; i >= 0; i--) {   ... Read More

Find the center of the circle using endpoints of diameter in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:20:38

196 Views

Suppose we have two endpoints of diameter of a circle. These are (x1, y1) and (x2, y2), we have to find the center of the circle. So if two points are (-9, 3) and (5, -7), then the center is at location (-2, -2).We know that the mid points of two points are −$$(x_{m},y_{m})=\left(\frac{(x_{1}+x_{2})}{2},\frac{(y_{1}+y_{2})}{2}\right)$$Example Live Demo#include using namespace std; class point{    public:       float x, y;       point(float x, float y){          this->x = x;          this->y = y;       }       void display(){          cout

Find sum of a number and its maximum prime factor in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:17:22

182 Views

Suppose we have a positive number n, and we have to find the sum of N and its maximum prime factor. So when the number is 26, then maximum prime factor is 13, so sum will be 26 + 13 = 39.Approach is straight forward. Simply find the max prime factor, and calculate the sum and return.Example Live Demo#include #include using namespace std; int maxPrimeFact(int n){    int num = n;    int maxPrime = -1;    while (n % 2 == 0) {       maxPrime = 2;       n /= 2;    }    for (int i = 3; i 2)    maxPrime = n;    return maxPrime; } int getRes(int n) {    int sum = maxPrimeFact(n) + n;    return sum; } int main() {    int n = 26;    cout

Find square root of number upto given precision using binary search in C++

Farhan Muhamed
Updated on 04-Aug-2025 19:02:57

2K+ Views

In this article, we will learn how to find the square root of a number up to a given precision by using binary search algorithm and implement it in C++. Before dive into the concept, make sure that you have a basic understanding of binary search algorithm. Square Root of a Number Upto a Given Precision In this problem, you are given a positive floating point number N and a positive integer P. The task is to find the square root of N up to P decimal places using binary search. Scenario 1 Input: N = ... Read More

Advertisements