15) Trapping Rain Water
15) Trapping Rain Water
#include<bits/stdc++.h>
int res = 0;
// Find the maximum element on its right
}
return res;
// Driver code
int main()
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
Output
6
Complexity Analysis:
Time Complexity: O(n2).
There are two nested loops traversing the array, So time Complexity is
O(n2).
Space Complexity: O(1).
No extra space required.
return water;
}
// Driver program
int main()
{
int arr[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum water that can be accumulated is "
<< findWater(arr, n);
return 0;
}
Output: Maximum water that can be accumulated is 6
Complexity Analysis:
Time Complexity: O(n).
Only one traversal of the array is needed, So time Complexity is O(n).
Space Complexity: O(n).
Two extra array is needed each of size n.
#include <iostream>
using namespace std;
int findWater(int arr[], int n)
{
// initialize output
int result = 0;
return result;
}
int main()
{
int arr[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum water that can be accumulated is "
<< findWater(arr, n);
}
Output:
Maximum water that can be accumulated is 6
Complexity Analysis:
Time Complexity: O(n).
Only one traversal of the array is needed.
Auxiliary Space: O(1).
As no extra space is required.
Method 3: Here another efficient solution has been shown.
// Driver Code
int main()
{
int arr[] = { 0, 1, 0, 2, 1, 0,
1, 3, 2, 1, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Driver code
int main()
{
int arr[] = { 0, 1, 0, 2, 1, 0,
1, 3, 2, 1, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxWater(arr, n);
return 0;
}
Output:
6
Time Complexity: O(n)
Auxiliary Space: O(n)
// Driver code
int main() {
int arr[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
cout << maxWater(arr, n) << endl;
return 0;
}
Output :
6
Time Complexity: O(n)
Auxiliary Space: O(1)
16) Pythagorean Triplets
Method 1 (Naive)
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
// Now fix one element one by one and find the other two
// elements
for (int i = n - 1; i >= 2; i--) {
// To find the other two elements, start two index
// variables from two corners of the array and move
// them toward each other
int l = 0; // index of the first element in arr[0..i-1]
int r = i - 1; // index of the last element in arr[0..i-1]
while (l < r) {
// A triplet found
if (arr[l] + arr[r] == arr[i])
return true;
#include <bits/stdc++.h>
using namespace std;
// Hashing array
int hash[maximum + 1] = { 0 };
// If a is not there
if (hash[i] == 0)
continue;
// Find c
int val = sqrt(i * i + j * j);
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
int arr[] = { 3, 2, 4, 6, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
if (checkTriplet(arr, n))
cout << "Yes";
else
cout << "No";
}
Output
Yes
Time Complexity:O(n2)
int main()
{
int arr[] = { 12, 4, 7, 9, 2, 23, 25, 41, 30,
40, 28, 42, 30, 44, 48, 43, 50 };
int m = 7; // Number of students
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum difference is "
<< findMinDiff(arr, n, m);
return 0;
}
// Driver code
int main()
{
int price[] = { 2, 30, 15, 10, 8, 25, 80 };
int n = sizeof(price) / sizeof(price[0]);
cout << "Maximum Profit = " << maxProfit(price, n);
return 0;
}
Output
Maximum Profit = 100
The time complexity of the above solution is O(n).
Valley-Peak approach
#include <iostream>
using namespace std;
int main()
{
int price[] = { 2, 30, 15, 10, 8, 25, 80 };
int n = 7;
// adding array
int profit = 0;
// Initializing variable
// valley-peak approach
/*
80
/
30 /
/\ 25
/ 15 /
/ \ /
2 10 /
\/
8
*/
for (int i = 1; i < n; i++)
{
// Fill leftMax[]1..n-1]
for (int i = 1; i < n; i++)
leftMax[i] = max(leftMax[i-1], arr[i-1]);
// Driver program
int main()
{
int arr[] = {5, 1, 4, 3, 6, 8, 10, 7, 9};
int n = sizeof arr / sizeof arr[0];
cout << "Index of the element is " << findElement(arr, n);
return 0;
}
Output:
Index of the element is 4
Time Complexity: O(n)
Auxiliary Space: O(n)
// Driver Code
int main()
{
int arr[] = { 5, 1, 4, 3, 6, 8, 10, 7, 9 };
int n = sizeof arr / sizeof arr[0];
// Function Call
cout << "Index of the element is "
<< findElement(arr, n);
return 0;
}
Output
Index of the element is 4
Time Complexity: O(n)
Auxiliary Space: O(1)
// Driver program
int main()
{
int arr[] = {4, 3, 7, 8, 6, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
zigZag(arr, n);
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
Output:
3 7 4 8 2 6 1
Time complexity: O(n)
Auxiliary Space: O(1)
21)last index of 1
22) Print a given matrix in spiral form
#include <bits/stdc++.h>
using namespace std;
if (matrix.size() == 0)
return ans;
// Iterate from 0 to R * C - 1
for (int i = 0; i < R * C; i++) {
ans.push_back(matrix[r]);
seen[r] = true;
int cr = r + dr[di];
int cc = c + dc[di];
// Driver code
int main()
{
vector<vector<int>> a { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
for(int x:spiralOrder(a))
{
cout << x << " ";
}
return 0;
}
Output:[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
Complexity Analysis:
Time Complexity: O(N)O(N), where NN is the total number of elements in the
input matrix. We add every element in the matrix to our final answer.
Space Complexity: O(N)O(N), the information stored in seen and in ans.
Method 2:
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 6
/* Driver Code */
int main()
{
int a[R][C] = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } };
// Function Call
spiralPrint(R, C, a);
return 0;
}
Output
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
Complexity Analysis:
Time Complexity: O(m*n).
To traverse the matrix O(m*n) time is required.
Space Complexity: O(1).
No extra space is required.
Method 3: (Recursive Approach)
#define R 4
#define C 4
// Function for printing matrix in spiral
// form i, j: Start index of matrix, row
// and column respectively m, n: End index
// of matrix row and column respectively
void print(int arr[R][C], int i, int j, int m, int n)
{
// If i or j lies outside the matrix
if (i >= m or j >= n)
return;
print(arr, i + 1, j + 1, m - 1, n - 1);
}
// Driver Code
int main()
{
int a[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
// Function Call
print(a, 0, 0, R, C);
return 0;
}
Output
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Complexity Analysis:
Time Complexity: O(m*n).
To traverse the matrix O(m*n) time is required.
Space Complexity: O(1).
No extra space is required
.
Method 4: (DFS Recursive Approach)
#include <iostream>
#include <vector>
using namespace std;
#define R 4
#define C 4
// to traverse spirally
vector<int> spirallyTraverse(int matrix[R][C])
{
vector<int> res;
spirallyDFSTravserse(matrix, 0, 0, 0, res);
return res;
}
// Driver Code
int main()
{
int a[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
// Function Call
vector<int> res = spirallyTraverse(a);
int size = res.size();
for (int i = 0; i < size; ++i)
cout << res[i] << " ";
cout << endl;
return 0;
}
Output
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Complexity Analysis:
Time Complexity: O(m*n). To traverse the matrix O(m*n) time is required.
Space Complexity: O(1). No extra space is required (without consideration of the stack
used by the recursion).
23) Given an array of numbers, program to arrange the numbers to form the largest
number
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Driver code
int main()
{
vector<string> arr;
return 0;
}
Output
6054854654
Time Complexity: O(nlogn) ,sorting is considered to have running time complexity of
O(nlogn) and the for loop runs in O(n) time.
Auxiliary Space: O(1).