Friday, July 4, 2025

๐Ÿš€ DSA with C – Day 5: Strings in C


๐ŸŽฏ Goal:


๐Ÿง  What is a String in C?

A string is a character array ending with a null character (\0).

๐Ÿ”ง Declaration


char str[100]; // Character array

๐Ÿ“ฅ Input


scanf("%s", str); // No spaces fgets(str, sizeof(str), stdin); // With spaces

๐Ÿ‘จ‍๐Ÿ’ป 1. Reverse a String


#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); // Use fgets if you want spaces int len = strlen(str); for (int i = 0; i < len / 2; i++) { char temp = str[i]; str[i] = str[len - i - 1]; str[len - i - 1] = temp; } printf("Reversed string: %s\n", str); return 0; }

๐Ÿ‘จ‍๐Ÿ’ป 2. Check if a String is a Palindrome


#include <stdio.h> #include <string.h> int main() { char str[100]; int flag = 1; printf("Enter a string: "); scanf("%s", str); int len = strlen(str); for (int i = 0; i < len / 2; i++) { if (str[i] != str[len - i - 1]) { flag = 0; break; } } if (flag) printf("Palindrome\n"); else printf("Not a palindrome\n"); return 0; }

๐Ÿ‘จ‍๐Ÿ’ป 3. Character Frequency Count


#include <stdio.h> #include <string.h> int main() { char str[100]; int freq[256] = {0}; // ASCII chars printf("Enter a string: "); scanf("%s", str); for (int i = 0; str[i] != '\0'; i++) { freq[(int)str[i]]++; } printf("Character frequencies:\n"); for (int i = 0; i < 256; i++) { if (freq[i] > 0) printf("%c: %d\n", i, freq[i]); } return 0; }

๐Ÿ‘จ‍๐Ÿ’ป 4. Check if Two Strings Are Anagrams

Two strings are anagrams if they contain the same characters in any order.


#include <stdio.h> #include <string.h> int isAnagram(char str1[], char str2[]) { int count[256] = {0}; if (strlen(str1) != strlen(str2)) return 0; for (int i = 0; str1[i]; i++) { count[(int)str1[i]]++; count[(int)str2[i]]--; } for (int i = 0; i < 256; i++) { if (count[i] != 0) return 0; } return 1; } int main() { char str1[100], str2[100]; printf("Enter two strings: "); scanf("%s %s", str1, str2); if (isAnagram(str1, str2)) printf("Anagram\n"); else printf("Not anagram\n"); return 0; }

๐Ÿงช Practice Problems:

  1. Count vowels and consonants in a string.

  2. Convert lowercase letters to uppercase.

  3. Remove duplicate characters from a string.

  4. Check if two strings are rotations of each other (e.g., abc and cab).


๐Ÿ“˜ Homework for Day 5:

  • Implement your own versions of:

    • strlen()

    • strcpy()

    • strcmp()

  • Try a string problem from LeetCode: Valid Anagram

✅ Day 4 solution - DSA with C

 Find the GCD of Two Numbers Using Recursion

GCD (Greatest Common Divisor) using Euclidean Algorithm:


#include <stdio.h> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int num1, num2; printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2)); return 0; }

✅ 2. Recursive Function to Calculate Power (xโฟ)


#include <stdio.h> int power(int x, int n) { if (n == 0) return 1; return x * power(x, n - 1); } int main() { int base, exponent; printf("Enter base and exponent: "); scanf("%d %d", &base, &exponent); printf("%d^%d = %d\n", base, exponent, power(base, exponent)); return 0; }

✅ 3. Reverse an Array Using Recursion


#include <stdio.h> void reverse(int arr[], int start, int end) { if (start >= end) return; // Swap int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; reverse(arr, start + 1, end - 1); } int main() { int arr[100], n; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); reverse(arr, 0, n - 1); printf("Reversed array:\n"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }

Tuesday, July 1, 2025

๐Ÿš€ DSA with C – Day 4: Recursion + Factorial + Fibonacci + Array Sum

 

๐ŸŽฏ Goal:

  • Understand the concept of Recursion

  • Write recursive functions for:

    • Factorial

    • Fibonacci series

    • Sum of array elements

  • Compare recursion with iteration


๐Ÿง  What is Recursion?

A function calling itself to solve a smaller version of the same problem.

๐Ÿ”„ Recursion Structure:

return_type function_name(parameters) {
if (base_condition) return result; else return function_name(smaller_problem); }

