Found 7197 Articles for C++

Find the longest sub-array having exactly k odd numbers in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:41:44

123 Views

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

Find the longest path in a matrix with given constraints in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:38:25

529 Views

Suppose we have one square matrix of order n. It has all distinct elements. So we have to find the maximum length path, such that all cells along the path are in increasing order with a difference of 1. From one cell we can move to four directions. Left, Right, Top and Bottom. So if the matrix is like −129538467So the output will be 4. As the longest path is 6→7→8→ 9To solve this problem, we will follow this idea. We will calculate longest path beginning with every cell. Once we have got the longest for all cells, we return ... Read More

Find number of cavities in a matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:39:31

137 Views

Consider one matrix is given. We have to find the number of cavities in the matrix. One element is denoted as cavity when all other elements surrounding it is greater than the element. So if the matrix is like −456715456So the output is 1.We simply check the surrounding elements and form the decision.Example#include #define MAX 100 using namespace std; int numberOfCavities(int array[][MAX], int n) {    int arr[n + 2][n + 2];    int count = 0;    for (int i = 0; i < n + 2; i++) {       for (int j = 0; j < ... Read More

Find the longest common prefix between two strings after performing swaps on second string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:35:42

322 Views

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 More

Find number from given list for which value of the function is closest to A in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:33:32

145 Views

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 More

Find nth number that contains the digit k or divisible by k in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:30:28

594 Views

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 More

Find the last non repeating character in string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:39:55

234 Views

Suppose we have a string str. We have to find the last non-repeating character in it. So if the input string is like “programming”. So the first non-repeating character is ‘n’. If no such character is present, then return -1.We can solve this by making one frequency array. This will store the frequency of each of the character of the given string. Once the frequency has been updated, then start traversing the string from the last character one by one. Then check whether the stored frequency is 1 or not, if 1, then return, otherwise go for previous character.Example#include ... Read More

Find the largest number that can be formed with the given digits in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:37:32

312 Views

Suppose we have an array of digits. We have to find the maximum number that can be obtained using all digits of the array. So if the array is like [3, 3, 9, 6, 2, 5], then maximum number can be 965332.From the problem, we can see that we can easily sort the digits in non-increasing order, then print them. But we can solve this using more efficient way. We can create one array of size 10 to store the frequency of each digit, then print the numbers from 9 to 0 accordingly.Example Live Demo#include #include using namespace std; ... Read More

Find minimum x such that (x % k) * (x / k) == n in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:38:56

185 Views

Given two positive integers n and k, and we have to find the positive integer x, such that (x % k)*(x / k) is same as n. So if the n and k are 4 and 6 respectively, then the output will be 10. So (10 % 6) * (10 / 6) = 4.As we know that the value of x % k will be in range [1 to k – 1] (0 is not included) Here we will find possible integer in the range that divides n and hence the given equation becomes: x = (n * k) / ... Read More

Find the largest interval that contains exactly one of the given N integers In C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:35:57

204 Views

Suppose we have an array of N distinct integers. We have to find the max element in an interval [L, R] such that the interval contains exactly one of the given N integers and 1

Advertisements