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



  1. hackerearth.com - https://www.hackerearth.com/problem/algorithm/pattern/

  2. hackerrank.com - https://www.hackerrank.com/challenges/printing-pattern-2/problem

  3. codeforwin.com - https://codeforwin.org/2015/07/right-triangle-star-pattern-program-in-c.html

  4. codeforwin.com - https://codeforwin.org/2015/07/equilateral-triangle-star-pattern-program-in-c.html

  5. codeforwin.com - https://codeforwin.org/2015/07/c-program-to-print-diamond-star-pattern.html

  6. codeforwin.com - https://codeforwin.org/2015/07/c-program-to-print-hollow-diamond-star-pattern.html

  7. codeforwin.com - https://codeforwin.org/2015/07/c-program-to-print-heart-star-pattern.html

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



  1. Design a simple calculator that performs Addition,Subtraction,Multiplication and Division.

  2. Write a program that takes a very large number as an input and print "Yes" if it is divisible by 3 else print "NO".

  3. Write a progran to reverse the given number.

  4. codechef.com - https://www.codechef.com/problems/BAO

  5. codechef.com - https://www.codechef.com/AUG18B/problems/SHKNUM

  6. codechef.com - https://www.codechef.com/BUGE2017/problems/BUG2K17C

  7. codechef.com - https://www.codechef.com/problems/PROXYC

  8. spoj.com - https://www.spoj.com/problems/ADDREV/

Array


  1. Resources


    1. geeksforgeeks.org - Array Data Structure

    2. codechef.com - Array

  2. Practice Problems


    1. hackerrank.com - https://www.hackerrank.com/challenges/arrays-ds/problem

    2. codechef.com - https://www.codechef.com/problems/LECANDY

    3. spoj.com - https://www.spoj.com/problems/PWRARR/

    4. codechef.com - https://www.codechef.com/problems/CNOTE

    5. codechef.com - https://www.codechef.com/problems/SALARY

    6. codechef.com - https://www.codechef.com/problems/RAINBOWA

    7. codechef.com - https://www.codechef.com/problems/TRACE

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



  1. Given an array which contains N integers. Create another array which contains Cummulative GCD of previous array.

  2. codechef.com - https://www.codechef.com/problems/SHADOW

  3. codechef.com - https://www.codechef.com/problems/GIVCANDY

  4. codeforces.com - https://codeforces.com/problemset/problem/664/A

  5. codechef.com - https://www.codechef.com/JUNE19B/problems/SUMAGCD

Strings


  1. Resources


    1. tutorialpoints.com - C++ Strings

    2. guru99.com - Java Strings

    3. docs.python.org - Python Strings

    4. tutorialpoints.com - Python Strings

    5. geeksforgeeks.org - Questions

  2. Practice Problems


    1. codechef.com - https://www.codechef.com/problems/CSUB

    2. codechef.com - https://www.codechef.com/problems/CHEFSTLT

    3. codechef.com - https://www.codechef.com/problems/STRPALIN

    4. codechef.com - https://www.codechef.com/problems/SNAKPROC

    5. codechef.com - https://www.codechef.com/AUG19A/problems/CHEFDIL

Sort Techniques


  1. Bubble Sort


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

  3. Insertion Sort


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

  5. Merge Sort


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

  7. Practice Problems


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



  1. codechef.com - https://www.codechef.com/problems/PRMNUM

  2. codechef.com - https://www.codechef.com/problems/RECL09

  3. codeforces.com - https://codeforces.com/problemset/problem/230/B

  4. codechef.com - https://www.codechef.com/problems/CNTPRIME

  5. codechef.com - https://www.codechef.com/CEEL2019/problems/TWINPR

  6. codechef.com - https://www.codechef.com/JULY19A/problems/GUESSPRM/

Pointers


  1. Resources


    1. geeksforgeeks.org - Pointers

    2. github.com - Eric Lewis Blog

    3. tutorialspoint.com - C++ Pointers

  2. Practice Problems


    1. Swap two numbers using Pointers.

    2. Sort an Array using Pointers.

    3. Resverse an Array using Pointers.

    4. Sum of Array using Pointers.

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.

  1. Resources


    1. geeksforgeeks.org - Stacks

  2. Practice Problems


    1. hackerrank.com - https://www.hackerrank.com/challenges/maximum-element/problem

    2. hackerrank.com - https://www.hackerrank.com/challenges/balanced-brackets/problem

    3. hackerrank.com - https://www.hackerrank.com/challenges/equal-stacks/problem

    4. codechef.com - https://www.codechef.com/problems/CL16BB

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

  1. Resources


    1. geeksforgeeks.org - Queues

  2. Practice Problems


    1. hackerrank.com - https://www.hackerrank.com/challenges/queue-using-two-stacks/problem

    2. hackerrank.com - https://www.hackerrank.com/challenges/castle-on-the-grid/problem

    3. codeforces.com - https://codeforces.com/problemset/problem/490/B

    4. codechef.com - https://www.codechef.com/COOK105A/problems/PEWDSVTS

    5. codechef.com - https://www.codechef.com/problems/DRCHE