Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 184 of 377

Find maximum height pyramid from the given array of objects in C++

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

Suppose we have an array of n objects. Each object has width W[i]. We have to arrange them in a pyramidal way like −Total width of ith is less than (i + 1)thTotal number of objects in the ith is less than (i + 1)thFor example, if the weights are like [40, 100, 20, 30], then output will be 2. So top level is 30, then lower level 20, 40 and 100To solve this, we will use the greedy approach. The idea is to use place the objects with lower width at the top, the next object at the level ...

Read More

Find maximum sum possible equal sum of three stacks in C++

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

Suppose we have three stacks of positive numbers. We have to find the possible equal maximum sum of stacks with removal of top elements allowed. The stacks are represented as an array. The first index of the array represents the top element of the stack. Suppose the stack elements are like [3, 10], [4, 5] and [2, 1]. The output will be 0. The sum can only be equal after removing all elements from all stacks.To solve this, we will follow this idea. The idea is to compare the sum of each stack, if they are not equal, then remove ...

Read More

Find number of pairs (x, y) in an array such that x^y > y^x in C++

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

Suppose we have two arrays X and Y of positive integers. Find the number of pairs such that x^y > y^x, where x is an element of X and y is an element of Y. Suppose the X = [2, 1, 6], and Y = [1, 5], then output will be 3. As there are three pairs, these are (2, 1), (2, 5) and (6, 1)We can solve this in an efficient way. The logic is simple, it will be when y > x then x^y > y^x with some exceptions. So this is the trick.Sort the array Yfor each ...

Read More

Find numbers of balancing positions in string in C++

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

Suppose we have a string. We have to find the balancing position count in that string from where left and right part of the string contains same characters. The frequency of characters does not matter. So if the string is “ABAABA”, then the number of balancing positions is 3. These positions are AB|AABA, ABA|ABA, ABAA|BA.To solve this, we will follow some efficient approach. After traversing the string we first feel right[] with counts of all characters. Then traverse the string from left to right. For every character we increment its count in left[] and decrement the count in right. For ...

Read More

How to Read and Print an Integer value in C++

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

Here we will see how to read integer from user and display in C++. To take input we will use the cin operator, and to display we will use cout operator. The syntax will be like −Input −int x; cin >> x;Output −int x = 110; cout x;    cout

Read More

How to sort an array of dates in C/C++?

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

Suppose we have an array of dates. Here we will see how to sort then using C or C++ code. The dates are stored in a class (struct can be used in C also). We will use the sort function of the C++ STL. For comparing dates, we have to write our own compare function that will be used in the sort function. Let us see the example to get better view.Example#include #include #include using namespace std; class Date {    public:       int d, m, y; }; bool compare(const Date &date1, const Date &date2){    if (date1.y ...

Read More

Timer in C++ using system calls

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

Here we will see how to design timer in C++ using a system call. We will not use any graphics or animations. Here timer means the stopwatch, that is up-counting the time. The used system calls are −sleep(n) − This will help the program to sleep for n number of secondssystem() − This is used to execute the system command by passing command as an argument to this function.Example#include #include #include #include using namespace std; int hrs = 0; int mins = 0; int sec = 0; void showClk() {    system("cls");    cout

Read More

Type Conversion in C++

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

Here we will see what are the type conversion techniques present in C++. There are mainly two types of type conversion. The implicit and explicit.Implicit type conversionThis is also known as automatic type conversion. This is done by the compiler without any external trigger from the user. This is done when one expression has more than one datatype is present.All datatypes are upgraded to the datatype of the large variable.bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long doubleIn the implicit conversion, it may lose ...

Read More

Why is a[i] == i[a] in C/C++ arrays?

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

Here we will see one amazing trick in C or C++. The array subscript A[i] can also be written as i[a]. In C/C++ E1[E2] is defined as (*((E1) + (E2))). The compiler performs arithmetic internally to access the array elements. Because of the conversion of rules, that is applied to the binary + operator, if E1 is an array object, and E2 is an integer, then E1[[E2] signifies the E2th element in the E1 array. So A[B] can be defined as *(A + B), so B[A] = *(B + A). so they are basically the same thing.Example#include using namespace ...

Read More

Best Time to Buy and Sell Stock in Python

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

Suppose we have an array A, here A[i] is indicating the price of a given stock on day i. We have to find the maximum profit. We can complete at most one transaction. (Transaction means to buy and sell stocks). But we have to keep in mind that we may not engage in multiple transactions at the same time. So we have to sell the stock before buying the new one.Suppose the array is like A = [7, 1, 5, 3, 6, 4], then the result will be 5. As we can see, if we buy on day 2 (index ...

Read More
Showing 1831–1840 of 3,768 articles
« Prev 1 182 183 184 185 186 377 Next »
Advertisements