Found 1339 Articles for C

Active and Inactive cells after k Days?

Arnab Chakraborty
Updated on 31-Jul-2019 12:16:53

342 Views

Here we will see one interesting problem. Suppose one binary array is given of size n. Here n > 3. A true value or 1 value indicates that the active state, and 0 or false indicates inactive. Another number k is also given. We have to find active or inactive cells after k days. After every day state of ith cell will be active if the left and right cells are not same, if they are same, then it will be inactive. The left most and right most cell has no cell before and after it. So left most and ... Read More

Rat in a Maze with multiple steps or jump allowed?

Arnab Chakraborty
Updated on 31-Jul-2019 12:13:48

463 Views

The rat in maze problem is one of the well-known problem of the backtracking. Here we will see that problem with little variation. Suppose one NxN maze M is given. The starting point is top left corner M[0, 0], and the destination is right bottom corner M[N – 1, N - 1]. One rat is placed at the starting point. Our goal is to find a path from starting point to ending point that can be by the rat to reach the destination. Here the rat can jump (The variation). Now there are some constraintsThe rat can move either towards ... Read More

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

Arnab Chakraborty
Updated on 31-Jul-2019 12:08:17

174 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

A Pancake Sorting Problem?

Arnab Chakraborty
Updated on 31-Jul-2019 12:06:03

216 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
Updated on 31-Jul-2019 12:03:12

445 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

A matrix probability question ?

Arnab Chakraborty
Updated on 31-Jul-2019 12:01:37

200 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

Print the given pattern recursively

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here, as per the given problem pattern needs to be displayed using recursive approach.Recursive function is the one that calls itself n number of times. There can be ‘n’ number of recursive function in a program. The problem working with recursive function is their complexity.AlgorithmSTART Step 1 -> function int printpattern(int n)    If n>0       Printpattern(n-1)       Print *    End IF End Step 2 -> function int pattern(int n)    If n>0       pattern(n-1)    End IF    Printpattern(n)    Print End STOPExample#include int printpattern(int n) {    if(n>0) { ... Read More

Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

112 Views

Given with equation program must find the value of ‘a’ where a+b Declare start variables b=10, x=9, n=40 and flag=0, divisible Step 2 -> Loop For divisible = (b / x + 1 ) * x and divisible = 1       Print divisible-1       Set flag=1    End END STOPExample#include int main(int argc, char const *argv[]) {    int b=10, x=9, n=40, flag = 0;    int divisible;    for (divisible = (b / x + 1 ) * x ; divisible = 1) {          printf("%d ", divisible - b );          flag = 1;       }    }    return 0; }Outputif we run above program then it will generate following output8 17 26

Print n smallest elements from given array in their original order

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

252 Views

Given with array of let’s say k elements the program must find the n smallest elements amongst them in their appearing order.Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Ouput : 1, 2, 3 Input k is 3 it means 3 shortest elements among the set needs to be displayed in original order like 1 than 2 and than 3AlgorithmSTART Step 1 -> start variables as int i, max, pos, j, k=4 and size for array size Step 2 -> Loop For i=k and i=0 and j--       If arr[j]>max         ... Read More

Print n terms of Newman-Conway Sequence

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

396 Views

Newman-Conway Sequence is used for generating following integer sequence.1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12Formula used for generating Newman-Conway sequence for n numbers is −P(n) = P(P(n - 1)) + P(n - P(n - 1)) Where, p(1) =p(2) =1AlgorithmSTART Step 1 -> Input variable n(e.g. 20) Step 2 -> start variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 -> Loop For i=3 and i End Loop For STOPExample#include int main() {    int n = 20,i;    int p[n + 1];    p[1] = 1;    p[2] = 1;    printf("Newman-Conway Sequence is :");    printf("%d %d ",p[1],p[2]);    for (i = 3; i

Advertisements