Category Archives: Misc

View Miscellaneous problems

Write a program to swap 2 numbers.

Question: Write a program to swap 2 numbers? Input: a = 4, b = 19 Output: a = 19, b = 4 Swapping 2 numbers is one of the most basic tasks while programming. But sometimes, we tend to make error in it. For instance look at this code. The output of the above program is:- What happened?… Read More »

Algorithm Analysis

We have been discussing the time complexities of each of the algorithm that we have discussed. Let us try to analyze, how these time complexities are calculated. O(1) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. For example swap()… Read More »

Find the median of a sequence of n integers.

Question: Given a sequence of integers, find the median of the integers? Input: arr [] = {15, 23, 4, 16, 8, 42}. Find median. Output: 15 Since the question does not specify anything we cannot assume that the given array is a sorted one. The definition of median is the (n/2)th element of a sorted sequence of integers.… Read More »

Some general tips for programming.

A Programming Contest is a delightful playground for the exploration of intelligence of programmers. To start solving problems in contests, first of all, you have to fix your aim. Some contestants want to increase the number of problems solved by them and the other contestants want to solve less problems but with more efficiency. Choose any of the… Read More »

What is a wild pointer?

Question: What is a wild pointer in a program? Uninitialized pointers are known as wild pointers. These are called so because they point to some arbitrary memory location and may cause a program to crash or behave badly. This can be understood from the below examples. Please note that if a pointer p points to a known variable… Read More »

What is a memory leak?

Question:What is a memory leak in a program? We have been writing many programs where we have used the “free()” keyword. One such example can be found in the “Delete the complete list” function found in the post – Important traversals in a Linked List. The code snippet is:- This is done due to a reason. When we… Read More »

Unordered Linear Search

Let us assume that given an array whose elements order is not known. That means the elements of the array are not sorted. In this case if we want to search for an element then we have to scan the complete array and see if the element is there in the given list or not. This requires to… Read More »

Write a C program to reverse digits of a number?

ITERATIVE VERSION The iterative version can be done in this way:- Input: num Initialize rev_num = 0 Loop while num > 0 Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; Divide num by 10 Return rev_num Here is an implementation of the same. RECURSIVE WAY

Write a program to calculate pow(x,n)

We will discuss how to calculate the result when a number ‘x’ is raised to the power ‘y’. Originally C provides a standard function that allows us to directly use the power function. It can be used in the following manner. Now suppose that we need to write a custom function by ourselves. Here are the sample methods… Read More »

Convert a decimal number to any base.

Here is an implementation of a simple algorithm that can be used to convert any number in decimal format to a number to any other base ‘b’. The base ‘b’ can be octal, binary or even any other arbitrary base like 7.