Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 377 of 377

A Puzzle on C/C++ R-Value Expressions?

Arnab Chakraborty
Arnab Chakraborty
Updated on 31-Jul-2019 217 Views

Here we will see one puzzle. Suppose there is a program which is given as below, we have to tell what will be the output and why?Example#include using namespace std; int main() {    int x = 0xab;    ~x;    cout

Read More

A Pancake Sorting Problem?

Arnab Chakraborty
Arnab Chakraborty
Updated on 31-Jul-2019 261 Views

Here we will see another sorting problem named Pancake sort. This problem is simple. We have one array. We have to sort this. But we can use only one operation called rev(arr, i). This will reverse the elements of arr from 0 to ith position.This idea is like the selection sort. We repeatedly place the max element at end reduce the size of the array. Let us see the algorithm to get the idea.AlgorithmpancakeSort(arr, n)Begin    size := n    while size > 1, do       index := index of max element in arr from [0 to size ...

Read More

A nested loop puzzle?

Arnab Chakraborty
Arnab Chakraborty
Updated on 31-Jul-2019 486 Views

In this section we will see one interesting problem. We will see two code segments. Both are with two nested loops. We have to identify which one will run faster. (We will assume that the compiler is not optimizing the code).Segment 1for(int i = 0; i < 10; i++){    for(int j = 0; j

Read More

A matrix probability question ?

Arnab Chakraborty
Arnab Chakraborty
Updated on 31-Jul-2019 244 Views

Here we will see one matrix probability problem. We have one rectangular matrix. We can move four directions from the current cell with equal probability. These four directions are left, right, up and down. We have to calculate the probability after N moves from position M[i, j].Here we will do something related to DFS. We will traverse recursively traverse in each of the four possible rooms from the current room. Then we will calculate the probability with one less move. As each of the four directions has equal probability, then each direction will contribute 0.25 of total probability. If we ...

Read More

A Boolean Matrix Question in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 322 Views

Here we will see one interesting Boolean matrix problem. One Boolean matrix is given which contains 0’s and 1’s. Our goal is to find where 1 is marked. If the 1 is marked at position mat[i, j], then we will make all entries to 1 of the row i and column j. Let us see an example. If the matrix is like below −1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0Then after modification, it will be −1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1AlgorithmmatrixUpdate(matrix[R, ...

Read More

C++ Program for Sum of squares of first n natural numbers?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 673 Views

In this problem we will see how we can get the sum of squares of first n natural numbers. Here we are using one for loop, that runs from 1 to n. In each step we are calculating square of the term and then add it to the sum. This program takes O(n) time to complete. But if we want to solve this in O(1) or constant time, we can use this series formula −AlgorithmsquareNNatural(n)begin    sum := 0    for i in range 1 to n, do       sum := sum + i^2    done    return ...

Read More

C++ tricks for competitive programming (for C++ 11)?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 369 Views

Here we will see some good tricks of C++ programming language that can help us in different area. Like if we want to participate in some competitive programming events, then these tricks will help us to reduce the time for writing codes. Let us see some of these examples one by one.Checking whether a number is odd or even without using % operator. This trick is simple. We can perform bitwise AND operation with the number and 1. If the result is non-zero then this is odd, otherwise this is even. The logic is too simple. All odd numbers have ...

Read More

C++ Program for Comb Sort?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 414 Views

The basic idea of comb sort and the bubble sort is same. In other words, comb sort is an improvement on the bubble sort. In the bubble sorting technique, the items are compared with the next item in each phase. But for the comb sort, the items are sorted in a specific gap. After completing each phase, the gap is decreased. The decreasing factor or the shrink factor for this sort is 1.3. It means that after completing each phase the gap is divided by 1.3. Time Complexity is O(n log n) for best case. O(n2/2nP) (p is number of ...

Read More
Showing 3761–3768 of 3,768 articles
« Prev 1 373 374 375 376 377 Next »
Advertisements