C / C++

How many possible ways the child can run up the ladder

A child is running up a ladder with n steps, and can hop either 1 step or 2 steps at a time. Calculate how many possible ways the child can run up the ladder. This problem is similar to Fibonacci Problem. Each step n can be reached from either two steps below (n-2) or one Read More →

Remove duplicate characters in a string

Given a string, Write a program to remove duplcate characters from the string. Input String : crazyforcode Output String : crazyfode Algorithm : 1. For each character, check if it is duplicate of already found characters. 2. Skip duplicate characters and update the non duplicate characters. Method 1 Using Extra Space. Time Complexity : O(N) Read More →

Print letters followed by their frequency (Run Length Encoding)

Given a string ,Write a program to print letter followed by it’s frequency.This is also knows as Run Length Encoding. Example: Input aaabcc Output a3b1c2 Algorithm: 1.Pick the first character from the string and print it. 2.Count the number of subsequent occurrences of the picked character and then print the count. 3.Pick the next character Read More →

Write a program to detect loop in a Linked List


Floyd’s Algorithm: This problem can be solved by using two pointers, slow pointer and fast pointer.Both these pointers will point to head of the linked list initially. Increment slow pointer by 1 node and the fast pointer by 2 nodes. If there’s a loop, the fast pointer will meet the slow pointer somewhere.We’ll discuss in Read More →

Find the maximum of three integers using the ternary operator

Here is how you do it max = ((a>b)?((a>c)?a:c):((b>c)?b:c)); Here is another way max = ((a>b)?a:b)>c?((a>b)?a:b):c; Here is some code to find the max of 4 numbers… Method 1 Method 2

Find the angle between hour handle and minute handle

Given the time in 24 hour format.Find the angle between hour handle and minute handle in a clock at any given time. Solution : Visualize the clock as a 360 degree circle.As we know that Hour handle completes 360 degree in 12 hours.In 1 hour or 60 minutes it complete 360/12 i.e. 30 degree.Minute handle Read More →

Write code to remove duplicates in a sorted array

In this simple C program, we change the original array and also send the new size of the array back to the caller. Method 1 And here is the output… OLD : [1] [1] [2] [3] [5] [6] [6] [7] [10] [25] [100] [123] [123] NEW : [1] [2] [3] [5] [6] [7] [10] [25] [100] [123] Method 2 If we dont want to change the input array and just want to print the array without any duplicates, the solution is very Read More →

Write a C program for calculating the factorial of a number

Here is a recursive C program Please note that there is no error handling added to this function (to check if n is negative or 0. Or if n is too large for the system to handle).