Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 197 of 377

Find the fractional (or n/k – th) node in linked list in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 313 Views

Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.To solve this we have to follow some steps like below −Take two pointers called ...

Read More

Find the next identical calendar year in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 284 Views

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ...

Read More

Find the second last node of a linked list in single traversal in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 722 Views

Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) { ...

Read More

Shuffle an Array using STL in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 873 Views

Here we will see the Shuffle and random_shuffle in C++. These functions are used to shuffle array elements in C++. We can use the vector also instead of arrays, the usage is similar. Let us see the random_shuffle() first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example#include using ...

Read More

upper_bound in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 186 Views

Here we will see that is the upper_bound() function in C++ STL. This function returns an iterator that points to the first element in the container, which is considered to go after val. The syntax is like:iterator upper_bound (const value_type& val); const_iterator upper_bound (const value_type& val) const;The return value is an iterator, pointing to the first element in the container which is considered to go after val.Example#include #include using namespace std; int main () {    set myset;    set::iterator itlow, itup;    for (int i = 1; i < 10; i++) myset.insert(i*10);    itup = myset.upper_bound (60); ...

Read More

Construct a complete binary tree from given array in level order fashion in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have an array A[], with n elements. We have to construct the binary tree from the array in level order traversal. So the elements from the left in the array will be filled in the tree level-wise starting from level 0. So if the array is like A = [1, 2, 3, 4, 5, 6], then the tree will be like below:If we see closer, we can find that when the parent is present at index i, then its two children will be at index (2i + 1), and (2i + 2). Thus we can insert left and ...

Read More

Construct an array from GCDs of consecutive elements in given array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 248 Views

Suppose we have an array A[], with n elements. We have to find another array B[], whose size is n+1, such that GCD of B[i] and B[i + 1] is A[i]. If there are multiple solutions, then print one of them whose array sum is minimum. So if A = [1, 2, 3], then output will be [1, 2, 6, 3]When A has only one element say K, then B = [K, K]. So the B[0] will be A[0]. Now consider we are done up to index i, so we have already processed index i, and calculated B[i + 1]. ...

Read More

Construct an array from XOR of all elements of array except element at same index in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 264 Views

Suppose we have an array A[] with n positive elements. We have to create another array B, such that B[i] is XOR of all elements of A[] except A[i]. So if the A = [2, 1, 5, 9], then B = [13, 14, 10, 6]To solve this, at first we have to find the XOR of all elements of A, and store it into variable x, then for each element of A[i], find B[i] = x XOR A[i]Example#include using namespace std; void findXOR(int A[], int n) {    int x = 0;    for (int i = 0; i ...

Read More

Two Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 19K+ Views

Suppose we have an array of integers. We have to return the indices of two integers, such that if we add them up, we will reach to a specific target that is also given. Here we will take one assumption, that is always there will be one unique solution in the array, so no two set of indices for same target will be there.For an example, suppose the array is like A = [2, 8, 12, 15], and the target sum is 20. Then it will return indices 1 and 2, as A[1] + A[2] = 20.To solve this, we ...

Read More

Longest Common Prefix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 7K+ Views

Suppose we have a set of strings in an array. We have to find the Longest Common Prefix amongst the string in the array. Here we will assume that all strings are lower case strings. And if there is no common prefix, then return “”.So if the array of a string is like ["school", "schedule", "Scotland"], then the Longest Common Prefix is “sc” as this is present in all of these string.To solve this, we will take the first string as curr, now take each string from the array and read them character by character, and check the characters between ...

Read More
Showing 1961–1970 of 3,768 articles
« Prev 1 195 196 197 198 199 377 Next »
Advertisements