0% found this document useful (0 votes)
14 views28 pages

Module 3

The document outlines several coding problems related to arrays and matrices, including tasks such as inserting product IDs into a sorted list, counting occurrences of the second largest element, and checking for consecutive marks. Each problem includes input and output formats, constraints, and example scenarios to illustrate the requirements. Additionally, coding snippets are provided for each problem to guide implementation.

Uploaded by

devisreek48
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

Module 3

The document outlines several coding problems related to arrays and matrices, including tasks such as inserting product IDs into a sorted list, counting occurrences of the second largest element, and checking for consecutive marks. Each problem includes input and output formats, constraints, and example scenarios to illustrate the requirements. Additionally, coding snippets are provided for each problem to guide implementation.

Uploaded by

devisreek48
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays 1D - Inclass

1. Insertion of New Product IDs

In an e-commerce platform, the system manages a list of product IDs that are used to track each
product. The platform needs to efficiently insert new product IDs into a sorted list to maintain the
order. Rahul, a software engineer, has been tasked with creating a function to insert a new product
ID into the correct position in the sorted list of product IDs.

Rahul needs to ensure that the list is always sorted, and the product ID is inserted in the right
position without disturbing the existing order.

Given an array of product IDs (sorted in non-decreasing order) and a new product ID, your task is to
insert the new product ID into the array, keeping the list sorted.

Input Format

 The first line contains an integer n representing the number of existing product IDs.

 The second line contains n integers representing the product IDs in sorted order.

 The third line contains an integer newProductId, which represents the new product ID to be
inserted.

Constraints

 1 ≤ n ≤ 1000

 The product IDs are integers and are sorted in non-decreasing order.

 The new product ID to be inserted is an integer.

Output Format

 Output the new array with the product ID inserted at the correct position, maintaining the
sorted order.

Example 1

Sample Input 1

101 105 110 120 130

107

Sample Output 1
101 105 107 110 120 130

Explanation

The array is initially sorted as [101, 105, 110, 120, 130]. The new product ID 107 is inserted in the
correct position, between 105 and 110, resulting in the array [101, 105, 107, 110, 120, 130].

Coding

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */

int n;

cin >> n;

int arr[n+1];

