UNIT I Algorithmic Problem Solving
UNIT I Algorithmic Problem Solving
Computing is the process of using computer hardware and software to input, store, process, and output
data/information. A computer system is composed of six fundamental components:
1. Hardware
Hardware refers to the physical, tangible components of a computer that can be seen and touched.
• Central Processing Unit (CPU) – Intel Core i9, AMD Ryzen
• Memory (RAM) – 8 GB, 16 GB DDR4 RAM
• Storage – 512 GB SSD, 1 TB Hard Disk
• Motherboard – connects all components together
• GPU – NVIDIA RTX 4090 for graphics processing
2. Software
Software is a set of programs and instructions that tell the hardware what to do. It is intangible.
Type Description Examples
System Software Manages hardware Windows 11, Linux, macOS
resources
Application Software Performs specific user tasks MS Word, Chrome, VLC
Programming Tools Help develop software Python, Java, VS Code, GCC
Utility Software Maintenance & support tasks Antivirus, Disk Cleaner
3. Input
Input is the raw data fed into a computer for processing. Input devices convert real-world data into
digital form.
• Keyboard – entering text and commands
• Mouse – pointing and clicking for navigation
• Scanner – converting paper documents to digital images
• Microphone – voice input for speech recognition
• Webcam – video input for video calls
4. Output
Output is the processed result delivered by the computer to the user or another system.
• Monitor – displays visual information (text, images, video)
• Printer – produces hard copies of documents
• Speakers – produce audio output
• Projector – displays output on large screen
5. Storage
Storage devices hold data permanently or temporarily for future use.
Type Location Example Speed
Primary (RAM) Inside CPU 8 GB DDR4 Very Fast – temporary
Secondary External to CPU SSD, HDD Moderate – permanent
Cache Inside CPU chip L1, L2, L3 Fastest – very small
Cloud Remote servers Google Drive Depends on internet
6. Processing
Processing is the core activity where the CPU transforms raw input data into meaningful output using
arithmetic, logic, and control operations.
• ALU (Arithmetic Logic Unit) – performs +, -, ×, ÷ and AND, OR, NOT
• Control Unit (CU) – fetches, decodes, and executes instructions
• Registers – tiny, ultra-fast temporary storage inside CPU
Example:
Input : User types '5 + 3' on keyboard
Process : CPU computes 5 + 3 = 8
Output : Monitor displays 8
Storage : Result may be saved to disk
Computers internally use binary (base-2) because electronic circuits have two states: ON (1) and OFF
(0). Different numbering systems are used for different purposes.
Method: Repeatedly divide the decimal number by 2. Record the remainders from bottom to top.
Result:
45₁₀ = 101101₂
Read remainders from bottom to top: 1 0 1 1 0 1
Result:
156₁₀ = 10011100₂
Method: Multiply each bit by 2 raised to its positional power (right to left, starting from 0), then sum all
results.
Result:
101101₂ = 45₁₀
Result:
11001010₂ = 202₁₀
Method: Repeatedly divide the decimal number by 8. Record remainders from bottom to top.
Result:
255₁₀ = 377₈
Result:
1500₁₀ = 2734₈
Result:
255₁₀ = FF₁₆
Example 2: Convert 2748 to Hexadecimal
2748 ÷ 16 = 171 R 12 → C
171 ÷ 16 = 10 R 11 → B
10 ÷ 16 = 0 R 10 → A
Result:
2748₁₀ = ABC₁₆
A. Logical Thinking
Logical thinking is the ability to analyse a situation, identify relationships and patterns, and draw valid
conclusions using reason and evidence. It is the foundation of rational problem solving.
Real-life Example:
Situation: The light in the room is not working.
B. Algorithmic Thinking
Algorithmic thinking is the ability to solve a problem by defining a clear, precise, step-by-step sequence
of instructions that can be executed by a human or computer to produce the desired result.
Problem solving in computing is a structured process that transforms a problem statement into a
working solution. It involves three major phases:
Activities:
• Identify the most efficient approach (brute force, divide & conquer, etc.)
• Write pseudocode or draw a flowchart
• Choose appropriate data structures
• Consider edge cases and boundary conditions
• Evaluate time and space complexity
Step 3: Decomposition
Decomposition is the process of breaking a large, complex problem into smaller, manageable sub-
problems. Each sub-problem is solved independently and the solutions are combined.
Benefits of Decomposition:
• Makes complex problems easier to understand
• Sub-problems can be solved in parallel
• Promotes code reuse through functions/modules
• Easier to test and debug individual parts
Sub-problems:
1. Input sub-system → Read student name, ID, marks for each subject
2. Calculation sub-sys → Compute total, average, grade for each student
3. Comparison sub-sys → Find class topper and rank students
4. Output sub-system → Format and print the report card
Definition
An algorithm is a finite, ordered set of unambiguous and executable instructions that solves a given
problem in a finite amount of time. It takes input, processes it through a series of steps, and produces a
definite output.
Origin: The word 'algorithm' comes from the name of the 9th-century mathematician Muhammad ibn
Musa al-Khwarizmi.
# Characteristic Explanation
1 Finiteness Must terminate after a finite number of steps – no infinite loops
2 Definiteness Every step must be precisely and unambiguously defined
3 Input Has zero or more well-defined inputs
4 Output Produces at least one meaningful output
5 Effectiveness Every step must be basic and executable in finite time
6 Correctness Produces the correct output for all valid inputs
7 Generality Works for all instances of the problem, not just specific cases
Step 1: START
Step 2: READ N
Step 3: SET sum = 0, i = 1
Step 4: WHILE i <= N DO
Step 5: sum = sum + i
Step 6: i = i + 1
Step 7: END WHILE
Step 8: PRINT sum
Step 9: STOP
Trace for N = 4:
i=1: sum=1 i=2: sum=3 i=3: sum=6 i=4: sum=10
Output: 10
Algorithm
Algorithm: LARGEST_THREE
Input : Three numbers A, B, C
Output : The largest among A, B, C
Step 1: START
Step 2: READ A, B, C
Step 3: IF A >= B AND A >= C THEN
Step 4: PRINT 'Largest is A =', A
Step 5: ELSE IF B >= A AND B >= C THEN
Step 6: PRINT 'Largest is B =', B
Step 7: ELSE
Step 8: PRINT 'Largest is C =', C
Step 9: END IF
Step 10: STOP
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
┌────▼────────────────┐
│ READ A, B, C │
└────┬────────────────┘
│
┌─────────▼──────────────┐
│ Is A >= B AND A >= C? │
└──────┬──────────┬──────┘
YES│ │NO
┌───────▼──┐ ┌───▼─────────────────────┐
│ Print A │ │ Is B >= A AND B >= C ? │
└───────┬──┘ └───┬─────────────┬───────┘
│ YES│ │NO
│ ┌──────▼──┐ ┌────▼────┐
│ │ Print B │ │ Print C │
│ └──────┬──┘ └────┬────┘
│ │ │
└────┬────┘─────────────┘
│
┌────▼────┐
│ STOP │
└─────────┘
Q11. Prime Number Check – Algorithm and Flowchart
Definition
A prime number is a natural number greater than 1 that has no divisors other than 1 and itself.
Examples: 2, 3, 5, 7, 11, 13...
Algorithm
Algorithm: CHECK_PRIME
Input : A positive integer N
Output : Whether N is PRIME or NOT PRIME
Step 1: START
Step 2: READ N
Step 3: IF N <= 1 THEN
Step 4: PRINT 'Not Prime'
Step 5: GOTO Step 12
Step 6: END IF
Step 7: SET i = 2
Step 8: WHILE i <= sqrt(N) DO
Step 9: IF N MOD i == 0 THEN
Step 10: PRINT 'Not Prime'
Step 11: GOTO Step 12
Step 12: END IF
Step 13: i = i + 1
Step 14: END WHILE
Step 15: PRINT 'Prime'
Step 16: STOP
Flowchart
┌─────────┐
│ START │
└────┬────┘
│
┌────▼──────────┐
│ READ N │
└────┬──────────┘
│
┌────────▼───────┐
│ Is N <= 1? │
└──┬──────────┬──┘
YES│ │NO
┌───────▼──┐ ┌────▼──────────────┐
│Not Prime │ │ SET i = 2 │
└───────┬──┘ └────┬──────────────┘
│ │
│ ┌──────▼───────────────┐
│ │ Is i <= sqrt(N) ? │
│ └──┬──────────────┬────┘
│ YES│ │NO
│ ┌────▼──────────┐ ┌▼──────┐
│ │Is N % i == 0? │ │ PRIME │
│ └──┬────────┬───┘ └───┬───┘
│ YES│ │NO │
│ ┌──▼──────┐ │i=i+1 │
│ │Not Prime│ └──────┐ │
│ └──┬──────┘ │ │
└────┘ │ │
└────┘
│
┌────▼────┐
│ STOP │
└─────────┘
A. Pseudocode
Pseudocode is an informal, high-level description of an algorithm using a mixture of natural language
and simplified programming-like syntax. It is NOT actual code – it cannot be compiled or run.
Characteristics:
• Language-independent – not tied to any programming language
• Easy to read and understand by non-programmers
• Uses keywords: BEGIN, END, READ, PRINT, IF, WHILE, FOR
• No strict syntax rules – uses indentation for structure
B. Flowchart
A flowchart is a graphical/pictorial representation of an algorithm using standard symbols and arrows to
show the flow of logic.
Symbol Shape Meaning
Oval / Rounded Rect ( START / STOP ) Terminal – Begin or End
Parallelogram / INPUT / OUTPUT / Input or Output operation
Rectangle [ PROCESS ] Processing / Computation
Diamond < DECISION > Decision (Yes/No branch)
Arrow → Flow of control
Circle () Connector between parts
Advantages of Flowcharts:
• Provides a visual overview of the entire program
• Easier to identify logic errors before coding
• Useful for explaining programs to non-technical stakeholders
• Serves as documentation for maintenance
C. Programming Language
A programming language is a formal, machine-readable language used to implement algorithms. Unlike
pseudocode, code in a programming language must follow strict syntax rules and can be
compiled/interpreted and executed.
Examples:
5! = 5 × 4 × 3 × 2 × 1 = 120
6! = 720
0! = 1
Pseudocode
BEGIN FACTORIAL
READ N
IF N < 0 THEN
PRINT 'Factorial undefined for negative numbers'
STOP
END IF
SET factorial = 1
SET i = 1
WHILE i <= N DO
factorial = factorial × i
i = i + 1
END WHILE
END FACTORIAL
Flowchart
┌──────────┐
│ START │
└────┬─────┘
│
┌────▼─────────┐
│ READ N │
└────┬─────────┘
│
┌───────▼──────┐
│ Is N < 0? │
└──┬───────┬───┘
YES│ │NO
┌──────▼───┐ ┌▼──────────────────┐
│ Error │ │ SET fact=1, i=1 │
│ Print │ └──────┬────────────┘
└──────┬───┘ │
│ ┌───────▼───────────┐
│ │ Is i <= N ? │
│ └──┬────────────┬───┘
│ YES│ │NO
│ ┌──────▼──────┐ ┌──▼──────────────────┐
│ │fact = fact×i│ │ PRINT fact │
│ │i = i + 1 │ └──────────────────────┘
│ └──────┬──────┘ │
│ └──────────┐ │
│ ↑ │
│ (loop back) │
└────────────────────────── ┘
│
┌────▼────┐
│ STOP │
└─────────┘
Python Implementation
n = int(input('Enter a number: '))
if n < 0:
print('Factorial undefined for negative numbers')
else:
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(f'{n}! = {factorial}')
Result:
Enter a number: 6
6! = 720
Definition
The program state (or computational state) is the complete snapshot of all the values stored in
variables, memory, and data structures at any particular moment during the execution of a program. As
the program executes each statement, the state changes.
Formal Definition: State = { (variable₁, value₁), (variable₂, value₂), ..., (variableₙ, valueₙ) } at a
specific point in execution
total = 0 # Line 1
count = 0 # Line 2
numbers = [10, 25, 15, 30, 20] # Line 3
# State progression:
# n=10, result=10 → n=9, result=19 → n=8, result=27
# ... → n=1, result=55 → n=0: loop exits
Concept Explanation
Initial State State of variables before program begins execution
Current State Snapshot of all variable values at a specific program line
State Transition How one statement changes the state (assignment, loop, call)
Final State State of variables after the program completes
Invalid State A state where variables hold unexpected/wrong values (a bug)