C Articles

Page 72 of 96

Print the corner elements and their sum in a 2-D matrix in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Aug-2019 3K+ Views

Given an array of size 2X2 and the challenge is to print the sum of all the corner elements stored in an array.Assume a matrix mat[r][c], with some row “r” and column “c” starting row and column from 0, then its corner elements will be; mat[0][0], mat[0][c-1], mat[r-1][0], mat[r-1][c-1]. Now the task is to get these corner elements and sum those corner elements i.e., mat[0][0] + mat[0][c-1] + mat[r-1][0] + mat[r-1][c-1], and print the result on the screen.ExampleInput: Enter the matrix elements :    10 2 10    2 3 4    10 4 10 Output: sum of matrix is ...

Read More

Print the balanced bracket expression using given brackets in C Program

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Aug-2019 416 Views

Given four variables a, b, c, d with predefined values that will print the given bracket depending upon the variable used.Where variable, a for (( b for () c for )( d for ))The task is to use all the given brackets and print the balanced bracket expression, if we cannot form a balanced bracket expression then print -1. In case of multiple answers we can print any of the multiple answers which can be formed using the given brackets.ExampleInput: a = 3, b = 2, c = 4, d = 3 Output : (((((()()()()())))))()()To achieve this result we can, ...

Read More

Print the last occurrence of elements in array in relative order in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Aug-2019 376 Views

Given an array a[] with elements and the task is to print the last occurrences of the given elements in the list. Here we not only have to remove the duplicate elements but also we have to maintain the order of the occurrences of the elements in an array as per the last time they have occurred.Like we have an array of 6 elements also containing some duplicate values i.e., {1, 3, 2, 3, 1, 2} so the result should be in form of 3 1 2.ExampleInput: a[]={4, 2, 2, 4, 1, 5, 1} Output : 2 4 5 1AlgorithmSTART ...

Read More

Print reverse of a Linked List without extra space and modification in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Aug-2019 530 Views

The task is to print the nodes starting from the end of the linked list without using extra space which means there shouldn’t be any extra variable instead the head pointer pointing the first node will be moved.ExampleInput: 10 21 33 42 89 Output: 89 42 33 21 10There can be many solutions to print the linked list in reverse order, like recursive approach (use extra space), reverse the linked list(requires modification in the given Linked List), pushing the elements on a stack and then pop and display the elements one by one(requires space O(n)), but these solutions seem to ...

Read More

Baum Sweet Sequence in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 245 Views

Here we will see the Baum Sweet Sequence. This sequence is one binary sequence. If a number n has an odd number of contiguous 0s, then nth bit will be 0, otherwise nth bit will be 1.We have a natural number n. Our task is to find the n-th term of Baum Sweet sequence. So we have to check whether it has any consecutive block of zeros of odd length.If the number is 4 then the term will be 1, because 4 is 100. So it has two (even) number of zeros.AlgorithmBaumSweetSeqTerm (G, s) −begin    define bit sequence seq ...

Read More

Binary Search using pthread in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 621 Views

We know that the binary search approach is one of the most suitable and effective sorting algorithm. This works on sorted sequence. The algorithm is simple, it simply finds the element from middle, then divide the list by two parts, and moves either towards the left sublist, or right sublist.We know the algorithm of it. Now we will see how to use binary search technique in multithreading environment. The number of threads depends on number of cores are present in the system. Let us see the code to get the idea.Example#include #define MAX 16 #define MAX_THREAD 4 using namespace ...

Read More

Binary representation of next greater number with same number of 1’s and 0’s in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 336 Views

Suppose we have one binary number, that is representation of a number n. We have to find binary representation of a number which is smallest but larger than n, and it also has same number of 0s and 1s. So if the number is 1011 (11 in decimal), then the output will be 1101 (13). This problem can be found using the next-permutation calculation. Let us see the algorithm to get the idea.AlgorithmnextBin(bin) −Begin    len := length of the bin    for i in range len-2, down to 1, do       if bin[i] is 0 and bin[i+1] ...

Read More

BFS using vectors & queue as per the algorithm of CLRS in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 416 Views

In CLRS book the BFS algorithm is described using vectors and queue. We have to implement that algorithm using C++ STL. Let us see the algorithm at first.AlgorithmBFS(G, s) −begin    for each vertex u in G.V - {s}, do       u.color := white       u.d := infinity       u.p := NIL    done    s.color := green    s.d := 0    s.p := NIL    Q := NULL    insert s into Q    while Q is not null, do       u = delete from Q       for ...

Read More

Betrothed numbers in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 630 Views

Here we will see the Betrothed number. This is a pair of numbers, such that the sum of the proper divisors of one number is one more than the other number. We have to find these pairsFor an example, the pair is like (48, 75). So the divisors of 48 is {1, 2, 3, 4, 6, 8, 12, 16, 24} and sum is 76. Similarly, the divisors of 75 is {1, 3, 5, 15, 25} so sum is 49.AlgorithmBetrothedPairs (n) −begin    for num in range 1 to n, do       sum := 1       for ...

Read More

Auxiliary Space with Recursive Functions in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 447 Views

Here we will see how the auxiliary space is required for recursive function call. And how it is differing from the normal function call?Suppose we have one function like below −long fact(int n){    if(n == 0 || n == 1)       return 1;    return n * fact(n-1); }This function is recursive function. When we call it like fact(5), then it will store addresses inside the stack like below −fact(5) ---> fact(4) ---> fact(3) ---> fact(2) ---> fact(1)As the recursive functions are calling itself again and again, addresses are added into stack. So if the function is ...

Read More
Showing 711–720 of 953 articles
« Prev 1 70 71 72 73 74 96 Next »
Advertisements