for(int i=0;i<n;i++){

cin >> arr[i];

cin >> arr[n];

for(int i=n;i>0;i--){

if(arr[i]<arr[i-1]){

int temp=arr[i];

arr[i]=arr[i-1];

arr[i-1]=temp;

}
}

for(int i=0;i<=n;i++){

cout << arr[i] << " ";

return 0;

2. Second Occurrence

Charles is given an array A. He wants to find the count of occurrence of second largest element in the
array. Your task is to help him find and return an integer value representing the count of occurrence
of the second largest element in an array.

Note

If the array contains the same elements, then return 0

The array has only consecutive elements

Input Format

Input1: An integer N, representing length of array.

Input2: An integer array A

Output Format

Return an integer value representing the count of occurrence of the second largest element in an
array.

Example 1

Sample Input 1

Input1: 8

Input2: 1 2 3 4 4 5 5 5

Sample Output 1

2
Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n];

for(int i=0;i<n;i++){

cin >> arr[i];

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

if(arr[i]>arr[j]){

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

int second;

int count=0;

for(int i=n-1;i>0;i--){

if(arr[i]>arr[i-1]){

second=arr[i-1];

break;

for(int i=0;i<n;i++){
if(arr[i]==second){

count++;

cout << count;

3. Dinner Dishes

You are planning a dinner menu for an event, and you have a list of N dishes stored in array A, each
with a certain cost associated with it. You must select two such dishes, such that the sum of their
costs is maximum among all available pairs. Your task is to find and return an integer value
representing the sum of such available pair. Return 0 in case of no such pair available.

Input Format

Input1: An integer value N representing the number of dishes available.

Input2:An integer array A representing the cost of dishes

Output Format

Return an integer value representing sum of such pair available. In case, there are no such pair
present, return 0.

Example 1

Sample Input 1

Input1: 4

Input2: 1 10 5 15

Sample Output 1

25

Coding
#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n];

for(int i=0;i<n;i++){

cin >> arr[i];

int m1=0;

for(int i=0;i<n;i++){

if(arr[i]>m1){

m1=arr[i];

int m2=0;

for(int i=0;i<n;i++){

if((arr[i]>m2) && (arr[i]!=m1)){

m2=arr[i];

if(n<2){

cout << 0;

else{

if(m2==0){

cout << m1+m1;

}else{
cout << m1+m2;

4. Nick's check

Nick has been given a list of random numbers by his teacher.

These numbers are marks of several students of his class.

He is required to arranged the marks in increasing order and hence check whether the new
arrangement of marks are successive in nature or not.

You need to write a function such that it returns 1 if the complete arrangement consists of
consecutive marks, otherwise return 0.

Note

If two students have the same marks, then after arranging them in increasing order, they will not be
considered as consecutive.

Input Format

Input1: Integer N i.e., size of the array

Input2: Integer array for elements of the array

Output Format

Return 1 if all the numbers are consecutive after arrangement, otherwise return 0.

Example 1

Sample Input 1

Input1: 6

Input2: 3 7 2 5 4 6

Sample Output 1

1
Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n];

for(int i=0;i<n;i++){

cin >> arr[i];

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

if(arr[i]<arr[j]){

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

int flag=0;

int a=arr[0];

for(int i=0;i<n;i++){

if(a==arr[i]){

a++;

else{

flag=1;
break;

if(flag==0)

cout << 1;

else

cout << 0;

5. The lost Digit

You are working on a number sequence puzzle. You have an array A, that is supposed to contain all
the numbers from 1 to N, but you realize one number is missing. The array might have been shuffled,
so the numbers are not in order. Your task is to find and return an integer value representing the
missing number from the sequence.

Input Format

Input1: An integer value N representing the length of the sequence

Input2: An integer array A

Output Format

Return an integer value representing the missing number from the sequence.

Example 1

Sample Input 1

Input1: 5

Input2: 3 1 2 5

Sample Output 1

Explanation
Here, A = {3, 1, 2, 5} and N = 5. The sequence should contain the numbers from 1 to 5 but the
number 4 is missing from the array A. Hence, 4 is returned as the output.

Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n-1];

for(int i=0;i<n-1;i++){

cin >> arr[i];

int a=(n*(n+1))/2;

int sum=0;

for(int i=0;i<n-1;i++){

sum=sum+arr[i];

cout << a-sum;

6. Alice's Magical shoes!

Alice has a pair of magical shoes that allows her to climb 3 stairs at once. In the city, there are N
houses whose roofs Alice wants to reach. The number of stairs to the roof of each house is given in
an array A. Alice can reach the roofs of only those houses where the number of stairs is a multiple of
3. Your task is to find and return an integer value representing the count of the number of houses
whose roof Alice can climb up to.

Input Format

Input1: An integer value N representing the number of houses.


Input2: An integer array A representing the number of stairs in each house.

Output Format

Return an integer value representing the count of the number of houses whose roofs Alice can climb
up to.

Example 1

Sample Input 1

Input1: 4

Input2: 12 21 3 4

Sample Output 1

Explanation

Here, the given array is (12,21,3,4). There is only one house, (the 4th), whose roof Alice cannot go up
to as it is not a multiple of 3. Therefore, 3 is returned as the output.

Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n];

for(int i=0;i<n;i++){

cin >> arr[i];

}
int count=0;

for(int i=0;i<n;i++){

if(arr[i]%3==0){

count++;

cout << count;

7. Merging Attendance Records

In a school, teachers maintain attendance records for each class using matrices. Each row of the
matrix represents a different class, while each column represents a different day. The value in each
cell indicates the number of students present on that day. Occasionally, the school administration
needs to combine attendance records from two different weeks to get an overall view of student
attendance.

You have been tasked to create a program that reads two attendance matrices, each representing the
attendance records for a different week, and then adds them together to create a final matrix
representing the total attendance for those two weeks.

Problem Statement

Write a program that takes two matrices of the same dimensions (number of classes and days) as
input and computes the element-wise sum of these matrices. The output should reflect the total
number of students present for each class on each day over the two weeks.

Input Format

1. The first line contains two integers, classes and days, representing the dimensions of the
matrices.

2. The next classes lines contain space-separated integers representing the attendance records
for the first week.

3. The following classes lines contain space-separated integers representing the attendance
records for the second week.

Constraints

 Both matrices will have the same dimensions (i.e., both matrices are classes x days).

 All integers are non-negative.


Output Format

Print the resulting attendance matrix with each row on a new line, where each element in a row is
separated by a space.

Sample Input

23

123

456

789

111

Sample Output

8 10 12

567

Explanation

In the sample input, the first matrix represents attendance records for the first week and the second
matrix represents records for the second week. The program sums the attendance for each class on
each day, resulting in a new matrix that shows the combined attendance over the two weeks.

Coding

#include <iostream>

using namespace std;

int main(){

int row,col;

cin >> row >>col;

int arr1[row][col];

for(int i=0;i<row;i++){

for(int j=0;j<col;j++){
cin >> arr1[i][j];

int arr2[row][col];

for(int i=0;i<row;i++){

for(int j=0;j<col;j++){

cin >> arr2[i][j];

for(int i=0;i<row;i++){

for(int j=0;j<col;j++){

cout << arr1[i][j]+arr2[i][j] << " ";

8. Irene's Audience Arrangement Checker

Irene, one of the event organizers, has arranged the audience in a matrix format where boys are
represented by 1s and girls by 0s. To win a prize, she must ensure that all girls (0s) are positioned
below the main diagonal of the matrix. If she successfully arranges the audience according to this
condition, she will be rewarded; otherwise, she will not.

Your task is to help the judges determine if Irene's arrangement is valid by checking if the matrix is an
upper triangular matrix.

Problem Statement

Write a program that checks whether a given matrix is an upper triangular matrix. A matrix is
considered upper triangular if all the elements below the main diagonal are zero.

Input Format
1. The first line contains an integer n, representing the number of rows and columns of the
square matrix.

2. The next n lines contain space-separated integers representing the matrix.

Constraints

 The matrix is a square matrix (i.e., the number of rows equals the number of columns).

Output Format

Print "Upper triangular matrix" if the condition is satisfied; otherwise, print "Not an Upper triangular
matrix"

Example 1

Sample Input 1

123

012

001

Sample Output 1

Upper triangular matrix

Explanation

In this input, all elements below the diagonal are 0, hence it is an upper triangular matrix.

Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;
int arr[n][n];

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cin >> arr[i][j];

for(int i=1;i<n;i++){

for(int j=0;j<i;j++){

if(arr[i][j]!=0){

cout << "Not an Upper triangular matrix";

return 0;

cout << "Upper triangular matrix";

9. Robotic Warehouse Navigation - Snake Pattern Traversal

In a modern warehouse, a robot is tasked with scanning items in storage shelves arranged in a grid-
like format. The robot moves in a snake-like pattern to ensure that all shelves are scanned efficiently.
Specifically, the robot starts at the first row and scans from left to right, then moves to the next row
and scans from right to left, and so on.

Write a program that outputs the order in which the robot scans the items in a given matrix in a
snake pattern.

Input Format

 The first line contains two integers, m (number of rows) and n (number of columns).

 The next m lines contain n integers each, representing the item IDs on the storage shelves.

Output Format

 Print the item IDs in the order the robot scans them in a single line, separated by spaces.
Constraints

 1≤m,n≤1001 \leq m, n \leq 1001≤m,n≤100

 Item IDs are integers ranging from 111 to 100010001000.

Sample Input

34

1234

5678

9 10 11 12

Sample Output

1 2 3 4 8 7 6 5 9 10 11 12

Explanation

Input Matrix (Item IDs on Shelves)

1234

5678

9 10 11 12

 The robot starts at the first row and moves left to right: 1 2 3 4

 It then moves to the second row and scans from right to left: 8 7 6 5

 Finally, it moves to the third row and scans from left to right: 9 10 11 12

Output

The order of item IDs in the snake pattern is 1 2 3 4 8 7 6 5 9 10 11 12.

Coding

#include <iostream>

using namespace std;

int main(){
int row,col;

cin >> row >>col;

int arr1[row][col];

for(int i=0;i<row;i++){

for(int j=0;j<col;j++){

cin >> arr1[i][j];

for(int i=0;i<row;i++){

if(i%2==0){

for(int j=0;j<col;j++){

cout << arr1[i][j] << " ";

else{

for(int j=col-1;j>=0;j--){

cout << arr1[i][j] << " ";

10. Identifying Maximum Defect Levels in Production Batches

In a manufacturing unit, a quality control team is tasked with monitoring defect levels across multiple
production batches. The defect levels for each item in a batch are recorded in a matrix format, where
each row represents a production batch and each column represents the defect level of an item
within that batch.

The quality control team needs to find the maximum defect level in each production batch to identify
the most defective item in each batch. Write a program to help them identify the maximum defect
levels for all production batches.
Input Format

 The first line contains two integers, m (number of production batches) and n (number of
items in each batch).

 The next m lines contain n integers each, representing the defect levels of items in each
batch.

Output Format

 Output m integers, each representing the maximum defect level in the corresponding
production batch.

Sample Input

34

5386

2947

1638

Sample Output

Explanation

Input Matrix (Defect Levels)

Copy code

5386

2947

1638

Batch 1: The maximum defect level is 8.

Batch 2: The maximum defect level is 9.

Batch 3: The maximum defect level is 8.

Output
The program outputs the maximum defect level for each batch, which helps the quality control team
quickly assess the severity of defects in each batch.

Coding

#include <iostream>

#include <climits>

using namespace std;

int main(){

int n,m;

cin >> n >> m;

int arr[n][m];

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

cin >> arr[i][j];

for(int i=0;i<n;i++){

int max=INT_MIN;

for(int j=0;j<m;j++){

if(arr[i][j]>max){

max=arr[i][j];

cout << max << endl;

Arrays 1D and 2D – Post Class


1. Power Grid Monitoring - Computing Diagonal Load Balances

In a power distribution center, the load levels are monitored and recorded in a grid-like structure to
analyze how power is being distributed across different regions. The grid is represented as an n×n
matrix, where each element represents the load level at a specific point in the grid.

The engineers are interested in efficiently calculating the sum of load levels along the two main
diagonals:

 Primary diagonal: From the top-left to the bottom-right.

 Secondary diagonal: From the top-right to the bottom-left.

Write a program to compute the sum of the load levels along both diagonals.

Input Format

 The first line contains a single integer, nnn (the size of the square matrix).

 The next nnn lines contain nnn integers each, representing the load levels in the grid.

Output Format

 Print two integers: the sum of the primary diagonal and the sum of the secondary diagonal,
separated by a space.

Sample Input

523

864

197

Sample Output

18 10

Explanation

Input Matrix (Load Levels):

523

864

197
Primary Diagonal: 5+6+7=18

Secondary Diagonal: 3+6+1=10

Output The program outputs 18 10, which represents the sum of the primary and secondary
diagonals, respectively.

Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n][n];

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cin >> arr[i][j];

int diagnol1,diagnol2;

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

if(i==j){

diagnol1+=arr[i][j];

if(i+j==n-1){

diagnol2+=arr[i][j];

}
}

cout << diagnol1 << " " << diagnol2;

2. Counting Sorted Production Lines

In a manufacturing plant, items produced in assembly lines are arranged in rows for quality control.
Each row represents a production line, and each item in the row has a quality score. The plant's
quality assurance team needs to identify how many production lines have items sorted in non-
decreasing order based on their quality scores.

Write a program that counts the number of rows in the matrix that are sorted in non-decreasing
order.

Input Format

 The first line contains two integers, m (number of rows) and n (number of columns).

 The next m lines contain n integers each, representing the quality scores of items in each
production line.

Output Format

 Print the total count of rows that are sorted in non-decreasing order.

Constraints

 1≤m,n≤100

 The quality scores are integers ranging from −1000 to 1000.

Sample Input

45

10 20 30 40 50

15 10 5 0 -5

55678

9 11 10 13 14

Sample Output

2
Explanation

Input Matrix (Quality Scores of Items in Production Lines)

10 20 30 40 50 -> Sorted

15 10 5 0 -5 -> Not sorted

55678 -> Sorted

9 11 10 13 14 -> Not sorted

The 1st and 3rd rows are sorted in non-decreasing order, so the output is 2.

Output

The program counts the rows where quality scores are sorted in non-decreasing order.

Coding

#include <iostream>

using namespace std;

int main(){

int n,m;

cin >> n >> m;

int arr[n][m];

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

cin >> arr[i][j];

int count=0;

for(int i=0;i<n;i++){

int flag=0;

for(int j=0;j<m-1;j++){
if(arr[i][j]>arr[i][j+1]){

flag=1;

if(flag==0){

count++;

cout << count;

3. Unsold Products

In a coding project you are developing a feature for a data visualization tool that process large
datasets of products stored in array A. One dataset contains sales figures for various products where
a sale value of 0 indicates that the product was not sold during a specific period. Your task is to find
and return an integer array representing the dataset representing all the unsold products at the end.

Input Format

Input1:An integer N containing the array size

Input2: An integer array A containing sales number of a particular product.

Output Format

Return an integer array representing the dataset representing all the unsold products at the end.

Sample Input 1

Input1:7

Input2:5 2 0 8 0 2 1

Samplr Output 1

5282100
Explanation

Here, A = {5, 2, 0, 8, 0, 2, 1}. The dataset contains zeros at position 2 and 4. After shifting all zeros to
the end, the non-zero elements {5, 2, 8, 2, 1} retain the original order. And the last two positions will
be occupied with zeros.

Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin>>n;

int arr[n];

for(int i=0;i<n;i++){

cin >> arr[i];

for(int i=0;i<n;i++){

for(int j=i;j<n;j++){

if(arr[i]==0){

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

for(int i=0;i<n;i++){

cout << arr[i] << " ";

}
4. Finding the Median of Positive Product IDs

You are managing an inventory system, and you have an array of product IDs. Some of the IDs may be
negative, which indicates a faulty product that should be ignored. You need to determine the product
ID at the middle of the list, but only considering the positive product IDs. If there are two mid indices
after ignoring the negative IDs, you are required to return the element at the smaller index.

Write a function to find and return the product ID at the mid index from the list of positive product
IDs, ignoring all negative IDs. If there are two mid indices, return the product ID at the smaller index.

Input Format

 The first line contains an integer n, representing the number of product IDs.

 The second line contains n integers, representing the product IDs. These can include both
positive and negative numbers.

Output Format

 Print the product ID located at the mid-index of the array after ignoring all negative product
IDs.

Assumptions

 The array will have at least one positive number.

 If there are two mid indices after ignoring negative numbers, return the one at the smaller
index.

Example 1

Sample Input 1

11 23 -3 3 -5 -32

Sample Output 1

23

Explanation

After removing negative numbers from the array, the positive product IDs are {11, 23, 3}. The middle
product ID is 23, which is the mid index of the filtered array.
Coding

#include <iostream>

using namespace std;

int main(){

int n;

cin >> n;

int arr[n];

for(int i=0;i<n;i++){

cin >> arr[i];

int arr1[n];

int a=0,count=0;

for(int i=0;i<n;i++){

if(arr[i]>0){

arr1[a++]=arr[i];

count++;

if(count==0){

cout << -1;

else{

cout << arr1[count/2];

You might also like