1. Given an array of positive integers arr[], return the second largest element from the array.
If
the second largest element doesn't exist then return -1.
Algorithm: Find Second Largest Element in an Array
Step 1:
Start
Step 2:
Read integer n (size of array)
Step 3:
If n < 2
Print -1
Stop the program
Step 4:
Declare an array arr of size n
Step 5:
Read n array elements
Step 6:
Initialize:
largest = INT_MIN
secondLargest = INT_MIN
Step 7:
For each element arr[i] from index 0 to n-1, do:
1. If arr[i] > largest
o Assign secondLargest = largest
o Assign largest = arr[i]
2. Else if arr[i] < largest AND arr[i] > secondLargest
o Assign secondLargest = arr[i]
Step 8:
After loop completion:
If secondLargest == INT_MIN
o Print -1
Else
o Print secondLargest
Step 9:
Stop
Program:
#include <iostream>
#include<limits>
using namespace std;
int main()
{
int n;
cin >> n; // read array size
if(n < 2)
{
cout << -1;
return 0;
}
int arr[n]; // create array
for(int i = 0; i < n; i++)
{
cin >> arr[i]; // read array elements
}
int largest = INT_MIN;
int secondLargest = INT_MIN;
for(int i = 0; i < n; i++)
{
if(arr[i] > largest)
{
secondLargest = largest;
largest = arr[i];
}
else if(arr[i] < largest && arr[i] > secondLargest)
{
secondLargest = arr[i];
}
}
if(secondLargest == INT_MIN)
cout << -1;
else
cout << secondLargest;
return 0;
}
Input:
5
10 20 4 45 99
Output:
45
Input:
4
7777
Output:
-1
Input:
1
5
Output:
-1
2. Given two numbers N and M, a 2D array A of size N * M which contains 'x' or '.' only and two numbers X, Y
which donates a cell position in A such that X is the row number and Y is the column number. Determine
whether all neighbors of the given cell are 'x' or not.
Algorithm
Step 1:
Start
Step 2:
Read integers N and M
Step 3:
Declare a 2D array A[N][M]
Step 4:
Read all elements of matrix
(Each element is either 'x' or '.')
Step 5:
Read position X and Y
Step 6:
Convert to 0-based index
X=X-1
Y=Y-1
Step 7:
For i from X-1 to X+1
For j from Y-1 to Y+1
o If (i == X AND j == Y)
→ Skip (because it is the given cell)
o If (i >= 0 AND i < N AND j >= 0 AND j < M)
If A[i][j] != 'x'
Print "no"
Stop
Step 8:
If loop completes without finding '.'
Print "yes"
Step 9:
Stop
Program:
#include <iostream>
using namespace std;
int main()
{
int N, M;
cin >> N >> M;
char A[100][100];
// Read matrix
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
cin >> A[i][j];
}
}
int X, Y;
cin >> X >> Y;
// Convert to 0-based index
X--;
Y--;
for(int i = X-1; i <= X+1; i++)
{
for(int j = Y-1; j <= Y+1; j++)
{
// Skip center cell
if(i == X && j == Y)
continue;
// Boundary check
if(i >= 0 && i < N && j >= 0 && j < M)
{
if(A[i][j] != 'x')
{
cout << "no";
return 0;
}
}
}
}
cout << "yes";
return 0;
}
Sample Input 1
33
xxx
x.x
xxx
22
Output:
yes
Sample Input 2
33
xxx
xxx
xx.
22
Output:
no
Sample Input 3
33
xxx
xxx
xxx
11
Output:
Yes
3. Given an array A of size N. Print the array elements after shifting all zeroes in array A to the right
Algorithm
Step 1:
Start
Step 2:
Read integer N
Step 3:
Declare array A[N]
Step 4:
Read array elements
Step 5:
Initialize count = 0
(This will store position for next non-zero element)
Step 6:
Traverse array from i = 0 to N-1
If A[i] != 0
o Assign A[count] = A[i]
o Increment count
Step 7:
After placing all non-zero elements,
From index count to N-1
Assign A[i] = 0
Step 8:
Print array
Step 9:
Stop
Program:
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
int A[N];
for(int i = 0; i < N; i++)
{
cin >> A[i];
}
int count = 0;
// Move non-zero elements to front
for(int i = 0; i < N; i++)
{
if(A[i] != 0)
{
A[count] = A[i];
count++;
}
}
// Fill remaining positions with zero
for(int i = count; i < N; i++)
{
A[i] = 0;
}
// Print result
for(int i = 0; i < N; i++)
{
cout << A[i] << " ";
}
return 0;
}
Sample Input 1
5
10203
Output:
12300
Sample Input 2
6
001230
Output:
123000
Sample Input 3
4
0000
Output:
0000