Found 7347 Articles for C++

Find last index of a character in a string in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:19:59

486 Views

Suppose we have a string str. We have another character ch. Our task is to find the last index of ch in the string. Suppose the string is “Hello”, and character ch = ‘l’, then the last index will be 3.To solve this, we will traverse the list from right to left, if the character is not same as ‘l’, then decrease index, if it matches, then stop and return result.Example#include using namespace std; int getLastIndex(string& str, char ch) {    for (int i = str.length() - 1; i >= 0; i--)       if (str[i] == ch)   ... Read More

Find last element after deleting every second element in array of n integers in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:12:04

356 Views

Consider we have one circular array containing integers from 1 to n. Find the last element, that would remain in the list after deleting every second element starting from first element. If the input is 5, then array will be [1, 2, 3, 4, 5]. Start from 1. After deleting each second element will be like −1 0 3 4 5 1 0 3 0 5 0 0 3 0 5 0 0 3 0 0So the element that remains in the list is 3.We will solve this problem using recursion. Suppose n is even. The numbers 2, 4, 6 ... Read More

Find largest word in dictionary by deleting some characters of given string in C++

Samual Sam
Updated on 01-Nov-2019 10:54:54

149 Views

Consider we have a dictionary, and a string s. Find the longest string in the dictionary, that can be formed by deleting some characters of the string s. Suppose the s is “apbreoigroakml”, The dictionary has {“prog”, “ram”, “program”}, then the result will be “program”.To solve this, we will traverse all dictionary words, and for each word, we will check whether the subsequence of the given string and is longest of all such words. Finally return longest word with given string as subsequence.Example#include #include using namespace std; bool isSubSequence(string s1, string s2) {    int m = s1.length(), n = ... Read More

Find largest element from array without using conditional operator in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:07:30

209 Views

Suppose we have an array A with some elements. We have to find the largest element in the array A, but the constraint is, we cannot use any conditional operator. So if A = [12, 63, 32, 24, 78, 56, 20], then maximum element will be 78.To solve this problem, we will use the bitwise AND operation. at first we will insert one extra element INT_MAX (where all bit is 1) in array. Then we will try to find maximum AND value of any pair from the array. This obtained max value will contain AND value of INT_MAX and largest ... Read More

Find k closest numbers in an unsorted array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:06:33

359 Views

Consider we have an array A with few elements. The array is unsorted. We have two other value X and k. Our task is to find the k number of nearest elements of X from the array A. If the element X is present in the array, then it will not be shown in the output. If A = [48, 50, 55, 30, 39, 35, 42, 45, 12, 16, 53, 22, 56] and X = 35, k = 4. The output will be 30, 39, 42, 45.To solve this, we will use the heap data structure. The steps will be ... Read More

Find k closest elements to a given value in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:00:58

94 Views

Consider we have an array A with few elements. We have two other value X and k. Our task is to find the k number of nearest elements of X from the array A. If the element X is present in the array, then it will not be shown in the output. If A = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56] and X = 35, k = 4. The output will be 30, 39, 42, 45.To solve this, we will use the binary search approach. Using this we will get the crossover point. ... Read More

Find if n can be written as product of k numbers in C++

Arnab Chakraborty
Updated on 01-Nov-2019 09:57:45

114 Views

Suppose we have a number N. We have another number k. We have to check the number can be represented using k numbers or not. Suppose a number 54, and k = 3, then it will print the numbers like [2, 3, 9], if it cannot be represented, then print that.To solve this, we will find all prime factors of N, and store them into a vector, then to find k numbers greater than 1, we check the size of the vector is greater than k or not. If size is less than k, then return -1, otherwise print first ... Read More

Find if array can be divided into two subarrays of equal sum in C++

Arnab Chakraborty
Updated on 01-Nov-2019 09:54:12

128 Views

Suppose we have an array A. We have to check whether we can split the array into two parts, whose sum are equal. Suppose the elements are [6, 1, 3, 2, 5], then [6, 1], and [2, 5] can be two subarrays.This problem can be solved easily by following these rules. We have to find the sum of all elements of the array at first, then for each element of the array, we can calculate the right sum by using total_sum – sum of elements found so far.Example#include #include using namespace std; void displaySubArray(int arr[], int left, int right) {    cout

Find frequency of smallest value in an array in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:23:53

308 Views

Here we will see how to find the frequency of smallest element in an array. Suppose the array elements are [5, 3, 6, 9, 3, 7, 5, 8, 3, 12, 3, 10], here smallest element is 3, and the frequency of this element is 4. So output is 4.To solve this we will find the smallest element of the list, then we count the occurrences of first numbers, and that will be the result.Example#include using namespace std;    int min_element(int arr[], int n){    int min = arr[0];    for(int i = 1; i

Find Excel column number from column title in C++

Arnab Chakraborty
Updated on 01-Nov-2019 07:21:57

189 Views

We know that the excel column numbers are alphabetic. It starts from A, and after Z, it will AA, AB, to ZZ, then again AAA, AAB, to ZZZ and so on. So column 1 is A, column 27 is Z. Here we will see how to get the column letter if number of column is given. So if the column number is 80, then it will be CB.Suppose we have a number n, and its value is 28, then we need to take reminder with 26. If the remainder is 0, then the number is 26, 52 and so on. ... Read More

Advertisements