DSAFreaks PS-1 Program Notes
1. Pattern Programs
1. Rectangle Star Pattern

Code –
Java
C++
public class DSAFREAKS
{
public static void rightTriangle(int n, int m){
for(int i = 1; i <= n; i++){ //outer loop for number of rows(n)
for(int j = 1; j <= m; j++){ // inner loop for columns
System.out.print("* "); // print star
}
System.out.println(); // ending line after each row
}
}
public static void main(String args[]){
int n = 5, m = 6;
rightTriangle(n, m);
}
}
#include <iostream>
using namespace std;
void rightTriangle(int n, int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << "* ";
}
cout << endl;
}
}
int main() {
int n = 5, m= 6;
rightTriangle(n, m);
return 0;
}
2. Right Triangle Star Pattern

Code –
Java
C++
public class DSAFREAKS
{
public static void rightTriangle(int n){
for(int i = 1; i <= n; i++){ //outer loop for number of rows(n)
for(int j = 1; j <= i; j++){ // inner loop for columns
System.out.print("* "); // print star
}
System.out.println(); // ending line after each row
}
}
public static void main(String args[]){
int n = 5;
rightTriangle(n);
}
}
#include <iostream>
using namespace std;
void rightTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
}
int main() {
int n = 5;
rightTriangle(n);
return 0;
}
3. Downward Triangle Star Pattern

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args){
int rows = 5;
for (int i = rows; i >= 1; i--){
for (int j = 1; j <= i; j++){
System.out.print("* ");
}
System.out.println();
}
}
}
#include <iostream>
using namespace std;
int main() {
int rows;
cin>>rows;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
4. Mirrored Right Triangle Star Pattern

Code –
Java
C++
public class Main
{
public static void printStars(int n) {
for(int i = 1; i <= n; i++){ //outer loop for number of rows(n)
for(int j = n-i; j >= 1; j--){ // inner loop for spaces
System.out.print(" "); // printing space
}
for(int j = 1; j <= i; j++){ // inner loop for columns
System.out.print("* "); // print star
}
System.out.println(); // ending line after each row
}
}
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
#include <iostream>
using namespace std;
void printStars(int n) {
for (int i = 1; i <= n; i++) {
for (int j = n - i; j >= 1; j--) {
cout << " ";
}
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
}
int main() {
int n = 5;
printStars(n);
return 0;
}
5. Right down Mirror Star Pattern

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args){
int rows = 5;
for (int i = rows; i >= 1; i--){
for (int j = i; j < rows; j++){
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
#include <iostream>
using namespace std;
int main(){
int rows = 5;
for (int i = rows; i >= 1; i--){
for (int j = i; j < rows; j++){
cout << " ";
}
for (int j = 1; j <= i; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
6. Pyramid Program

Code –
Java
C++
public class DSAFREAKS
{
public static void pyramidPattern(int n) {
for (int i = 1; i <= n; i++){ //outer loop for number of rows(n)
for (int j = 1; j <= n-i; j++){ //inner loop for spaces
System.out.print(" "); //print space
}
for (int j = 1; j <= i; j++ ){ //inner loop for number of columns
System.out.print("* "); //print star
}
System.out.println(); //ending line after each row
}
}
public static void main(String args[]) //driver function
{
int n = 5;
pyramidPattern(n);
}
}
#include <iostream>
using namespace std;
void printStars(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
cout << " ";
}
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
}
int main() {
int n = 5;
printStars(n);
return 0;
}
7. Reversed Pyramid Star Pattern

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args){
int rows = 5;
for (int i = 1; i <= rows; i++){
for (int j = 1; j < i; j++){
System.out.print(" ");
}
for (int j = 1; j <= rows-i+1; j++){
System.out.print("* ");
}
System.out.println();
}
}
}
#include <iostream>
using namespace std;
int main(){
int rows = 5;
for (int i = 1; i <= rows; i++){
for (int j = 1; j < i; j++){
cout<<" ";
}
for (int j = 1; j <= rows-i+1; j++){
cout<<"* ";
}
cout<<endl;
}
return 0;
}
8. Diamond Shape Pattern Program in Java

Code –
Java
C++
public class Main{
public static void main(String args[]){
int n = 5, space = n-1;
for (int i = 1; i <= n; i++){
for (int j = 1; j<= space; j++){
System.out.print(" ");
}
space--;
for (int j = 1; j <= 2 * i - 1; j++){
System.out.print("*");
}
System.out.println();
}
space = 1;
for (int i = 1; i <= n - 1; i++){
for (int j = 1; j <= space; j++){
System.out.print(" ");
}
space++;
for (int j = 1; j <= 2 * (n - i) - 1; j++){
System.out.print("*");
}
System.out.println();
}
}
}
#include<iostream>
using namespace std;
int main() {
int n = 5, space = n-1;
for (int i = 1; i <= n; i++){
for (int j = 1; j<= space; j++){
cout << " ";
}
space--;
for (int j = 1; j <= 2 * i - 1; j++){
cout << "*";
}
cout << endl;
}
space = 1;
for (int i = 1; i <= n - 1; i++){
for (int j = 1; j <= space; j++){
cout << " ";
}
space++;
for (int j = 1; j <= 2 * (n - i) - 1; j++){
cout << "*";
}
cout << endl;
}
return 0;
}
9. Hollow Triangle Star pattern

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args){
int rows = 5;
for (int i=1; i<= rows ; i++){
for (int j = i; j < rows ; j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
#include<iostream>
using namespace std;
int main() {
int rows = 5;
for (int i=1; i<= rows ; i++){
for (int j = i; j < rows ; j++) {
cout<<" ";
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
cout<<"*";
}
else {
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
10. Simple number program

Code –
Java
C++
public class DSAFREAKS{
public static void printNums(int n) {
int i, j,num;
for(i=0; i<n; i++) {
num=1;
for(j=0; j<=i; j++) {
System.out.print(num+ " ");
num++;
}
System.out.println();
}
}
public static void main(String args[]){
int n = 5;
printNums(n);
}
}
#include<iostream>
using namespace std;
int main() {
int 5;
for(int i=0; i<n; i++) {
int num=1;
for(int j=0; j<=i; j++) {
cout<<num<<" ";
num++;
}
cout<<endl;
}
return 0;
}
11. Pattern 11

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args) {
int k = 1, n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j< i + 1; j++) {
System.out.print(k++ + " ");
}
System.out.println();
}
}
}
#include<iostream>
using namespace std;
int main() {
int k = 1, n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j< i + 1; j++) {
System.out.print(k++ + " ");
}
System.out.println();
}
return 0;
}
12. Diamond Pattern using Numbers

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args) {
for (int i = 1; i <= 4; i++){
int n = 4;
for (int j = 1; j<= n - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k);
}
for (int l = 2; l <= i; l++) {
System.out.print(l);
}
System.out.println();
}
for (int i = 3; i >= 1; i--){
int n = 3;
for (int j = 0; j<= n - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k);
}
for (int l = 2; l <= i; l++){
System.out.print(l);
}
System.out.println();
}
}
}
#include<iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k);
}
for (int l = 2; l <= i; l++) {
System.out.print(l);
}
System.out.println();
}
for (int i = 3; i >= 1; i--){
int n = 3;
for (int j = 0; j<= n - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k);
}
for (int l = 2; l <= i; l++){
System.out.print(l);
}
System.out.println();
}
return 0;
}
13. Binary Number Pattern