๐Ÿ‘จ‍๐Ÿ’ป 1. Factorial Using Recursion

#include <stdio.h>
int factorial(int n) { if (n == 0 || n == 1) return 1; else return n * factorial(n - 1); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Factorial of %d is %d\n", num, factorial(num)); return 0; }

๐Ÿ‘จ‍๐Ÿ’ป 2. Fibonacci Series Using Recursion


#include <stdio.h> int fibonacci(int n) { if (n == 0) return 0; if (n == 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); } int main() { int terms; printf("Enter number of terms: "); scanf("%d", &terms); printf("Fibonacci Series: "); for (int i = 0; i < terms; i++) { printf("%d ", fibonacci(i)); } return 0; }

⚠️ Note: Recursive Fibonacci is inefficient for large n – we’ll optimize it later with memoization or iteration.


๐Ÿ‘จ‍๐Ÿ’ป 3. Sum of Array Using Recursion


#include <stdio.h> int sumArray(int arr[], int n) { if (n == 0) return 0; return arr[n - 1] + sumArray(arr, n - 1); } int main() { int arr[100], n; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Sum of array = %d\n", sumArray(arr, n)); return 0; }

๐Ÿงช Practice Problems:

  1. Find the GCD of two numbers using recursion.

  2. Write a recursive function to calculate the power (xโฟ).

  3. Reverse an array using recursion.


๐Ÿ“˜ Homework for Day 4:

  • Compare iteration vs recursion by writing factorial using loops.

  • Try writing Fibonacci with memoization or using a loop.

  • LeetCode problem suggestion: Climbing Stairs – this is basically Fibonacci!


๐Ÿ“… Day 4 Summary:

  • ✅ Understood recursion

  • ✅ Implemented factorial, Fibonacci, and sum via recursion

  • ✅ Practiced recursive thinking




Sunday, June 29, 2025

๐Ÿ“˜ Day 1: Introduction to Database Management System (DBMS)

 


✅ What is a Database?

A Database is an organized collection of data that can be easily accessed, managed, and updated.

๐Ÿง  Example: Your college stores data of students — names, roll numbers, marks — in a structured format.


✅ What is a DBMS?

A Database Management System (DBMS) is software that interacts with users, applications, and the database itself to capture and analyze data.

๐Ÿ’ก DBMS Examples:

  • MySQL

  • Oracle

  • PostgreSQL

  • MongoDB (NoSQL DB)


✅ Advantages of DBMS over File System

File SystemDBMS
Data RedundancyReduced Redundancy
No data sharingMulti-user support
No data securityHigh security
Difficult backupEasy backup & recovery
Complex queriesEasy SQL-based queries

✅ Types of DBMS

TypeDescriptionExample
HierarchicalParent-childIBM IMS
NetworkGraph structureIDS
RelationalTables (rows/columns)MySQL, Oracle
Object-orientedStores data as objectsVersant

๐Ÿง  MCA Exams usually focus on Relational DBMS (RDBMS).

✅ Components of DBMS

  1. Hardware: Physical devices (server, storage)

  2. Software: The DBMS itself (MySQL, etc.)

  3. Data: The actual stored data

  4. Users:

    • DBA (Database Admin)

    • End Users

    • Application Programmers

  5. Procedures: Rules for design & access


✅ Architecture of DBMS

๐Ÿ“Š 3-Tier Architecture:

  1. External Level: User view

  2. Conceptual Level: Logical structure

  3. Internal Level: Physical storage

Draw this in exams – it gets full marks!


+---------------------+ <-- External Level (View) | User 1 / User 2 ... | +---------------------++---------------------+ <-- Conceptual Schema | Logical Structure | +---------------------++---------------------+ <-- Internal Level (Storage) | Files, Indexes, Data| +---------------------+

๐Ÿ“ Day 1 Quick Questions (Important for Exams)

  1. What is a DBMS? Explain its advantages.

  2. Differentiate between DBMS and File System.

  3. Explain 3-tier architecture of DBMS with a diagram.

  4. List and describe the types of DBMS.

  5. Who are the users of DBMS?


๐Ÿ’ป Mini Practice (Optional)

If you have MySQL installed, try this:


CREATE DATABASE dbms_day1; USE dbms_day1; CREATE TABLE student ( id INT PRIMARY KEY, name VARCHAR(100), course VARCHAR(50) );

✅ Homework (15 mins)

  • Revise all notes above

  • Draw DBMS architecture diagram in your notebook

  • Write down 3 advantages of DBMS

  • Prepare answers for the 5 questions above

๐ŸŽฏ MCA DBMS Study Plan for Scoring High in College


We’ll break it down into:

๐Ÿ“˜ 1. Important Theory Topics (Frequently Asked in Exams)

๐Ÿ’ก 2. Expected Questions from Each Topic

๐Ÿ’ป 3. Basic SQL Practice (For Viva / Practical Exams)


๐Ÿ“˜ 1. Important Theory Topics

UnitTopicMust-Know Concepts
1Introduction to DBMSDefinition, advantages, DBMS vs RDBMS, applications
2Data Models & ER ModelEntity, attributes, relationships, ER diagrams
3Relational ModelTuple, attribute, schema, keys (primary, foreign)
4Relational AlgebraSelect, Project, Join, Union, Set Difference
5SQL BasicsCREATE, INSERT, SELECT, UPDATE, DELETE, WHERE, JOINS
6Normalization1NF, 2NF, 3NF, BCNF, functional dependency
7Transaction ManagementACID properties, commit, rollback
8Indexing & StorageB+ trees, hashing, file organization


๐Ÿ’ก 2. Important Questions to Study

Short Questions:

  • What is DBMS? List its advantages.

  • Define primary and foreign keys with examples.

  • What is normalization?

Long Questions (most scoring):

  • Draw and explain the architecture of DBMS.

  • Explain ER model with an example and convert it to tables.

  • What are the types of keys in DBMS? Explain with example.

  • Write and explain different SQL commands with examples.

  • Explain all normal forms with suitable examples.

  • What is a transaction? Explain ACID properties.

๐Ÿง  Tip: Draw diagrams in theory exams — ER diagrams, DBMS architecture, relational schema — these fetch full marks.


๐Ÿ’ป 3. Basic SQL Commands (Exam Viva Focus)


-- Create a table CREATE TABLE student ( id INT PRIMARY KEY, name VARCHAR(100), course VARCHAR(50) ); -- Insert data INSERT INTO student VALUES (1, 'Adarsh', 'MCA'); -- View table SELECT * FROM student; -- Update a record UPDATE student SET course = 'MCA Part 2' WHERE id = 1; -- Delete a record DELETE FROM student WHERE id = 1;

๐Ÿ’ก Learn:

  • INNER JOIN

  • GROUP BY and HAVING

  • Subqueries (simple SELECT inside WHERE)


๐Ÿ“… Weekly Exam-Focused Study Plan (Short-Term Revision)

DayFocus Area
Day 1DBMS Intro + Architecture + Advantages
Day 2ER Model + Diagram Practice
Day 3Keys + Relational Model
Day 4Relational Algebra + Set operations
Day 5SQL Basics (DDL, DML, DQL) + Practice
Day 6Normalization (1NF to BCNF)
Day 7Transactions + Indexing + Storage

๐Ÿ“ Bonus: Exam Writing Tips
  • Write definitions in your own words.

  • Underlined keywords: DBMS, Primary Key, ACID, etc.

  • Use diagrams: ER model, Architecture.

  • Give examples always — even for short answers.

  • Neat formatting = easy marks!

Saturday, June 28, 2025

✅ Day 4: Sorting Algorithms (Basic to Intermediate)


๐ŸŽฏ Goals for Today

  • Understand how sorting works and why it’s needed

  • Learn and implement:

    • Bubble Sort

    • Selection Sort

    • Insertion Sort

  • Analyze time & space complexities

  • Practice beginner sorting problems


๐Ÿ“˜ 1. Why Sorting Matters

Sorting helps:

  • Optimize searching (e.g., binary search)

  • Solve problems like duplicates, frequency, etc.

  • Prepare data for algorithms like two pointers, greedy, divide & conquer, etc.


๐Ÿ” 2. Sorting Algorithms

๐Ÿ”น Bubble Sort

Repeatedly swap adjacent elements if they are in the wrong order.


public class BubbleSort { public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { boolean swapped = false; for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { // swap int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = true; } } if (!swapped) break; } } }
  • Time Complexity: Worst → O(n²), Best → O(n) (already sorted)

  • Space: O(1)


๐Ÿ”น Selection Sort

Find the minimum element and put it at the beginning.


public class SelectionSort { public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // swap int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } }
  • Time Complexity: Always O(n²)

  • Space: O(1)


๐Ÿ”น Insertion Sort

Build the sorted array one element at a time by inserting elements at the correct position.


public class InsertionSort { public static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int current = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > current) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = current; } } }
  • Time Complexity: Best → O(n), Worst → O(n²)

  • Space: O(1)


๐Ÿง  3. Sorting Algorithm Comparison Table

AlgorithmTime Complexity (Best / Avg / Worst)SpaceStable?
Bubble SortO(n) / O(n²) / O(n²)O(1)
Selection SortO(n²) / O(n²) / O(n²)O(1)
Insertion SortO(n) / O(n²) / O(n²)O(1)


๐Ÿ” 4. Practice Problems

Try these:

  • Sort an array of 0s, 1s, and 2s (Dutch National Flag)

  • Check if an array is sorted and rotated

  • Count the number of swaps required to sort the array

  • Sort a nearly sorted (k-sorted) array

You can also implement a custom comparator or reverse order sort (forward-thinking Java practice).


๐Ÿ“š Homework for Day 4

✅ Implement all three sorting algorithms
✅ Solve 2 sorting-based problems from LeetCode:

Would you like solutions for these 2 LeetCode problems?


⏭️ What’s Coming on Day 5:

  • Advanced Sorting: Merge Sort, Quick Sort

  • Time Complexity Analysis

  • Recursion involved in sorting

๐Ÿš€ DSA with C – Day 3: Bubble Sort & Binary Search

 

๐ŸŽฏ Goal:

  • Learn Bubble Sort and sort arrays.

  • Understand how Binary Search works.

  • Compare Linear vs Binary Search.

  • Practice problems on sorted arrays.


๐Ÿง  Theory

✅ Bubble Sort:

  • Repeatedly compares adjacent elements and swaps them if they’re in the wrong order.

  • Worst-case time complexity: O(n²)

  • Best-case (already sorted): O(n) with optimization


๐Ÿ‘จ‍๐Ÿ’ป Bubble Sort in C


#include <stdio.h> int main() { int arr[100], n, i, j, temp; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (i = 0; i < n; i++) scanf("%d", &arr[i]); // Bubble Sort for (i = 0; i < n - 1; i++) { int swapped = 0; for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { // Swap temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = 1; } } if (swapped == 0) break; // Already sorted } printf("Sorted array:\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }

✅ Binary Search:

  • Efficient searching technique on sorted arrays.

  • Time Complexity: O(log n)

  • Repeatedly divide the array and compare with middle element.


๐Ÿ‘จ‍๐Ÿ’ป Binary Search in C


#include <stdio.h> int binarySearch(int arr[], int n, int key) { int low = 0, high = n - 1, mid; while (low <= high) { mid = (low + high) / 2; if (arr[mid] == key) return mid; else if (arr[mid] < key) low = mid + 1; else high = mid - 1; } return -1; // Not found } int main() { int arr[100], n, key; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d sorted elements:\n", n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Enter element to search: "); scanf("%d", &key); int result = binarySearch(arr, n, key); if (result == -1) printf("Element not found.\n"); else printf("Element found at index %d.\n", result); return 0; }

๐Ÿ” Linear vs Binary Search

FeatureLinear SearchBinary Search
Time ComplexityO(n)O(log n)
Sorted Array Required❌ No✅ Yes
SpeedSlowerFaster for large data
ApproachScan all elementsDivide & Conquer

๐Ÿงช Practice Problems:

  1. Sort an array using Bubble Sort in descending order.

  2. Count how many swaps were done during Bubble Sort.

  3. Use Binary Search to find the number of occurrences of a given element.

  4. If not found in Binary Search, return where it should be inserted.


๐Ÿ“˜ Homework for Day 3:

  • Implement recursive Binary Search.

  • Write a program that first sorts using Bubble Sort and then searches using Binary Search.

  • LeetCode problem suggestion: Search Insert Position


๐Ÿ“… Day 3 Summary

  • ✅ Learned Bubble Sort (with optimization)

  • ✅ Implemented Binary Search (iterative)

  • ✅ Practiced comparing search algorithms

✅ UNIT 4 — POSET, LATTICES & BOOLEAN ALGEBRA (DISCRETE MATHEMATICS)

  ✅ UNIT 4 — POSET, LATTICES & BOOLEAN ALGEBRA 1. Poset Partially Ordered Set A pair (A, ≤) where relation is: Reflexive Anti-...