Top 500 Codes

PrepInsta Top 500 Codes

Below You will find some of the most important codes in languages like C, C++ and Java. These codes are of prime importance for college semester exams and also for various online tests and interviews of the companies offering placements within varying range in LPA.

These codes are very important since these will help you clear your basic concepts in various languages.

Getting Started

 

Working with Numbers

Codes for Recursion

  • Power of a Number – C | C++ | Java | Python
  • Prime Number – C | C++ | Java | Python
  • Largest element in an array – C | C++ | Java | Python
  • Smallest element in an array – C | C++ | Java | Python
  • Reversing a Number – C | C++ | Java | Python
  • HCF of two numbers – C | C++ | Java | Python
  • LCM of two numbers – C | C++ | Java | Python
  • Program to calculate length of the string using recursion- C | C++ | Java | Python
  • Print All Permutations of a String- C | C++ | Java | Python
  • Given an integer N the task is to print the F(N)th term.- C | C++ | Java | Python
  • Given a list arr of N integers, print sums of all subsets in it- C | C++ | Java | Python
  • Last non-zero digit in factorial- C | C++ | Java | Python
  • Given a positive integer N, return the Nth row of pascal’s triangle – C | C++ | Java | Python
  • Given an integer N representing the number of pairs of parentheses, the task is to generate all combinations of well-formed(balanced) parentheses – C | C++ | Java | Python
  • Find the Factorial of a number using recursion – C | C++ | Java | Python
  • Find all possible Palindromic partitions of the given String – C | C++ | Java | Python
  • Find all the N bit binary numbers having more than or equal 1’s than 0’s – C | C++ | Java | Python
  • Given a set of positive integers, find all its subsets – C | C++ | Java | Python
  • Given a string s, remove all its adjacent duplicate characters recursively – C | C++ | Java | Python
array

Important Codes related to Arrays

Operations on Strings

  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Replacing a particular word with another word in a string – C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python
  • Print all permutations of a given string in lexicographically sorted order : C | C++ | Java | Python
Operations on Strings

Advanced Codes related to Arrays

  • Given an array which consists of only 0, 1 and 2. Sort the array without using any  algorithm – C | C++ | Java | Python
  • Find the “Kth” max and min element of an array – C | C++ | Java | Python
  • Move all the negative elements to one side of the array – C | C++ | Java | Python
  • Find the Union and Intersection of the two sorted arrays. – C | C++ | Java | Python
  • Find Largest sum contiguous Subarray – C | C++ | Java | Python
  • Minimize the maximum difference between heights – C | C++ | Java | Python
  • Minimum no. of Jumps to reach the end of an array – C | C++ | Java | Python
  • Find duplicate in an array of N+1 Integers – C | C++ | Java | Python
  • Merge 2 sorted arrays without using extra space. – C | C++ | Java | Python
  • Kadane’s Algorithm – C | C++ | Java | Python
  • Merge Intervals – C | C++ | Java | Python
  • Count Inversion – C | C++ | Java | Python
  • Best time to buy and Sell stock – C | C++ | Java | Python
  • Find all pairs on integer array whose sum is equal to given number – C | C++ | Java | Python
  • Find if there is any subarray with sum equal to 0 – C | C++ | Java | Python
  • Find factorial of a Large Number – C | C++ | Java | Python
  • Find common elements In 3 sorted arrays – C | C++ | Java | Python
  • Rearrange the array in alternating positive and negative items with O(1) extra space – C | C++ | Java | Python
  • Given an array of size n and a number k, find all elements that appear more than ” n/k ” times. – C | C++ | Java | Python
  • Maximum profit by buying and selling a share atmost twice – C | C++ | Java | Python
  • Next Permutation – C | C++ | Java | Python
  • Find longest consecutive subsequence – C | C++ | Java | Python
  • Trapping Rain water problem – C | C++ | Java | Python
  • Chocolate Distribution problem – C | C++ | Java | Python
  • Smallest Subarray with sum greater than a given value – C | C++ | Java | Python
  • Three way partitioning of an array around a given value – C | C++ | Java | Python
  • Minimum no. of operations required to make an array palindrome – C | C++ | Java | Python
  • Median of 2 sorted arrays of equal size – C | C++ | Java | Python
  • Median of 2 sorted arrays of different size – C | C++ | Java | Python

