ARRAYS
[Link] Statement
A teacher records the marks of N students in M subjects.
The marks are stored in a 2D array.
Rows → Students
Columns → Subjects
Write a program to calculate the total marks obtained by each student.
Constraints
1≤ N ≤10
1≤M≤10
Marks range from 0 to 100
Sample Input:
33
80 75 90
60 70 65
85 95 88
First line → number of students and subjects.
Sample Output:
Student 1 Total = 245
Student 2 Total = 195
Student 3 Total = 268
[Link] Statement
A shopkeeper sells 3 different products in his shop. For 3 days, he records:
How many items were available in stock
How many items were sold
The data is stored in two 2D arrays:
Stock array → items available
Sold array → items sold
Rows represent products, and columns represent days.
Write a program to calculate the remaining items after sales for each product on each day.
Formula:
Remaining = Stock – Sold
Display the remaining items in matrix format.
Constraints
1≤products≤10
1≤days≤7
0≤stock[ i ][ j]≤1000
0≤sold[ i ][ j]≤stock[ i][ j]
Sample Input:
Enter number of products and days:
33
Enter stock values:
50 40 30
60 50 45
70 65 60
Enter sold values:
10 5 8
15 10 5
20 10 15
Sample Output:
Remaining items:
40 35 22
45 40 40
50 55 45
[Link] Description
You are given a 1D array containing n distinct integers taken from the range 0 to n.
One number is missing from the array.
Write a program to find the missing number.
Input Format
First line: Integer n (size of array)
Second line: n space-separated integers
Output Format
Print the missing number
Sample Input
5
01245
Sample Output
3
[Link] Description
You are given an array representing the number of steps taken each day.
Find the number of days where the step count is strictly higher than the previous day.
Input Format
First line: Integer n (number of days)
Second line: n space-separated integers representing step counts
Output Format
A single integer representing the number of days where steps increased compared
to the previous day
Constraints
1 ≤ n ≤ 200
0 ≤ steps[i] ≤ 100000
For n = 1, output should be 0 (no previous day to compare)
Test Case 1
Input:
5
1000 2000 1500 1800 2500
Output:
3
Explanation:
2000 > 1000 ✔
1800 > 1500 ✔
2500 > 1800 ✔
Test Case 2
Input:
4
3000 3000 3000 3000
Output:
0
[Link] Book Arrangement System
A librarian maintains a list of book IDs. Every time a new book arrives, it is added to the list.
To keep the system efficient, the librarian wants the list to always remain sorted in ascending
order.
However, instead of sorting the entire list again, you must insert each new book into its
correct position using Insertion Sort logic.
Problem Statement
You are given an array of N book IDs.
Simulate the Insertion Sort process step-by-step and print the array after each iteration.
Input Format
First line: Integer N
Second line: N space-separated integers (book IDs)
Output Format
Print the array after each pass of insertion sort
Constraints
1 ≤ N ≤ 50
1 ≤ book ID ≤ 10^5
Do not use built-in sorting methods
Sample Input:
5
83529
Sample Output:
38529
35829
23589
23589
[Link] Price Tracker (First & Last Occurrence)
An e-commerce system stores product prices in sorted order.
Due to multiple sellers, the same price can appear multiple times.
When a user searches for a price, the system should return:
The first occurrence index
The last occurrence index
This helps in identifying the range of sellers offering that price.
Problem Statement
Given a sorted array of integers (prices) and a target value X,
find:
First occurrence of X
Last occurrence of X
If X is not found, return -1 -1.
Input Format
First line: Integer N
Second line: N sorted integers
Third line: Integer X
Output Format
Two integers: firstIndex lastIndex
Constraints
1 ≤ N ≤ 10^5
0 ≤ arr[i] ≤ 10^9
Must use Binary Search (O(log N))
Sample Input 1:
7
10 20 20 20 30 40 50
20
Sample Output 1:
13
Sample Input 2:
7
10 20 20 20 30 40 50
25
Sample Output 2:
-1 -1
[Link] POINTER PATTERN
Approach
1. Opposite Direction: Two pointers start from opposite ends of the array and move towards each
other.
2. Same Direction: Two pointers start from the same end of the array and move in the same
direction.
Problems
1. Two Sum (Easy): Given an array of integers and a target sum, find two elements that add up to the
target. Input array is sorted
2. Reverse String (Easy): Reverse a string using two pointers.
3. Container With Most Water (Medium): Given an array of heights, find the maximum area of water
that can be trapped between two lines.
4. 3Sum (Medium): Given an array of integers, find all triplets that add up to zero.
5. Remove Duplicates from Sorted Array (Easy)
6. Partition Array into Disjoint Intervals (Medium)