Found 7197 Articles for C++

Find most significant set bit of a number in C++

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

2K+ Views

Here we will see if a number is given, then how to find the value of Most Significant Bit value, that is set. The value is power of 2. So if the number is 10, MSB value will be 8.We have to find the position of MSB, then find the value of the number with a set-bit at kth position.Example#include #include using namespace std; int msbBitValue(int n) {    int k = (int)(log2(n));    return (int)(pow(2, k)); } int main() {    int n = 150;    cout

Find middle of singly linked list Recursively in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:26:31

508 Views

Consider we have a list of numbers; our task is to find the middle of the linked list using recursion. So if the list elements are [12, 14, 18, 36, 96, 25, 62], then the middle element is 36.To solve this problem, we will count total number of nodes in the list in recursive manner and do half of this. Then rolling back through recursion decrement n by 1 in each call, return element where n is zero.Example#include #include using namespace std; class Node{    public:       int data;       Node *next; }; Node* getNode(int data){ ... Read More

Find maximum element of each row in a matrix in C++

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

686 Views

Consider we have a matrix, our task is to find the maximum element of each row of that matrix and print them. This task is simple. For each row, reset the max, and find the max element, and print it. Let us see the code for better understanding.Example#include #define MAX 10 using namespace std; void largestInEachRow(int mat[][MAX], int rows, int cols) {    for (int i = 0; i < rows; i++) {       int max_row_element = mat[i][0];    for (int j = 1; j < cols; j++) {       if (mat[i][j] > max_row_element)          max_row_element = mat[i][j];    }    cout

Find maximum element of each column in a matrix in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:21:54

560 Views

Consider we have a matrix, our task is to find the maximum element of each column of that matrix and print them. This task is simple. For each column, reset the max, and find the max element, and print it. Let us see the code for better understanding.Example#include #define MAX 10 using namespace std; void largestInEachCol(int mat[][MAX], int rows, int cols) {    for (int i = 0; i < cols; i++) {       int max_col_element = mat[0][i];    for (int j = 1; j < rows; j++) {       if (mat[j][i] > max_col_element)          max_col_element = mat[j][i];    }    cout

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

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

640 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

509 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

222 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

277 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

473 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

168 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

Advertisements