Array
1. Problem Statement: Attendance Analyzer
Description:
Your college uses a system to track student attendance each day. You're
given an 1 0
array of integers represents present represents absent. Your
where and task
is to count the number of present and absent days in a month.
Sample Inputs & Outputs:
Input: int[] attendance = 1, 0, 1, 1,
0
Output: Present = 3, Absent
=2 1, Input:
1, 1, 1
Output: Present = 4, Absent = 0
Input: 0, 0, 0
Output: Present = 0, Absent = 3
Corner Test Cases:
Empty Array {} * Output: Present = 0, Absent = 0
→ Single 1 * Output: Present = 1, Absent = 0
Element:
2. Problem Statement: Temperature Logger
Description:
A weather station stores daily high temperatures in an array. Write a
program to find the highest and lowest temperature of the week.
Sample Inputs & Outputs:
Input: 32, 35, 31, 30, 29, 40,
38
Output: Max = 40, Min =
29 Input:
25, 25, 25, 25
Output: Max = 25, Min = 25
Array 1
s
Corner Test Cases:
Single day:20 * Output: Max = 20, Min = 20
Negative temps:
-5, -10, -3 * Output: Max = -3, Min = -10
3. Problem Statement: Exam Result Tracker
Description:
Each element in the array represents a student's exam score out of
100. Write a program to count how many students passed (score 35)
and how many failed.
Sample Inputs & Outputs:
Input: 45, 32, 90, 29,
76
Output: Passed = 3, Failed
=2 Input:
34, 34, 34
Output: Passed = 0, Failed = 3
Corner Test Cases:
All 90, 80, 75
passed: 10, 0, 34
All failed:
Exactly 35:35, 35 * Passed = 2
4. Problem Statement: Classroom Seating Problem
Description:
In a classroom, seat numbers are stored in an array. Due to a manual
error, some seat numbers are repeated. Your task is to find and print all
the duplicate seat
numbers.
Sample Inputs & Outputs:
Input: 10, 20, 30, 20, 40,
10
Output: Duplicate seats:
10, 20 1, Input:
2, 3, 4
Output: No duplicates
Array 2
s
Corner Test
Cases:
* Output: Duplicate seat: 5
All elements
same:
5, 5, 5, 5
Empty array: * Output: No duplicates
{}
5. Problem Statement: Power Consumption Logger
Description:
An electric meter stores units consumed each day in an array. Write a
program to calculate the total units consumed and the average daily
consumption.
Sample Inputs & Outputs:
Input: 10, 20, 30, 40
Output: Total = 100, Average =
25.0 0, Input:
0, 0
Output: Total = 0, Average = 0.0
Corner Test Cases:
Single day: 15 * Total = 15, Average = 15.0
Mixed values:5, 10, 15, 0 * Total = 30, Average = 7.5
Problem 6: Identifying Empty Seats
Problem Statement:
A cinema hall has a row of 10 seats represented by an means the
array, where 1
seat is occupied 0 Corner Cases:
and seats are
empty.
Sample Input &
Output:
Input: 1, 0, 0, 1, 1, 0, 1, 0, 0,
1
Input: 1, 1, 1, 1, 1, 1, 1, 1, 1,
1
Input: 0, 0, 0, 0, 0, 0, 0, 0, 0,
0
Array 3
s
means itʼs empty.
Write a program to
count how many
* Output: 5
* Output: 0
* Output: 10
Array 4
s
All seats empty
All seats occupied
First and last seats empty only
Problem 7: Bakery Orders Tracker
Problem Statement:
A bakery receives daily orders represented as an array of quantities.
Your task is to find on how many days the orders exceeded 50.
Sample Input & Output:
Input: 45, 67, 30, 90, * Output: 2
10
Input: 10, 20, 30, 40,
* Output: 0
50
Input: 55, 60, 75,
* Output: 4
100
Corner Cases:
No orders over
50 All orders
over 50
Only one day with 51
Problem 8: Hospital Temperature Logger
Problem Statement:
Each day a hospital logs the temperature of patients. If temperature 100,
it's considered high fever. Given an array of temperatures, count how
many patients have high fever.
Sample Input & Output:
Input: 98, 101, 102, 99, * Output:2
97
Input: 96, 97, 98
* Output: 0
Input: 101, 103, 104
* Output: 3
Corner
Cases:
Array 5
s
No high fevers
All patients with high fever
One patient with exactly 100 (should not count)
Problem 9: Water Tank Filling System
Problem Statement:
An apartment has water tanks in 7 buildings. The water levels are stored in
an array. If any tank is empty (level = 0), display its position(s).
Sample Input & Output:
Input: 0, 10, 20, 0, 15, 5, * Output:0 3 6
0
Input: 10, 20, 30, 40, 50, 60,
* Output:No empty
70
tanks
Input: 0, 0, 0, 0, 0, 0,
* Output: 0 1 2 3 4 5
0
6
Corner Cases:
All tanks
empty No
tank empty
Last tank only is empty
Problem 10: Classroom Attendance System
Problem Statement:
A classroom has a list of students present (1) and absent (0). The teacher
wants to calculate attendance percentage.
Sample Input & Output:
Input: 1, 1, 0, 1, 0 * Output: 60%
Input: 0, 0, 0, 0, 0
* Output: 0%
Input: 1, 1, 1, 1, 1
* Output: 100%
Corner
Cases:
All students present
Array 6
s
All students absent
One student present
Problem 11: Customer Order Management System -
Swap First and Last Order
Problem Statement:
In a customer order management system, you have an array of order
IDs (integers). Sometimes, due to system bugs, the first and last order
IDs are
swapped. Write a program that fixes this issue by swapping the first and
last elements of the order array.
Sample Input & Output:
Input: 101, 202, 303, 404, * Output: 505, 202, 303, 404,
505 101
Input: 1001, 2001, 3001
* Output: 3001, 2001,
1001
Input: 101
* Output: 101
Corner
Cases:
Only one order in the array
All orders are the
same
Empty array
Problem 12: Product Rating System - Remove All Invalid
Ratings
Problem Statement:
In a product rating system, each product has a rating array. You need to
remove any invalid ratings (negative values) to keep only valid ratings.
Write a program that removes all invalid ratings from the array.
Sample Input & Output:
Input : Input:
Input
Array 7
s
* O
u
t
p
u
t: 4, -1, 5, -2, 3 4, 5, 3
* Ou 1, 1, 1, 1 1, 1, 1, 1
tp
ut:
* 0, -3, 2, 4 0, 2, 4
Ou
tp
ut:
Array 8
s
Corner Cases:
No invalid ratings in the
array All ratings are
invalid
Ratings with zero values
Problem 13: School Grading System - Find the Second
Highest Grade
Problem Statement:
In a school grading system, each student receives a grade
represented by a number. Write a program to find the second highest
grade in the list. If there is no second highest, return a message "No
second highest grade."
Sample Input & Output:
Input: 85, 90, 92, 88, 94 * Output: 92
Input: 75, 75, 75, 75
* Output: No second highest
grade
Input: 88, 95, 80, 92
* Output: 92
Corner
Cases:
All students have the same
grade Only one student in
the system
Only two students with the same grade
Problem 14: Employee Shifts - Rotate Employee Shifts by
K Positions
Problem Statement:
In a company, the shift timings of employees are represented as an array.
Due to k
scheduling changes, you need to rotate the employee shift position
timings by to the right. Write a program to implement this. s
Sample Input & Output:
Array 9
s 2 12, 1, 9, 10,
11
Input 9, 10, 11, 12, 1,
k * Output:
: =
Array 1
s 0
Input: ,k
8, 9, 10 = 1 * Output: 10, 8, 9
Input: 1, 2, 3 , k = 3 * Output: 1, 2, 3
Corner
Cases:
is larger than the array
k
size is 0
k
Array with all identical shift values
Problem 15: Student Attendance - Find Common Students
in Two Classes
Problem Statement:
Two classes in a school have a list of students (represented by their IDs)
who attended a special event. Write a program to find the common
students who attended the event in both classes.
Sample Input & Output:
Input: 101, 102, 103, 104, 104, 105, 106, * Output: 104
107
Input: 201, 202, 203
, 203, 204, 205 * Output: 203
Input: 301, 302
, 401, 402 * Output:No common
students
Corner
Cases:
No common students between the two
classes All students in both classes are
the same
One or both arrays are empty
Problem 16: Job Scheduling - Find the Maximum
Number of Jobs Completed
Problem Statement:
In a job scheduling system, each job has a start and end time. Write a
program
that determines the maximum number of jobs that can be completed
without any overlap. The jobs are represented as an array of pairs
Array 1
s 1
(start time, end time).
Array 1
s 2
Sample Input &
Output:
, * Output:3
Input: (1, 4), (2, 6), (5, 8), (7,
10)
(Jobs that can be completed: (1, 4), (5, 8), (7, 10))
Input: (1, 3), (2, 5), (4, 7), (6, , * Output:3
8)
Input: (1, 3), (3, 6), (7, 10)
, * Output:3
Corner Cases:
All jobs
overlap. No
jobs overlap.
Only one job.
Problem 17: Array Rotation - Rotate the Array by N
Positions
Problem Statement:
Write a program that rotates an array of integers to the right by N
positions. Do this without using extra space.
Sample Input & Output:
Input: 1, 2, 3, 4, 5, N 2 , * Output:4, 5, 1, 2, 3
Input: 10, 20, 30, 40, N 1
, * Output:40, 10, 20, 30
Input: 1, 1, 1, 1, N 3
, * Output:1, 1, 1, 1
Corner
Cases:
N is greater than the array
length. Array with all same
elements.
Array with one element.
Problem 18: Find Missing Numbers
Problem Statement:
Given an array with numbers in the range of 1 to N, some numbers are
Array 1
s 3
missing. Write a program to find the missing numbers.
Array 1
s 4
Sample Input & Output:
Input: 1, 2, 4, 6, 7, , * Output:3, 5
8
Input: 1, 3, 5
, * Output:2, 4
Input: 2, 4, 5, 1
, * Output:3
Corner
Cases:
No missing numbers.
All numbers are missing except
for one. Array is unordered.
Problem 19: Sort Array of 0s, 1s, and 2s (Dutch National
Flag Problem)
Problem Statement:
Given an array containing only 0's, 1's, and 2's, sort the array in linear
time and constant space.
Sample Input & Output:
Input: 2, 0, 1, 2, 1, 0 , * Output:0, 0, 1, 1, 2, 2
Input: 0, 0, 1, 2, 1
, * Output:0, 0, 1, 1, 2
Input: 1, 2, 0
, * Output:0, 1, 2
Corner
Cases:
Array with only one
element. All
elements are the same.
Array with a single type of element (e.g., only 1's).
Problem 20: Longest Consecutive Sequence
Problem Statement:
Given an unsorted array of integers, find the length of the longest
consecutive
sequence of numbers. For example, if the array 100,is
4, 200, 1, 3, , the longest
2
Array 1
s 0
consecutive sequence1,
is2, 3, 4 , and the output should be
4 .
Array 1
s 0
Sample Input & Output:
Input: 100, 4, 200, 1, 3, , * Output:4
2
Input 9, 1, 4, 7, 3, ,* (sequence: 1, 2, 3 )
: 2
Output: 3
4 1, 2, 3, 4
Input: 1, 9, 3, 10, 4, 20, ,* (sequence: )
2
Corner Output:
Cases:
All elements are the
same. The array is
empty.
No consecutive numbers in the array.
Problem 21: Maximum Product Subarray
Problem Statement:
Given an integer array, find the contiguous subarray (containing at least
one number) which has the largest product. Return that product.
Sample Input & Output:
Input ,* 6 (Subarra 2, 3 has the maximum
2, 3, -2, 4 Output:
: y 0 product) has the
Input -2, 0, -1 ,* 0
Output: (Subarray
48 maximum
4, -1, 2, 1 product)
Input: 2, -3, 4, -1, 2, ,* (Subarray )
1
Output:
Corner
Cases:
Array with all negative
elements. Array with only
one element.
Array with a mix of positive and negative numbers.
Problem 22: Subarray with Sum Equal to a Given Value
Array 11
s
Problem Statement:
Write a program to find all subarrays whose sum equals a given target
sum. Return the subarrays.
Sample Input & Output:
Array 12
s
Input , target sum 12 , * Output:3, 7, 7, 5
1, 2, 3, 7, 5 =
: , * Output:1, 1, 1
1, 1, 1, 1, 1 , target sum 3
Input 10, 2, -2, -20,= 10 2, -2, -20, -2, -20, 10
10
:
Input: , target sum , * Output:
=
Corner
Cases:
Array with no matching subarray.
Array with all elements equal to target sum.
Target sum larger than the sum of all elements.
Problem 23: Find All Pairs with Given Sum
Problem Statement:
Write a program that takes an array and a target sum. It finds and prints
all unique pairs of numbers that add up to the target sum.
Sample Input & Output:
Input , target sum 7 , * Output:(1, 6), (2, 5), (3, 4)
1, 3, 2, 4, 5, 6 =
: , * Output:(10, 7)
Input 10, 15, 3, 7 , target sum 17
= 2 (1, 1)
:
Input: 1, 1, 1, 1, 1 , target sum , * Output:
=
Corner
Cases:
Array with negative
numbers. No pairs found.
Pairs where multiple elements contribute to the same sum.
Problem 24: Rearrange Array Alternating Positive and
Negative
Problem Statement:
Array 13
s
Given an array, rearrange the array so that positive and negative
numbers are
arranged alternatively. The order of positive and negative numbers
should remain as in the original array.
Sample Input & Output:
Array 14
s
Input: 1, -1, 3, -2, 5, -4, * Output:1, -1, 3, -2, 5, -4
Input: 1, 2, 3, -4, -1, - , * Output:1, -4, 2, -1, 3, -
2 2
Input: 1, 2, -3, -4, 5 , * Output:1, -3, 2, -4, 5
Corner
Cases:
Array with only positive or only negative
elements. Array with no negative or no
positive elements.
Array with one element.
Problem 25: Maximum Length of Subarray with Positive
Product
Problem Statement:
Given an array of integers, find the maximum length of a subarray with a
positive product.
Sample Input & Output:
Input: 1, -2, -3, 4 , * Output:4 (The 1, -2, -3, 4 has the maximum
product subarray
)
,*
Input: 0, 1, -2, -3, -4 Output: 4
Input: -1, -2, -3, 0, 1,*
Output:
Corner 2
Cases:
Array with zeroes only.
Array with all negative numbers.
Array with a mix of positive and negative numbers.
[Link] Statement: Sorting Exam Scores (Bubble
Sort)
A teacher has a list of students' exam scores, and she wants to sort them
in
ascending order using the simplest sorting algorithm.
Array 15
s
Write a program that sorts the scores in ascending order using the
Bubble Sort
technique.
Array 16
s
Sample Input:
int[] scores = 95, 85, 78, 92, 88;
Sample Output:
78, 85, 88, 92, 95
Explanation:
Bubble sort compares each pair of adjacent elements and swaps
them if they are in the wrong order.
27. Problem Statement: Sorting Product Prices
(Selection Sort)
A retailer has a list of prices for products, and they want to sort them in
descending order using the Selection Sort algorithm.
Write a program to sort the product prices in descending order.
Sample Input:
int[] prices = 30, 10, 20, 40, 50;
Sample Output:
50, 40, 30, 20, 10
Explanation:
Selection sort finds the largest element and places it at the end of
the array, then repeats the process for the remaining array.
[Link] Statement: Sorting Names Alphabetically
(Insertion Sort)
Array 17
s
You have a list of names of employees, and you want to sort them in
lexicographical order (alphabetically) using the Insertion Sort
algorithm. Write a program to sort the names in lexicographical
order.
Sample Input:
String[] names = {"John", "Alice", "Bob", "Charlie"};
Sample Output:
Alice, Bob, Charlie, John
Explanation:
Insertion sort builds the final sorted array one element at a
time, moving elements into the correct position.
[Link] Statement: Find the Element (Linear Search)
In a store, customers are searching for a product in a linear manner through
a list of product IDs. You need to find the index of a product in a list.
Write a program to implement Linear Search to find the index of a given
product ID.
Sample Input:
int[] productIDs = 101, 102, 103, 104,
105; int target 103;
Sample Output:
Explanation:
Array 15
s
Linear search goes through each element one by one and compares it
with the target.
[Link] Statement: Sorting a List of Student
Grades (Quick Sort)
You are a teacher and have a list of student grades. You want to sort the
grades in ascending order using the Quick Sort algorithm to prepare
for report generation.
Write a program to sort the grades in ascending order.
Sample Input:
int[] grades = 75, 82, 90, 67, 88, 56;
Sample Output:
56, 67, 75, 82, 88, 90
Explanation:
Quick Sort is a divide-and-conquer algorithm that works by
selecting a "pivot" element and partitioning the array into two sub-
arrays based on the pivot. The process is recursively repeated for the
sub-arrays.
31. Problem Statement: Find the Sum of All Elements in
a Matrix
Scenario:
You are managing the accounting records for a large company. All
financial transactions are stored in a matrix, where each row represents
transactions for a particular day and each column represents different
departments. Your task is to calculate the total sum of all the
transactions across all days and departments.
Sample Input:
int[][] transactions = {
200, 400, 600,
Array 16
s
150, 300, 450,
100, 250, 350
}
;
Sample Output:
Total sum of transactions: 3800
[Link] Statement: Find the Maximum Element in a
Matrix
Scenario:
You are developing a game that tracks player scores across multiple
rounds. The scores for each player in each round are stored in a matrix.
You need to find the highest score achieved by any player across all
rounds.
Sample Input:
int[][] scores = {
10, 20, 30,
50, 40, 10,
25, 15, 35
};
Sample Output:
Maximum score: 50
[Link] Statement: Transpose a Matrix
Scenario:
You work in a data analytics firm, and a client provides a matrix
where each row represents sales data for different regions, and each
column represents a product. The client requests you to transpose the
matrix so that each row will represent a product, and each column will
represent sales data for a region.
Array 17
s
Sample Input:
int[][] salesData =
{
100, 200, 300,
150, 250, 350,
200, 300, 400
};
Sample Output:
Transposed Matrix:
100 150 200
200 250 300
300 350 400
[Link] Statement: Check if a Matrix is a Square
Matrix
Scenario:
You are working on an application that validates the input matrices for a
mathematical computation. One of the conditions is that the matrix
should be a square matrix (number of rows equals the number of
columns). Your task is to check if the provided matrix is a square matrix.
Sample Input:
int[][] matrix1
1, 2, 3,
4, 5, 6,
7, 8, 9
};
int[][] matrix2
1, 2,
3, 4
};
Array 1
s 8
Sample Output:
Matrix1 is a square matrix.
Matrix2 is a square
matrix.
[Link] Statement: Multiply Two Matrices
Scenario:
You are working for a logistics company, and you need to calculate the
total
distance traveled across different routes for each vehicle. The data is
stored in two matrices: one matrix contains distances for various
routes, and the other
contains the speed for each route. Your task is to multiply the two
matrices to get the total distance traveled for each vehicle.
Sample Input:
int[][] distances = {
10, 20, 30,
5, 10, 15
};
int[][] speeds = {
2, 3, 4,
3, 4, 5
};
Sample Output:
Resulting Matrix (Total
Distance): 50 70 90
30 40 50
[Link] Statement: Find the Trace of a Matrix
Scenario:
Array 1
s 9
You are working for a finance company where matrices are used to
represent financial records. The trace of a matrix is the sum of the
diagonal elements (from top left to bottom right). Your task is to
compute the trace of the matrix provided by your manager.
Sample Input:
int[][] financialData = {
100, 200, 300,
150, 250, 350,
200, 300, 400
};
Sample Output:
Trace of the matrix: 750
These questions should help your students practice matrix-related
problems while understanding the real-world context in which they
might apply. Let me know if
you'd like any further modifications!
[Link] Statement: Find All Duplicates in
a List of Participants' IDs
Scenario:
You are managing a large conference, and each participant has a unique
ID.
However, some participants mistakenly registered twice using the same
ID. After gathering all the registration details, you need to identify the
duplicate participant IDs to ensure no one is assigned multiple seats.
Sample Input:
int[] participantIds = 101, 102, 105, 102, 107, 105, 108;
Sample Output:
Array 2
s 0
Duplicate participant IDs: 102, 105
37. Problem Statement: Count the Number of Duplicate
Numbers in a List
Scenario:
In a data analytics company, you have a list of transaction IDs made by
customers during an online sale. Some customers placed multiple orders,
causing duplicate transaction IDs. Your manager asks you to find out
how many times each transaction ID is duplicated in the list, as this
will help them spot repeated
purchases.
Sample Input:
int[] transactionIds = 999, 1023, 1504, 999, 1504, 2456, 999;
Sample Output:
Duplicate transaction IDs and their
count: 999 * 3 times
1504 * 2 times
[Link] Statement: Remove Duplicate Elements
from a List of Item Codes
Scenario:
You're managing the inventory of a retail store, and the new stock arrival
list has some duplicate item codes due to human error. Your task is to
remove the
duplicate item codes so that the inventory system shows each item
code only once. This will help avoid confusion during stock management.
Sample Input:
Array 21
s
int[] itemCodes = 212, 342, 212, 456, 789, 342;
Sample Output:
Unique item codes: 212, 342, 456, 789
[Link] Statement: Find the First Duplicate Number
in a List
Scenario:
As a customer support agent in an e-commerce company, you are
handling
complaints about customers who ordered the same product
multiple times by mistake. To solve this problem, you are given a list
of product IDs and need to
identify the first product ID that was ordered twice. This will help you
immediately address the issue with the customer.
Sample Input:
int[] productIds = 1001, 2034, 3055, 1001, 4599, 6722;
Sample Output:
The first duplicated product ID is 1001
40. Problem Statement: Find All Elements That
Appear More Than Twice
Scenario:
On a social media platform, users can post comments on multiple posts.
Your task is to track users who have commented on more than two
posts. After collecting a list of user activity IDs, you need to find out which
activity IDs are duplicated more than twice to ensure that you flag
users who may have over-engaged.
Array 22
s
Sample Input:
Array 23
s
int[] activityIds = 3, 5, 3, 6, 7, 5, 3, 6, 9;
Sample Output:
Activity IDs that appear more than twice: 3
Array 24
s