Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 284 of 377
Find nth number that contains the digit k or divisible by k in C++
Given two positive integers n and k, and we have to find the nth number that contains the digit k or divisible by k. The k will be in range [2 to 9]. So if n and k are 15 and 3 respectively, then output is 33. As the numbers [3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 31, 33] These are those numbers where each element contains the digit k = 3 or divisibility by k and in this nth number is 33. So output is 33.Check each number that contains k and multiple of ...
Read MoreFind and print duplicate words in std::vector using STL functions using C++.
Consider we have a list of strings. The list has some duplicate strings. We have to check which strings are occurred more than once. Suppose the string list is like [“Hello”, “Kite”, “Hello”, “C++”, “Tom”, “C++”]Here we will use the hashing technique, so create an empty hash table, then traverse each string, and for each string, s is already present in the hash, then display the string, otherwise insert into the hash.Example#include #include #include using namespace std; void displayDupliateStrings(vector strings) { unordered_set s; bool hasDuplicate = false; for (int i = 0; i
Read MoreFind number from given list for which value of the function is closest to A in C++
Suppose we have a function F(n) such that F(n) = P – (0.006*n), where P is also given. Given a list of integers and a number A. The task is to find the number from given list, for which the value of the function is nearer to A. So if P = 12, and A = 5, then list will be {1000, 2000} So output will be 1000. So if P = 12 and A = 5, then for 1000, F(1000) = 12 – (0.006 * 1000) = 6 and for 2000, F(2000) = 12 – (0.006 * 2000) = ...
Read MoreFind area of parallelogram if vectors of two adjacent sides are given using C++.
Suppose we have two vectors for two adjacent sides of a parallelogram in the form $x\hat{i}+y\hat{j}+z\hat{k}$ Our task is to find the area of parallelogram. The area of parallelogram is magnitude of the cross product of two vectors. (|A × B|)$$\rvert \vec{A}\times\vec{B}\rvert=\sqrt{\lgroup y_{1}*z_{2}-y_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*z_{2}-x_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*y_{2}-x_{2}*y_{1}\rgroup^{2}}$$Example#include #include using namespace std; float area(float A[], float B[]) { float area = sqrt(pow((A[1] * B[2] - B[1] * A[2]), 2) + pow((A[0] * B[2] - B[0] * A[2]), 2) + pow((A[0] * B[1] - B[0] * A[1]), 2)); return area; } int main() { float A[] = {3, 1, -2}; ...
Read MoreFind the longest common prefix between two strings after performing swaps on second string in C++
Suppose we have two strings str1 and str2. Find the longest common prefix between them after performing zero or more operation on the second string. In each operation, we can swap any two letters. So if str1 = “HERE”, str2 = “THERE”, then output will be 4. The second string can be made “HERET” by just swapping the characters. So the longest prefix is of length 4.As we know that we can only swap on str2. And the length of the matrix should be maximized. So the idea is to traverse str1, and check if the frequency of the current ...
Read MoreFind ceil of a/b without using ceil() function in C++.
Here we will see how to get the ceiling value of a/b without using the ceil() function. If a = 5, b = 4, then (a/b) = 5/4. ceiling(5/4) = 2. To solve this, we can follow this simple formula −$$ceil\lgroup a,b\rgroup=\frac{a+b-1}{b}$$Example#include using namespace std; int ceiling(int a, int b) { return (a+b-1)/b; } int main() { cout
Read MoreFind the longest sub-array having exactly k odd numbers in C++
Suppose we have one array with n elements. The problem is to find longest sub-array which has exactly k odd numbers. So if A = [2, 3, 4, 11, 4, 12, 7], and k = 1, then output will be 4, sub-array is [4, 11, 4, 12]We can solve this using sliding window. The task is like below −Initialize max := 0, count := 0, and start := 0for i in range 0 to n – 1, do the followingif arr[i] mod 2 is not 0, then increase count by 1while count > k and start k && start
Read MoreFind difference between sums of two diagonals in C++.
Here we will see how to get the difference between the sums of two diagonals of a given matrix. Suppose we have a matrix of order N x N, we have to get the sum of primary and secondary diagonals, then get the difference of them. To get the major diagonal, we know that the row index and column index increases simultaneously. For the second diagonal, row index and column index values are increased by this formula row_index = n – 1 – col_index. After getting the sum, take the difference and return a result.Example#include #include #define MAX 100 using ...
Read MoreFind number of diagonals in n sided convex polygon in C++
Suppose we have a number n, and we have to find the number of diagonals for n sided convex polygon. So if the n = 5, then diagonals count will be 5.As this is n-sided convex polygon, from each vertex we can draw n – 3 diagonals leaving two sided adjacent vertices and itself. So for n vertices, it will be n*(n-3), but as we are considering twice, so it will be n(n – 3)/2.Example#include using namespace std; int diagonalCount(int n) { return n * (n - 3) / 2; } int main() { int n = 8; cout
Read MoreFind the minimum number of steps to reach M from N in C++
Suppose we have two integers N and M. We have to find minimum number of steps to reach M from N, by performing given operations −Multiply the number x by 2, so x will be 2*xSubtract one from the number x, so the number will be x – 1If N = 4 and M = 6, then output will be 2. So if we perform operation number 2 on N, then N becomes 3, then perform operation number one on updated value of N, so it becomes 2 * 3 = 6. So the minimum number of steps will be ...
Read More