Code –
Java
C++
public class DSAFREAKS{
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int num;
if(i%2 == 0){
num = 0;
for (int j = 1; j <= rows; j++){
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
else{
num = 1;
for (int j = 1; j <= rows; j++){
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
System.out.println();
}
}
}
#include<iostream>
using namespace std;
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int num;
if(i%2 == 0){
num = 0;
for (int j = 1; j <= rows; j++){
cout<<num;
num = (num == 0)? 1 : 0;
}
}
else{
num = 1;
for (int j = 1; j <= rows; j++){
cout<<num;
num = (num == 0)? 1 : 0;
}
}
cout<<endl;
}
return 0;
}
14. Pascal’s Triangle Program in Java

Code –
Java
C++
public class Main{
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for(int j = 1; j < n-i; j++){
System.out.print(" ");
}
int number = 1;
for (int j = 0; j <= i; j++) {
System.out.print(number + " ");
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
2. Basic logical problems
S.N | Topic | Problem Link | Solution 1 | Solution 2 |
---|---|---|---|---|
1 | Swap Two Numbers | GFG | GFG | |
2 | Check character is Vowel or consonant | GFG | ||
3 | Largest of three numbers | GFG | ||
4 | Check given year is leap year or not | GFG | ||
5 | Print Floyd’s Triangle | GFG | ||
6 | Convert decimal to binary | GFG | ||
7 | Sum of n natural numbers | GFG | ||
8 | Check prime number | GFG | ||
9 | Print all prime till N | GFG | ||
10 | Print Fibonacci Series till N | GFG | ||
11 | Count digits in a number ‘n’ | GFG | ||
12 | Power of a number | GFG | ||
13 | Sum of digits of a number | GFG | ||
14 | Reverse a number ‘n’ | GFG | ||
15 | Rotate a number by K | GFG | ||
16 | Factorial of a number | GFG | ||
17 | GCD/HCF of two numbers | GFG | ||
18 | LCM of two numbers | GFG | ||
19 | Number is palindrome or not | GFG | ||
20 | Check Armstrong number | GFG | ||
21 | Number is perfect or not | GFG | ||
22 | Decimal to any base | GFG | ||
23 | Any base to decimal | GFG | ||
24 | Any base to Any base | GFG |
3. Arrays(1D/2D)
S.N | Topic | Problem Link | Solution 1 | Solution 2 |
---|---|---|---|---|
1 | Find maximum/minimum elements | GFG | ||
2 | Print array in reverse order | GFG | ||
3 | Find element in an array | GFG | ||
4 | Average of all elements | GFG | ||
5 | Two Sum (Naive Approach) | GFG | ||
6 | Rotate an array | GFG | ||
7 | Bar chart using array elements | |||
8 | Read matrix from user | GFG | ||
9 | Add two matrices | GFG | ||
10 | Multiply two matrices | GFG | ||
11 | Transpose of matrix | Leetcode | ||
12 | Spiral of a matrix | GFG | ||
13 | Rotate a matrix | GFG | ||
14 | Ring Rotate a matrix | GFG | ||
15 | Saddle Point | GFG |
4. Strings
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Count number of words in string | GFG | |
2 | Print all the substrings of a string | ||
3 | Program to remove all spaces from string | GFG | |
4 | Check anagram of two strings | Leetcode | |
5 | String is palindrome or not | Leetcode | |
6 | Print all palindromic substrings | Leetcode | |
7 | String Compression | Leetcode | |
8 | Toggle every character | GFG |
5. Time Complexity

6. Binary Search Algorithm
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Search Target in Sorted array | Leetcode | |
2 | Search Insert Position | Leetcode | |
3 | Find First and Last Position of Element | Leetcode | |
4 | Number of Occurrences | GFG | |
5 | Ceil and Floor a number in sorted array | GFG | |
6 | Find Minimum in Rotated Sorted Array | Leetcode | |
7 | Search in Rotated Sorted Array | Leetcode | |
8 | Find element index in infinite sorted array | ||
9 | Search Element in 2D Array | Leetcode | |
10 | Search a 2D Matrix II | Leetcode | |
11 | Find Peak Element | Leetcode | |
12 | Find Peak Element in 2d Array | Leetcode | |
13 | Koko eating bananas | Leetcode | |
14 | Allocate Minimum Number Pages | GFG | |
15 | Find Smallest Divisor | Leetcode | |
16 | Capacity To Ship Packages Within D Days | Leetcode |
7. Array Problems Part 2
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Move Zeroes | Leetcode | |
2 | Missing Number | Leetcode | |
3 | Max Consecutive Ones | Leetcode | |
4 | Largest subarray with sum equals K(Only Positives) | ||
5 | Sort an array of 0’s and 1’s | GFG | |
6 | Sort an array of 0’s, 1’s and 2’s | Leetcode | |
7 | Maximum sum subarray (Kadane’s Algorithm) | Leetcode | |
8 | Majority Element | Leetcode | |
9 | Majority Element II | Leetcode | |
10 | Next Permutation | Leetcode | |
11 | Set Matrix Zeroes | Leetcode | |
12 | Maximum Product subarray | Leetcode |
8. Recursion
1. Understand Recursion
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Print Decreasing | GFG | |
2 | Print Increasing | GFG | |
3 | Print Increasing Decreasing | ||
4 | Factorial | GFG | |
5 | Power(x, n) using recursion | Leetcode | |
6 | Power(x, n) logarithmic | Leetcode | |
7 | Understanding Euler Tree Representation | ||
8 | Sort an array using recursion | GFG | |
9 | Sort an Stack using recursion | GFG | |
10 | Tower of Hanoi | GFG |
2. Choices Technique/Problems
9. Imp. Sorting Algorithms
10. Stack and Queues
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Implement Stack using Array | GFG | Learn More |
2 | Implement Queue using Array | GFG | Learn More |
3 | Implement Stack using Queue | Leetcode | |
4 | Implement Queue using Stack | Leetcode | |
5 | Duplicate Brackets | GFG | |
6 | Balanced Brackets | Leetcode | |
7 | Next greater element to the right | Leetcode | |
8 | Stock Span Problem | GFG | |
9 | Largest area Histogram | Leetcode | |
10 | Sliding Window Maximum | Leetcode | |
11 | Infix Evaluation | Leetcode | |
12 | Infix Conversion in Prefix and postfix | ||
13 | Postfix Evaluation and Conversion | ||
14 | Prefix Evaluation and Conversion | ||
15 | The Celebrity Problem | GFG | |
16 | Merge Overlapping Intervals | Leetcode | |
17 | Remove K Digits | Leetcode | |
18 | Sum of subarray Minimums | Leetcode | |
19 | Asteroid Collisions | Leetcode |
11. Priority Queue/Heap
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Kth Largest Element/K Largest Elements | Leetcode | |
2 | Kth Smallest Element/ K Smallest Elements | ||
3 | Sort K Sorted Array | GFG | |
4 | Merge K Sorted Arrays | GFG | |
5 | Find Median from Data Stream | Leetcode | |
6 | Kth Largest Element in a Stream | Leetcode | |
7 | Top K Frequent Elements | Leetcode | |
8 | Minimum cost of Ropes | GFG | |
9 | Implement Priority Queue |
12. HashMap/HashSet
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Highest Frequency Character | ||
2 | Intersection of Two Arrays(No Duplicates) | Leetcode | |
3 | Intersection of Two Arrays(With Duplicates) | ||
4 | Two Sum | Leetcode | |
5 | Longest Consecutive Sequence | Leetcode | |
6 | First repeating character | GFG | |
7 | Subarray Sum equals K | Leetcode | |
8 | Longest subarray with sum k | GFG | |
9 | Longest substring without repeating characters | Leetcode | |
10 | Uncommon Characters | GFG | |
11 | Uncommon Words | Leetcode | |
12 | Longest Subarray with equal 0’s and 1’s | GFG | |
13 | Count Subarrays with equal 0’s and 1’s | GFG | |
14 | Longest subarray with sum divisible by K | GFG | |
15 | Longest Palindrome | Leetcode | |
16 | Zero Sum Subarray | GFG | |
17 | Equivalent Sub-Arrays | GFG | |
18 | Count of substrings containing K ones | GFG | |
19 | Valid Sudoku | Leetcode | |
20 | HashMap Implementation |
13. LinkedList
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Introduction of Linked List | ||
2 | AddLast in Linked List | ||
3 | Display Linked List | ||
4 | Remove First From LinkedList | ||
5 | Get in Linked List | ||
6 | Add First in Linked List | ||
7 | Add at index in Linked List | ||
8 | Remove Last in Linked List | ||
9 | Remove at index in Linked List | ||
10 | Reverse Linked List (Data – Iterative) | Leetcode | |
11 | Reverse Linked List (Pointer Iterative) | Leetcode | |
12 | Reverse Linked List (Recursively) | Leetcode | |
13 | Remove Kth node from end of the Linked List | Leetcode | |
14 | Middle of the Linked List (Tortoise & Hare Method) | Leetcode | |
15 | Merge Two Sorted Linked Lists | Leetcode | |
16 | Merge Sort a Linked List | Leetcode | |
17 | Remove Duplicates from Sorted List | Leetcode | |
18 | Is Linked List Palindrome | Leetcode | |
19 | Segregate odd even linked list | GFG | |
20 | Add two numbers | Leetcode | |
21 | Intersection of Two Linked List | Leetcode | |
22 | Cycle detection in linked list | Leetcode | |
23 | Starting point of Cycle | Leetcode | |
24 | Rotate List | Leetcode | |
25 | Copy List with Random Pointer | Leetcode | |
26 | Reverse List in K-Group | Leetcode |
14. Binary Tree
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Binary Tree Introduction | ||
2 | Display a Binary Tree | ||
3 | Size, Sum, Max And Height of Binary Tree | ||
4 | Binary Tree Traversals(Pre, Post & In Order) | ||
5 | Iterative Pre order traversal | Leetcode | |
6 | Iterative In Order Traversal | Leetcode | |
7 | Iterative Post Order Traversal | Leetcode | |
8 | Level Order Traversal | Leetcode | |
9 | Root to Leaf Paths in binary tree | Leetcode | |
10 | Find node and Node to Root Path in binary tree | GFG | |
11 | Print K levels down | GFG | |
12 | K distance from root | Leetcode | |
13 | Clone a Binary Tree | GFG | |
14 | Remove Leaf Nodes | Leetcode | |
15 | Diameter of Binary Tree | Leetcode | |
16 | Balanced Binary Tree | Leetcode | |
17 | Zig Zag Level Order Traversal | Leetcode | |
18 | Vertical Order Traversal | Leetcode | |
19 | Left/Right View of Binary Tree | Leetcode | |
20 | Top View of Binary Tree | GFG | |
21 | Bottom View of Binary Tree | GFG | |
22 | LCA of a Binary Tree | Leetcode | |
23 | Maximum width of Binary Tree | Leetcode | |
24 | Construct Binary Tree using Preorder and Inorder | Leetcode | |
25 | Construct Binary Tree using Postorder and Inorder | Leetcode | |
26 | Maximum Path sum in binary tree | Leetcode |
15. Binary Search Tree
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Binary Search Tree Introduction | ||
2 | Size and sum of BST | ||
3 | Find in BST | Leetcode | |
4 | Max and min in BST | ||
5 | Add node in BST | Leetcode | |
6 | Delete Node in BST | Leetcode | |
7 | Add the sum of greater values | GFG | |
8 | LCA of BST | Leetcode | |
9 | Range sum of BST | Leetcode | |
10 | Target Sum Pair in BST | Leetcode | |
11 | Check if a Binary Tree is BST | Leetcode | |
12 | Find Predecessor and Successor | GFG | |
13 | Kth Smallest/Largest node | Leetcode |
16. Sliding Window & Two pointer
S.N | Topic | Problem Link | Solution |
---|---|---|---|
1 | Sum of all subarray of length K (Introduction) | GFG | |
2 | Max/Min Sum of Subarrays With Length K | GFG | |
3 | Maximum Sum of Distinct Subarrays With Length K | Leetcode | |
4 | Find all Anagrams in a String | Leetcode | |
5 | Sliding Window Maximum | Leetcode | |
6 | Longest K unique characters substring | GFG | |
7 | Longest Substring Without Repeating Characters | Leetcode | |
8 | Max consecutive One III | Leetcode | |
9 | Binary Subarrays With Sum | Leetcode | |
10 |