Category Archives: C Programming

Find if a no is Power of Two

Problem: Write a C program to find if a number is of power of 2? Solution: Method 1: (Using arithmetic) Keep dividing the number by two, i.e, do n = n/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes Read More →

Find row with maximum number of 1s in sorted matrix

Problem: You are given a MxN matrix with each row sorted. Matrix is having only 0′s and 1′s. You have to find row wotth maximum number of 1′s. eg. matrix given: 000111 001111 011111 000011 111111 // row with max number of 1′s Method 1: Brute force approach Traverse the matrix row wise and count Read More →

Pythagorean triplets in an array

Given an array of integers . Write an algorithm to find all the Pythagorean triples. Eg : i/p : int arr[ ] = {1,3,4,5,6,7,8,10,11} o/p: Print 3,4,5 and 6,8,10 Solution: Steps: (1) First square each element. (2) Sort the array in ascending order. This takes O(n log n). (3) Now consider each element a[ i Read More →

Search in a row wise and column wise sorted matrix

You are given an 2D array of MxN which is row and column wise sorted. What is the efficient way to search for an element? Steps: 1) Start from top right element 2) for loop compare this element e with x a) if they are equal then return its position b) e < x then Read More →

Generate integer from 1-8 with equal probability

Problem: Write a method to generate a random number between 1 and 8, given a method that generates a random number between 1 and 3. The distribution between each of the numbers must be uniform. Solution: Let’s think of this like a decision tree. Each rand3() will be a decision. If we somehow generate integers Read More →

Print a Square Matrix in Spiral Form

spiral

Given a N*N square matrix. Write a code to print it in spiral order. Time Complexity : O(N^2)

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 →

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 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). 

Hello World !

This is our first blog post and we couldn’t think of a better way to say hello. Hello World is often the first program written by many when learning a new programming language. So here we go to start a portal for all the people who are really crazy for coding.