Codes related to Matrix

  • Spiral traversal on a Matrix – C | C++ | Java | Python
  • Search an element in a matrix – C | C++ | Java | Python
  • Find median in a row wise sorted matrix – C | C++ | Java | Python
  • Find row with maximum no. of 1’s – C | C++ | Java | Python
  • Print elements in sorted order using row-column wise sorted matrix – C | C++ | Java | Python
  • Find a specific pair in matrix – C | C++ | Java | Python
  • Rotate matrix by 90 degrees – C | C++ | Java | Python
  • Kth smallest element in a row-column wise sorted matrix – C | C++ | Java | Python
  • Common elements in all rows of a given matrix – C | C++ | Java | Python

Pattern Printing

array
           ****
           ****
           ****
           ****

Square Star Pattern

Difficulty – ★

Checkout code for this program –

           ****
           *  *
           *  *
           ****

Hollow Square Star Pattern

Difficulty – ★ and 1/2

Checkout code for this program –

           ****
            ****
             ****
              ****

Rhombus Star Pattern

Difficulty – ★★

Checkout code for this program –

           ******
           ******
           ******
           ******

Rectangle Star Pattern

Difficulty – ★

Checkout code for this program –

           ******
           *    *
           *    *
           ******

Hollow Rectangle Star Pattern

Difficulty – ★ and 1/2

Checkout code for this program –

           ******
            ******
             ******
              ******

Parallelogram Star Pattern

Difficulty – ★★

Checkout code for this program –

           ****
          ****
         ****
        ****

Mirrored Rhombus Star Pattern

Difficulty – ★★ and 1/2

Checkout code for this program –

           *
           **
           ***
           ****

Triangle Star Pattern

Difficulty – ★ and 1/2

              *
             ***
            *****
           *******

Pyramid Star Pattern

Difficulty – ★★

              *
             * *
            *   *
           *******

Hollow Pyramid Star Pattern

Difficulty – ★★

           *******
            *****
             ***
              *

Inverted Pyramid Star Pattern

Difficulty – ★★

           *******
            *   *
             * *
              *

Inverted Hollow Pyramid Star Pattern

Difficulty – ★★ and 1/2

           *
           **
           ***
           ****
           ***
           **
           *
Half Diamond Star Pattern
              *
             **
            ***
           ****
            ***
             **
              *

Half Diamond Star Pattern Inverted

             *
            ***
           *****
          *******
           *****
            ***
             *

Diamond Star Pattern

Number Pattern Printing Programs

           1111
           1111
           1111
           1111

Basic Square 1 Pattern

Checkout code for this program –

           1111
           2222
           3333
           4444

Basic Square incrementing Pattern

Checkout code for this program –

           333
           313
           323
           333

Internal varsity square Pattern

Checkout code for this program –

           1
           23
           456
           78910

Basic Right Triangle Number Pattern

Checkout code for this program –

           10987
           456
           32
           1

Basic Right Triangle Number Pattern (Inverted)

Checkout code for this program –

           6666
           555
           44
           3

Basic incrementing Triangle Pattern initialised = 3

Checkout code for this program –

           3
           44
           555
           6666
Basic incrementing Triangle Pattern(Inverted) initialised = 3 Checkout code for this program –
           3
           4 5
           6 7 8
           9 10 11 12
Basic double incrementing Triangle Pattern initialised = 3 Checkout code for this program –
           3
           44
           555
           6666
           555
           44
           3

Basic incrementing Diamond Pattern(Inverted) initialised = 3

Checkout code for this program –

            3
            45
            678
            9101112
            678
            45
            3

Basic double incrementing Triangle Pattern initialised = 3

Checkout code for this program –

           3
           54
           876
           1211109
           876
           54
           3

Basic incrementing Triangle Pattern(Inverted) initialised Mirrored = 3

Checkout code for this program –

           2
           33
           444
           5555
           5555
           444
           33
           2

