Patterns
Patterns are the most basic and intuitive problems for any beginner entering into the programming world.
The goal is not to print just this one pattern, it is to learn the best approach to solve this kind of problems as these questions are
frequently asked in coding exams and in job interviews. Each task in the pattern problems come with input format and output format to
assist you in writing a proper code to get the desired output
Pre-requisites :
For Loop, While Loop
Practice Problems
-
hackerearth.com -
https://www.hackerearth.com/problem/algorithm/pattern/
hackerrank.com -
https://www.hackerrank.com/challenges/printing-pattern-2/problem
codeforwin.com -
https://codeforwin.org/2015/07/right-triangle-star-pattern-program-in-c.html
codeforwin.com -
https://codeforwin.org/2015/07/equilateral-triangle-star-pattern-program-in-c.html
codeforwin.com -
https://codeforwin.org/2015/07/c-program-to-print-diamond-star-pattern.html
codeforwin.com -
https://codeforwin.org/2015/07/c-program-to-print-hollow-diamond-star-pattern.html
codeforwin.com -
https://codeforwin.org/2015/07/c-program-to-print-heart-star-pattern.html
spoj.com -
https://www.spoj.com/problems/ABACABA/
Basic Mathematical Operations
One of the most basics you should know to start programming. Almost in every problems you have to use Mathematical
operations to solve that problem. The operations are Addition, Multiplication, Subtraction, Division, Modulo etc. Solving these problems will give
you confidence as well as command over the language.
Practice Problems
Design a simple calculator that performs Addition,Subtraction,Multiplication and Division.
Write a program that takes a very large number as an input and print "Yes" if it is divisible by 3 else print "NO".
Write a progran to reverse the given number.
codechef.com -
https://www.codechef.com/problems/BAO
codechef.com -
https://www.codechef.com/AUG18B/problems/SHKNUM
codechef.com -
https://www.codechef.com/BUGE2017/problems/BUG2K17C
codechef.com -
https://www.codechef.com/problems/PROXYC
spoj.com -
https://www.spoj.com/problems/ADDREV/
LCM and GCD
The greatest common divisor (GCD) of two numbers a and b is the greatest number that divides evenly into both a and b. Natively we could start
from the smallest of the two numbers and work our way downwards until we find a number that divides into both of them:
for (int i=Math.min(a,b); i>=1; i--)
if (a%i==0 && b%i==0)
return i;
Although this method is fast enough for most applications, there is a faster method called Euclid’s algorithm.
Euclid’s algorithm iterates over the two numbers until a remainder of 0 is found.
Using this algorithm we can find the lowest common multiple (LCM) of two numbers. For example the LCM of 6 and 9 is 18 since 18 is the smallest number that divides both 6 and 9. Here is the code for the LCM method:
int LCM(int a, int b)
{
return b*a/GCD(a,b);
}
Practice Problems
Given an array which contains N integers. Create another array which contains Cummulative GCD of previous array.
codechef.com -
https://www.codechef.com/problems/SHADOW
codechef.com -
https://www.codechef.com/problems/GIVCANDY
codeforces.com -
https://codeforces.com/problemset/problem/664/A
codechef.com -
https://www.codechef.com/JUNE19B/problems/SUMAGCD
Sort Techniques
Bubble Sort
One of the first sorting algorithms that is taught to students is bubble sort. While it is not fast enough in practice for all but the smallest data sets, it does serve the purpose of showing how a sorting algorithm works.
This is O(n²) runtime, and hence is very slow for large data sets. The single best advantage of a bubble sort, however, is that it is very simple to understand and code from memory. Additionally, it is a stable sort that requires no additional memory, since all swaps are made in place. Bubble Sort
Insertion Sort
Insertion sort is an algorithm that seeks to sort a list one element at a time. With each iteration, it takes the next element waiting to be sorted, and adds it, in proper location, to those elements that have already been sorted.
One of the principal advantages of the insertion sort is that it works very efficiently for lists that are nearly sorted initially. Furthermore, it can also work on data sets that are constantly being added to.
Insertion Sort
Merge Sort
A merge sort works recursively. First it divides a data set in half, and sorts each half separately. Next, the first elements from each of the two lists are compared. The lesser element is then removed from its list and added to the final result list.
Each recursive call has O(n) runtime, and a total of O(log n) recursions are required, thus the runtime of this algorithm is O(n * log n).
Merge Sort
Practice Problems
Take array as an input and try to sort by all Techniques
Prime Numbers
A number is prime if it is only divisible by 1 and itself. So for example 2, 3, 5, 79, 311 and 1931 are all prime, while 21 is not prime because it is divisible by 3 and 7. To find if a number n is prime we could simply check if it divides any numbers below it. We can use the modulus (%) operator to check for divisibility:
for (int i=2;i<n;i++)
if (n%i==0) return false;
return true;
Now suppose we wanted to find all the primes from 1 to 100000, then we would have to call the above method 100000 times. This would be very inefficient since we would be repeating the same calculations over and over again. In this situation it is best to use a method known as the
Sieve of Eratosthenes. The Sieve of Eratosthenes will generate all the primes from 2 to a given number n. It begins by assuming that all numbers are prime. It then takes the first prime number and removes all of its multiples. It then applies the same method to the next prime number. This is continued until all numbers have been processed.
Practice Problems
codechef.com -
https://www.codechef.com/problems/PRMNUM
codechef.com -
https://www.codechef.com/problems/RECL09
codeforces.com -
https://codeforces.com/problemset/problem/230/B
codechef.com -
https://www.codechef.com/problems/CNTPRIME
codechef.com -
https://www.codechef.com/CEEL2019/problems/TWINPR
codechef.com -
https://www.codechef.com/JULY19A/problems/GUESSPRM/
Binary Search
In its simplest form, Binary Search is used to quickly find a value in a sorted sequence (consider a sequence an ordinary array for now). We’ll call the sought value the target value for clarity. Binary search maintains a contiguous subsequence of the starting sequence where the target value is surely located. This is called the search space. The search space is initially the entire sequence. At each step, the algorithm compares the median value in the search space to the target value. Based on the comparison and because the sequence is sorted, it can then eliminate half of the search space. By doing this repeatedly, it will eventually be left with a search space consisting of a single element, the target value.
If the target value was not present in the sequence, binary search would empty the search space entirely. This condition is easy to check and handle. Since each comparison binary search uses halves the search space, we can assert and easily prove that binary search will never use more than O(log N) comparisons to find the target value.
Practice Problems
codechef.com -
https://www.codechef.com/COOK108B/problems/TWOVRIBL
codechef.com -
https://www.codechef.com/problems/CHEFRES
codeforces.com -
https://codeforces.com/problemset/problem/1138/A
codeforces.com -
https://codeforces.com/problemset/problem/1010/A
codechef.com -
https://www.codechef.com/problems/CHESAK
Stacks
Stacks are the data structure that are best described as, "last in, first out". The classic example is the pile of plates at the local buffet. The workers can continue to add clean
plates to the stack indefinitely, but every time, a visitor will remove from the stack the top plate, which is the last one that was added.
push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.
Resources
geeksforgeeks.org -
Stacks
Practice Problems
hackerrank.com -
https://www.hackerrank.com/challenges/maximum-element/problem
hackerrank.com -
https://www.hackerrank.com/challenges/balanced-brackets/problem
hackerrank.com -
https://www.hackerrank.com/challenges/equal-stacks/problem
codechef.com -
https://www.codechef.com/problems/CL16BB
codechef.com -
https://www.codechef.com/problems/CB07
Queues
A queue is a data structure that is best described as "first in, first out". A real world example of a queue is people waiting in line at the bank. As each person enters the bank, he or she is "enqueued" at the back of the line. When a teller becomes available, they are "dequeued" at the front of the line.
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added. In a queue, we remove the item the least recently added.
Resources
geeksforgeeks.org -
Queues
Practice Problems
hackerrank.com -
https://www.hackerrank.com/challenges/queue-using-two-stacks/problem
hackerrank.com -
https://www.hackerrank.com/challenges/castle-on-the-grid/problem
codeforces.com -
https://codeforces.com/problemset/problem/490/B
codechef.com -
https://www.codechef.com/COOK105A/problems/PEWDSVTS
codechef.com -
https://www.codechef.com/problems/DRCHE