Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 368 of 377

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 556 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 element from array without using conditional operator in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 336 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 elements to a given value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 225 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
Arnab Chakraborty
Updated on 01-Nov-2019 219 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
Arnab Chakraborty
Updated on 01-Nov-2019 218 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

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 406 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

Read More

Find elements of array using XOR of consecutive elements in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 382 Views

Consider we have to find a list of n elements. But we have the XOR value of two consecutive elements of the actual array. Also the first element of the actual is given. So if the array elements are a, b, c, d, e, f, then the given array will be a^b, b^c, c^d, d^e and e^f.As the first number is given, named a, that can help us to find all numbers. If we want to find the second element of the actual array, then we have to perform b = a ^ arr[i], for second element c = b ...

Read More

Find duplicates under given constraints in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 223 Views

Suppose we have a list with 6 different numbers. Only one number is repeated five times. So there are total 10 elements in the array. Find duplicate numbers using only two comparisons. If the list is like [1, 2, 3, 4, 4, 4, 4, 4, 5, 6], so output is 4.As there are only 10 numbers, then for any type of duplicate numbers, the range of numbers will be placed from index 3 to 5. By checking these indices, we can find the result.Example#include using namespace std; int getDuplicate(int array[]) {    if (array[3] == array[4])       return array[3];    else if (array[4] == array[5])       return array[4];    else       return array[5]; } int main() {    int a[] = {1, 2, 3, 4, 4, 4, 4, 4, 5, 6};    cout

Read More

Find duplicates in O(n) time and O(1) extra space - Set 1 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 377 Views

Suppose we have a list of numbers from 0 to n-1. A number can be repeated as many as possible number of times. We have to find the repeating numbers without taking any extra space. If the value of n = 7, and list is like [5, 2, 3, 5, 1, 6, 2, 3, 4, 5]. The answer will be 5, 2, 3.To solve this, we have to follow these steps −for each element e in the list, do the following steps −sign := A[absolute value of e]if the sign is positive, then make it negativeOtherwise it is a repetition.Example#include ...

Read More

Find cubic root of a number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 488 Views

Here we will see how to get the cubic root of a number. Suppose a number is say 27, then the cubic root of this number is 3. To solve this problem, we will define our own logic without using some library functions. We will use the binary search approach. We have to follow these steps to solve this problem.Suppose we have threshold value like threshold = 0.000001Start the left value as 0, and right value as numbercalculate mid := (left + right)/2if the absolute value of (number – mid3) is less than threshold, then return mid as answerif mid3 ...

Read More
Showing 3671–3680 of 3,768 articles
« Prev 1 366 367 368 369 370 377 Next »
Advertisements