Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 508 of 719
249 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
276 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
157 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
255 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
116 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
150 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
205 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
193 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
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
161 Views
Suppose we have two values a and b. We have to find x and y, such that ax – by = 0. So if a = 25 and b = 35, then x = 7 and y = 5.To solve this, we have to calculate the LCM of a and b. LCM of a and b will be the smallest value that can make both sides equal. The LCM can be found using GCD of numbers using this formula −LCM (a,b)=(a*b)/GCD(a,b)Example Live Demo#include #include using namespace std; void getSmallestXY(int a, int b) { int lcm = (a * b) / __gcd(a, b); cout