1D Array Problems
1. Reverse elements of a 1D array in-place without extra space
Input: Array A of size N
Output: A reversed in-place
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. Initialize i=1 , j=N
5. Repeat while i< j
o Swap A [i]and A [ j]
o i=i+1
o j= j−1
6. End repeat
7. Print array A
8. Stop
2. Find the largest element in a 1D array
Input: Array A of size N
Output: Largest element max
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. Initialize max= A [Link]
5. For i=2to N
o If A [i]>max then max= A[i]
6. End for
7. Print max
8. Stop
3. Find the smallest element in a 1D array
Input: Array A of size N
Output: Smallest element min
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. Initialize min=A [Link]
5. For i=2to N
o If A [i]<min then min=A [i]
6. End for
7. Print min
8. Stop
4. Calculate sum of all elements in a 1D array
Input: Array A of size N
Output: Sum of elements ∑ ¿
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. Initialize ∑ ¿ 0
5. For i=1to N
o ∑ ¿ ∑ + A [i]
6. End for
7. Print ∑ ¿
8. Stop
5. Compute the average of all elements in a 1D array
Input: Array A of size N
Output: Average avg
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. Initialize ∑ ¿ 0
5. For i=1to N
o ∑ ¿ ∑ + A [i]
6. End for
7. avg=∑ ¿ N
8. Print avg
9. Stop
6. Rotate a 1D array to the left by k positions
Input: Array A of size N , integer k
Output: Array A rotated left by k
Algorithm:
1. Start
2. Input N and k
3. Input array A [1. . N ]
4. Set k =k mod N
5. Create temporary array temp [1. . k ]
6. For i=1to k
o temp [i]=A [i]
7. For i=k + 1to N
o A [i−k ]= A [i]
8. For i=1to k
o A [N −k +i]=temp [i]
9. Print A
10. Stop
7. Rotate a 1D array to the right by k positions
Input: Array A of size N , integer k
Output: Array A rotated right by k
Algorithm:
1. Start
2. Input N and k
3. Input array A [1. . N ]
4. Set k =k mod N
5. Create temporary array temp [1. . k ]
6. For i=0 to k −1
o temp [k−i]= A [N −i]
7. For i=N −k down to 1
o A [i+k ]= A[i]
8. For i=1to k
o A [i]=temp [i]
9. Print A
10. Stop
8. Remove duplicates from a sorted 1D array in-place
Input: Sorted array A of size N
Output: Array A with duplicates removed, new size newN
Algorithm:
1. Start
2. Input N
3. Input sorted array A [1. . N ]
4. Initialize newN =1
5. For i=2to N
o If A [i]≠ A [newN ]
newN =newN +1
A [newN ]= A[i]
6. End for
7. Print array A [1. . newN ]
8. Stop
9. Find the second largest element in a 1D array
Input: Array A of size N
Output: Second largest element
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. Initialize max 1=−∞ , max 2=−∞
5. For i=1to N
o If A [i]>max 1then
max 2=max 1
max 1= A [i]
o Else if A [i]>max 2 and A [i]≠ max 1then
max 2=A [i]
6. End for
7. Print max 2
8. Stop
10. Find the missing number in an array containing numbers 1 to N with one missing
Input: Array A of size N−1
Output: Missing number
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N −1]
4. Calculate ∑ ¿ N∗(N + 1)/2
5. Initialize arrSum=0
6. For i=1to N−1
o arrSum=arrSum+ A [i]
7. End for
8. Missing number = ∑ −arrSum
9. Print missing number
10. Stop
11. Sort a 1D array using Bubble Sort
Input: Array A of size N
Output: Sorted array A
Algorithm:
1. Start
2. Input N
3. Input array A [1. . N ]
4. For i=1to N−1
o For j=1to N−i
If A [ j]> A [ j+1]
Swap A [ j] and A [ j+1]
5. End for
6. Print array A
7. Stop
12. Merge two sorted 1D arrays into a single sorted array
Input: Arrays A [1. . N ]and B[1.. M ]both sorted
Output: Sorted array C [1. . N + M ]
Algorithm:
1. Start
2. Input N , M
3. Input arrays A , B
4. Initialize i=1 , j=1 , k =1
5. While i≤ N and j ≤ M
o If A [i]≤ B[ j]
C [k ]= A [i]
i=i+1
o Else
C [k ]=B [ j]
j= j+1
o k =k +1
6. End while
7. While i≤ N
o C [k ]= A [i]
o i=i+1, k =k +1
8. While j ≤ M
o C [k ]=B [ j]
o j= j+1, k =k +1
9. Print C
10. Stop
13. Find a pair of elements in a 1D array with sum equals given value
Input: Array A , integer ∑ ¿
Output: Pairs with sum ∑ ¿
Algorithm:
1. Start
2. Input N , A , ∑ ¿
3. For i=1to N−1
o For j=i+1 to N
If A [i]+ A [ j]=∑ ¿
Print A [i], A [ j]
4. Stop
14. Move all zeros in a 1D array to the end
Input: Array A
Output: Array with zeros at end
Algorithm:
1. Start
2. Input N , A
3. Initialize pos=1
4. For i=1to N
o If A [i]≠ 0
A [ pos]= A[i]
pos=pos +1
5. For i= posto N
o A [i]=0
6. Print A
7. Stop
15. Count frequency of each element in a 1D array
Input: Array A
Output: Frequency count
Algorithm:
1. Start
2. Input N , A
3. Initialize visited[ ]as false
4. For i=1to N
o If visited[i]=false
Initialize count=1
For j=i+1 to N
If A [i]= A [ j]
count=count+1
visited[ j]=true
Print A [i], count
5. Stop
16. Shift all elements left by one position, move first element to end
Input: Array A
Output: Left shifted array
Algorithm:
1. Start
2. Input N , A
3. temp= A [Link]
4. For i=1to N−1
o A [i]= A [i+1]
5. A [N ]=temp
6. Print A
7. Stop
17. Check if array is sorted ascending or descending
Input: Array A
Output: Sorted type
Algorithm:
1. Start
2. Input N , A
3. Initialize asc =true , desc=true
4. For i=1to N−1
o If A [i]> A [i+1]then asc =false
o If A [i]< A [i+1]then desc =false
5. If asc print "Ascending"
Else if desc print "Descending"
Else print "Not sorted"
6. Stop
18. Find first and last occurrence of element in sorted 1D array
Input: Sorted array A , element X
Output: First and last indices
Algorithm:
1. Start
2. Input N , A , X
3. Initialize first=−1 ,last =−1
4. For i=1to N
o If A [i]= X and first=−1then first=i
o If A [i]= X then last =i
5. Print first , last
6. Stop
19. Rearrange array so negative numbers appear before positives
Input: Array A
Output: Rearranged array
Algorithm:
1. Start
2. Input N , A
3. Initialize j=1
4. For i=1to N
o If A [i]<0
Swap A [i], A [ j]
j= j+1
5. Print A
6. Stop
20. Find contiguous subarray with sum equals given value
Input: Array A , sum
Output: Indices of subarray
Algorithm:
1. Start
2. Input N , A , ∑ ¿
3. For i=1to N
o currSum= A[i]
o For j=i+1 to N
currSum=currSum+ A [ j]
If currSum=∑ ¿
Print i , jand Stop
4. Print "No subarray found"
5. Stop
2D Array Problems
1. Print matrix elements in row-major order
Input: Matrix A [M ][ N ]
Output: Elements printed row-wise
Algorithm:
1. Start
2. Input M , N , A
3. For i=1to M
o For j=1to N
Print A [i][ j]
4. Stop
2. Print matrix in spiral clockwise order
Input: Matrix A [M ][ N ]
Output: Elements printed spirally
Algorithm:
1. Start
2. Input M , N , A
3. Initialize top=1 , bottom=M ,¿ 1 , ¿=N
4. While top ≤ bottom and ¿ ¿
o For j=¿to ¿: print A [top ][ j]
o top=top+1
o For i=top to bottom : print A [i][¿]
o ¿=¿−1
o If top ≤ bottom
For j=¿down to ¿: print A [bottom ][ j]
bottom=bottom−1
o If ¿ ¿
For i=bottom down to top : print A [i]¿
¿¿1
5. Stop
3. Transpose of a square matrix
Input: Square matrix A [N ][N ]
Output: Transposed matrix
Algorithm:
1. Start
2. Input N , A
3. For i=1to N
o For j=i+1 to N
Swap A [i][ j]and A [ j][i]
4. Print A
5. Stop
4. Matrix multiplication
Input: Matrices A [M ][ N ], B[ N ][ P]
Output: Product matrix C [M ][P]
Algorithm:
1. Start
2. Input M , N , P , A , B
3. If N ≠ N Print “Matrix multiplication not possible” Stop
4. For i=1to M
o For j=1to P
Initialize C [i][ j]=0
For k =1to N
C [i][ j]=C [i][ j]+ A [i][k ]× B[k ][ j]
5. Print C
6. Stop
5. Rotate square matrix by 90 degrees clockwise
Input: Square matrix A [N ][N ]
Output: Matrix rotated 90° clockwise
Algorithm:
1. Start
2. Input N , A
3. Transpose A (as in previous)
4. For each row i=1to N , reverse row A [i]
5. Print A
6. Stop
6. Search element in sorted matrix (rows and columns sorted)
Input: Matrix A [M ][ N ], element X
Output: Position of X or not found
Algorithm:
1. Start
2. Input M , N , A , X
3. Initialize i=1 , j=N
4. While i≤ M and j ≥ 1
o If A [i][ j]=X Print i , jStop
o Else if A [i][ j]> Xj= j−1
o Else i=i+1
5. Print “Element not found”
6. Stop
7. Find row with maximum sum
Input: Matrix A [M ][ N ]
Output: Row number with maximum sum
Algorithm:
1. Start
2. Input M , N , A
3. Initialize maxSum=0 , rowIndex=0
4. For i=1to M
o rowSum=0
o For j=1to NrowSum+ ¿ A [i][ j]
o If rowSum>maxSummaxSum=rowSum , rowIndex=i
5. Print rowIndex
6. Stop
8. Print boundary elements of a matrix
Input: Matrix A [M ][ N ]
Output: Boundary elements
Algorithm:
1. Start
2. Input M , N , A
3. Print first row A [1. . N ][Link]
4. Print last column A [2. . M ][N ]
5. Print last row A [M ][ N−1. .1]
6. Print first column A [M −1. .2][Link]
7. Stop
9. Count negative elements in matrix
Input: Matrix A [M ][ N ]
Output: Count of negative elements
Algorithm:
1. Start
2. Input M , N , A
3. Initialize count=0
4. For each element of A
o If element < 0 count +¿ 1
5. Print count
6. Stop
10. Sum of diagonal elements of a square matrix
Input: Square matrix A [N ][N ]
Output: Sum of main diagonal elements
Algorithm:
1. Start
2. Input N , A
3. Initialize ∑ ¿ 0
4. For i=1to N
o ∑ + ¿ A [i][i]
5. Print ∑ ¿
6. Stop
11. Sum of anti-diagonal elements of a square matrix
Input: Square matrix A [N ][N ]
Output: Sum of anti-diagonal elements
Algorithm:
1. Start
2. Input N , A
3. Initialize ∑ ¿ 0
4. For i=1to N
o ∑ + ¿ A [i][N −i+1 ]
5. Print ∑ ¿
6. Stop
12. Replace all occurrences of an element in matrix
Input: Matrix A [M ][ N ], elements oldVal ,newVal
Output: Matrix with replaced values
Algorithm:
1. Start
2. Input M , N , A , oldVal , newVal
3. For i=1to M
o For j=1to N
If A [i][ j]=oldVal then A [i][ j]=newVal
4. Print A
5. Stop
13. Reverse each row of a matrix
Input: Matrix A [M ][ N ]
Output: Matrix with rows reversed
Algorithm:
1. Start
2. Input M , N , A
3. For each row i=1to M
o Initialize ¿ 1 , ¿=N
o While ¿ ¿
Swap A [i]¿
¿=1 ,¿−¿ 1
4. Print A
5. Stop
14. Calculate column-wise sums
Input: Matrix A [M ][ N ]
Output: Sum of each column
Algorithm:
1. Start
2. Input M , N , A
3. For j=1to N
o Initialize colSum=0
o For i=1to McolSum+ ¿ A [i][ j]
o Print colSum
4. Stop
15. Check if matrix is symmetric
Input: Square matrix A [N ][N ]
Output: True or False
Algorithm:
1. Start
2. Input N , A
3. For i=1to N
o For j=1to N
If A [i][ j]≠ A [ j][i]then Print false and Stop
4. Print true
5. Stop
16. Find maximum element in each row
Input: Matrix A [M ][ N ]
Output: Maximum of each row
Algorithm:
1. Start
2. Input M , N , A
3. For i=1to M
o Initialize maxVal= A [i][Link]
o For j=2to N
If A [i][ j]> maxValthen maxVal= A [i][ j]
o Print maxVal
4. Stop
17. Sort each row of matrix in ascending order
Input: Matrix A [M ][ N ]
Output: Matrix with sorted rows
Algorithm:
1. Start
2. Input M , N , A
3. For each row i=1to M
o Sort A [i][1. . N ]using bubble or another sort
4. Print A
5. Stop
18. Find saddle point of matrix
Input: Matrix A [M ][ N ]
Output: Saddle point(s) if any
Algorithm:
1. Start
2. Input M , N , A
3. For i=1to M
o Find min element in row i, at column j
o Check if A [i][ j]is maximum in column j
If yes, print A [i][ j]as saddle point
4. Stop