Basic incrementing Diamond Pattern(Inverted Sandwich) initialised = 3

Checkout code for this program –

            2
            34
            567
            891011
            891011
            567
            34
            2

Basic double incrementing Triangle Pattern initialised Sandwich= 3

Checkout code for this program –

           2
           43
           765
           1110198
           1110198
           765
           43
           2

Basic incrementing Triangle Pattern(Inverted) initialised Mirrored Sandwich = 3

Checkout code for this program –

Number Star Mix Pattern

           1*2*3*4
           5*6*7*8
           9*10*11*12
           13*14*15*16

Basic incrementing Squared Number-Star Pattern 

Checkout code for this program

C | Java | Python

           13*14*15*16
           9*10*11*12
           5*6*7*8
           1*2*3*4

Basic incrementing inverted Squared Number-Star Pattern

Checkout code for this program

C | Java | Python

          1*2*3*4
          9*10*11*12
          5*6*7*8
          13*14*15*16

Basic incrementing Squared Number-Star Pattern + Basic incrementing inverted Squared Number-Star Pattern (alternate)

Checkout code for this program

C | Java | Python

          1*2*3*4
          9*10*11*12
          13*14*15*16
          5*6*7*8

Basic incrementing Squared Number-Star Pattern + Basic incrementing inverted Squared Number-Star Pattern (alternate)

Type 2

Checkout code for this program

C | Java | Python

          1*2*3*4
          9*10*11*12
          17*18*19*20
          13*14*15*16
          5*6*7*8

Basic incrementing Squared Number-Star Pattern + Basic incrementing inverted Squared Number-Star Pattern (alternate)

Type 3

Checkout code for this program

C | Java | Python

          4*3*2*1
          12*11*10*9
          8*7*6*5
          16*15*14*13

Basic incrementing Squared Number-Star Pattern Mirrored + Basic incrementing inverted Squared Number-Star Pattern Mirrored (alternate)

Checkout code for this program

C | Java | Python

           1
           2*3
           4*5*6
           7*8*9*10

Basic incrementing Triangle Pattern

Checkout code for this program

C | Java | Python

           7*8*9*10
           4*5*6
           2*3
           1

Basic incrementing Triangle Pattern

Checkout code for this program

C | Java | Python

           1
           4*5*6
           2*3
           7*8*9*10

Basic incrementing Triangle Pattern + Inverted (Mix)

Checkout code for this program

C | Java | Python

           1
           3*2
           6*5*4
           10*9*8*7

Basic incrementing mirrored Triangle Pattern

Checkout code for this program

C | Java | Python

           10*9*8*7
           6*5*4
           3*2
           1

Basic incrementing inverted mirrored Triangle Pattern

Checkout code for this program

C | Java | Python

           1
           4*5*6
           2*3
           7*8*9*10

Basic incrementing Triangle Pattern + Inverted (Mix)

Checkout code for this program

C | Java | Python

           1
           2*2
           3*3*3
           4*4*4*4
           3*3*3
           2*2
           1

Basic Diamond Number Star Pattern

Checkout code for this program

C | Java | Python

           1
           2*2
           3*3*3
           4*4*4*4
           4*4*4*4
           3*3*3
           2*2
           1

Basic Diamond Number Star Pattern (Sandwich)

Checkout code for this program

C | Java | Python

           4*4*4*4
           3*3*3
           2*2
           1
           1
           2*2
           3*3*3
           4*4*4*4

Basic Diamond Number Star Pattern (Sandwich Inverted)

Checkout code for this program

C | Java | Python

           2
           3*3
           4*4*4
           3*3
           2

Basic Diamond Number Star Pattern initialised

Checkout code for this program

C | Java | Python

           2
           3*3
           4*4*4
           4*4*4
           3*3
           2

Basic Diamond Number Star Pattern (Sandwich)

Checkout code for this program

C | Java | Python

           6*6*6*6
           5*5*5
           4*4
           3
           3
           4*4
           5*5*5
           6*6*6*6

Basic Diamond Number Star Pattern (Sandwich Inverted)

Checkout code for this program

C | Java | Python

Few more pattern programs