Close

## Maximum numbers in a sliding window

Given an array of N values and a value K, Write a program to print the maximum of all the sliding windows (sub-array) of size K. For example let us consider the following array as input  [4, 5, 3, 2, 6, 1, 2, 3, 8, 4] Let as take the sliding window of size 3.…

## Inserting an element into sorted linked list

Given a sorted linked list write a program to insert data into it’s correct position.  For example consider the following linked list 1 -> 3 -> 4 -> 5  Inserting 2 should change the list to 1 -> 2 -> 3 -> 4 -> 5 Inserting 0 should change it to 0 -> 1 ->…

## Finding K-size sub array with minimum sum

Given an array of size N and a number K. We have to find the sub-array of size K such that the sum of it’s elements is minimum among all K-size sub arrays. For example Let us consider the following array of size 10.  {1, 2, 1, 3, 1, 1, 1, 4, 2, 3} The…

## Finding two numbers occuring once

Given an array containing all elements twice except two numbers. Write a program to find those two numbers. For example let us consider the following array {2, 3, 2, 1, 4, 1, 4, 5} The answer is {3,5} because they appear only once and all remaining elements appear twice. This question is somewhat similar to earlier…

## Number of bits to change from one number to other

Given two numbers, calculate the number of bits required to change from one number to the other. For example 12 in binary can be represented as 1100. 15 is represented as 1111. We need to change two bits to convert one number from the other as the last two bits are different. We can find…

## Pair swapping in a linked list

Given a singly linked list, we have to swap the adjacent nodes in it. For example some input/output samples are given below Input                              output ————————————————- 1->2->3->4->X                      2->1->4->3->X 1->2->3->4->5->X      …

## Printing the prime factorization of a given number

How do we write a program to find the prime factorization of a given number? For example prime factorization for 18 can written as 2 * 3 * 3 or 2 * 3 ^ 2. For any number there is only one possible prime factorization.  An efficient algorithm can be given as follows. we run…

## Finding all the factors of a given number

Given a number, how do we write a program to find all of it’s factors or divisors. For example, for the number 18, the factors are {1, 2, 3, 6, 9, 18}. An immediate algorithm that comes to our mind is to run a loop from 1 to n. In each iteration check if the…

## constant pointers and pointer to constant

In this post we talk a little bit about the constant pointers and pointer to a constant and the difference between them. In C programming, pointer is a crucial concept and it is always good to have a clear understanding of the fundamentals. Constant pointers: Constant pointer as the name indicates is a pointer whose…

## Reverse a single linked list

Given a singly linked list, how do we reverse it by re-arranging the links? For example if the given linked list is  1 -> 2 -> 3 -> NULL. The output of reverse method should be 3 -> 2 -> 1 -> NULL. The following diagram shows the simple steps to perform the reversal. And…