Found 7347 Articles for C++

Find the length of each string element in the Numpy array in C++

Arnab Chakraborty
Updated on 04-Nov-2019 07:52:59

89 Views

Here we will see how to get the length of each string element in the Numpy Array. Numpy is a library for Numeric Python, and it has very powerful array class. Using this we can store data in an array like structure. To get the length we can follow two different approach, these are like below −Exampleimport numpy as np str_arr = np.array(['Hello', 'Computer', 'Mobile', 'Language', 'Programming', 'Python']) print('The array is like: ', str_arr) len_check = np.vectorize(len) len_arr = len_check(str_arr) print('Respective lengts: ', len_arr)OutputThe array is like: ['Hello' 'Computer' 'Mobile' 'Language' 'Programming' 'Python'] Respective lengts: [ 5 8 6 8 ... Read More

Find the first, second and third minimum elements in an array in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:21:51

184 Views

Suppose we have an array of n elements. We have to find the first, second and the third minimum elements in the array. First minimum is the minimum of the array, second min is minimum but larger than the first one, and similarly the third min is minimum but greater than second min.Scan through each element, then check the element, and relate the condition for first, second and third min elements conditions to solve this problem.Example#include using namespace std; int getThreeMins(int arr[], int n) {    int first = INT_MAX, sec = INT_MAX, third = INT_MAX;       for ... Read More

Find the final X and Y when they are Altering under given condition in C++

Arnab Chakraborty
Updated on 08-Jul-2020 07:33:12

60 Views

Consider we have the initial values of two positive integers X and Y. Find the final value of X and Y, such that there will be some alteration as mentioned below −step1 − If X = 0 and Y = 0 then terminate the process, otherwise go to step2step2 − If X >= 2Y, then set X = X – 2Y, and go to step1, otherwise go to step3step3 − If Y >= 2X, then set Y = Y – 2X, and go to step1, otherwise end the process.The number X and Y will be in range [0 and 1018] ... Read More

Find sum of factorials in an array in C++

Arnab Chakraborty
Updated on 04-Nov-2019 07:43:09

156 Views

Consider we have an array A, which is sorted. It has all elements appears twice, but one element is present for only one time. We have to find that element. If the array is [1, 1, 3, 3, 4, 4, 5, 6, 6, 7, 7, 9, 9], so the single element is 5.We will use the binary search approach to solve this. All elements before the single element has their first occurrence at index 0, 2, 4, … and first occurrence at index 1, 3, 5, … but after the single element, all occurrences of the first number will be ... Read More

Find numbers a and b that satisfy the given condition in C++

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

434 Views

Consider we have an integer n. Our task is to find two numbers a and b, where these three conditions will be satisfied.a mod b = 0a * b > na / b < nIf no pair is found, print -1.For an example, if the number n = 10, then a and b can be a = 90, b = 10. This satisfies given rules.To solve this problem, we will follow these steps −Let b = n. a can be found using these three conditionsa mod b = 0 when a is multiple of ba / b < n, so ... Read More

Find N digits number which is divisible by D in C++

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

121 Views

Suppose we have two numbers N and D. We have to find N digit number, that is divisible by D. If N is 3, and D is 5, then the number can be 500. This can be solved easily. If D is 10 and N is 1, then it will be impossible. We can put D, and suppose the D has m number of digits, then attach N – m number of 0s to make it N digit number and divisible by D.Example#include using namespace std; string nDigitDivByD(int n, int d) {    string ans = "";    if (d ... Read More

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

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

1K+ 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

304 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

554 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

430 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

Advertisements