Skip to content

Commit a3a94d6

Browse files
committed
🚀 16-Apr-2022
1 parent 2febd01 commit a3a94d6

13 files changed

+914
-47
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
You are given an array a1,a2,…,an of positive integers. A good pair is a pair of indices (i,j) with 1=i,j=n such that, for all 1=k=n, the following equality holds:
2+
3+
|ai-ak|+|ak-aj|=|ai-aj|,
4+
where |x| denotes the absolute value of x.
5+
6+
Find a good pair. Note that i can be equal to j.
7+
8+
Input
9+
The input consists of multiple test cases. The first line contains a single integer t (1=t=1000) — the number of test cases. Description of the test cases follows.
10+
11+
The first line of each test case contains an integer n (1=n=105) — the length of the array.
12+
13+
The second line of each test case contains n integers a1,a2,…,an (1=ai=109) where ai is the i-th element of the array.
14+
15+
The sum of n for all test cases is at most 2·105.
16+
17+
Output
18+
For each test case, print a single line with two space-separated indices i and j which form a good pair of the array.
19+
The case i=j is allowed. It can be shown that such a pair always exists. If there are multiple good pairs, print any of them.
20+
21+
Example
22+
inputCopy
23+
3
24+
3
25+
5 2 7
26+
5
27+
1 4 2 2 3
28+
1
29+
2
30+
outputCopy
31+
2 3
32+
1 2
33+
1 1
34+
Note
35+
In the first case, for i=2 and j=3 the equality holds true for all k:
36+
37+
k=1: |a2-a1|+|a1-a3|=|2-5|+|5-7|=5=|2-7|=|a2-a3|,
38+
k=2: |a2-a2|+|a2-a3|=|2-2|+|2-7|=5=|2-7|=|a2-a3|,
39+
k=3: |a2-a3|+|a3-a3|=|2-7|+|7-7|=5=|2-7|=|a2-a3|.
40+
41+
42+
43+
44+
45+
46+
#include<bits/stdc++.h>
47+
using namespace std;
48+
49+
void solve() {
50+
int n;
51+
cin >> n;
52+
53+
vector <int> v(n);
54+
for (auto &x: v) cin >> x;
55+
56+
int minv = INT_MAX, maxv = INT_MIN, mini = 0, maxi = 0;
57+
58+
for (int i = 0; i < n; i++) {
59+
if (v[i] < minv) {
60+
minv = v[i];
61+
mini = i;
62+
}
63+
if (v[i] > maxv) {
64+
maxv = v[i];
65+
maxi = i;
66+
}
67+
}
68+
69+
cout << mini + 1 << " " << maxi + 1 << endl;
70+
}
71+
72+
int main(){
73+
ios_base::sync_with_stdio(false);
74+
cin.tie(NULL);
75+
76+
int t = 1;
77+
cin >> t;
78+
79+
while (t--) solve();
80+
81+
return 0;
82+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
You are given an integer array height of length n.
2+
There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
3+
4+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
5+
6+
Return the maximum amount of water a container can store.
7+
8+
Notice that you may not slant the container.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: height = [1,8,6,2,5,4,8,3,7]
16+
Output: 49
17+
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7].
18+
In this case, the max area of water (blue section) the container can contain is 49.
19+
Example 2:
20+
21+
Input: height = [1,1]
22+
Output: 1
23+
24+
25+
Constraints:
26+
27+
n == height.length
28+
2 <= n <= 105
29+
0 <= height[i] <= 104
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
int maxArea(vector<int>& height) {
40+
int res = 0, start = 0, end = height.size() - 1;
41+
42+
while (start <= end) {
43+
int water = min(height[start], height[end]) * (end - start);
44+
res = max(res, water);
45+
if (height[start] <= height[end]) start++;
46+
else end--;
47+
}
48+
return res;
49+
}
50+
};

competitive programming/leetcode/1557. Minimum Number of Vertices to Reach All Nodes.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
Given a directed acyclic graph, with n vertices numbered from 0 to n-1, and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.
1+
Given a directed acyclic graph, with n vertices numbered from 0 to n-1,
2+
and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.
23

3-
Find the smallest set of vertices from which all nodes in the graph are reachable. It's guaranteed that a unique solution exists.
4+
Find the smallest set of vertices from which all nodes in the graph are reachable.
5+
It's guaranteed that a unique solution exists.
46
57
Notice that you can return the vertices in any order.
68
@@ -12,14 +14,16 @@ Example 1:
1214
1315
Input: n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
1416
Output: [0,3]
15-
Explanation: It's not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].
17+
Explanation: It's not possible to reach all the nodes from a single vertex.
18+
From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].
1619
Example 2:
1720

1821

1922

2023
Input: n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
2124
Output: [0,2,3]
22-
Explanation: Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.
25+
Explanation: Notice that vertices 0, 3 and 2 are not reachable from any other node,
26+
so we must include them. Also any of these vertices can reach nodes 1 and 4.
2327

2428

2529
Constraints:
@@ -41,15 +45,23 @@ All pairs (fromi, toi) are distinct.
4145
class Solution {
4246
public:
4347
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) {
44-
vector<bool> incEdge(n, false);
45-
vector<int> res;
46-
for(auto &it: edges){
47-
incEdge[it[1]]=true;
48+
vector <bool> incEdge(n, false);
49+
vector <int> res;
50+
for (auto &it: edges) {
51+
incEdge[it[1]] = true;
4852
}
49-
for(int i=0;i<n;i++){
50-
if(incEdge[i]==false) res.push_back(i);
53+
54+
for (int i = 0; i < n; i++) {
55+
if (incEdge[i] == false) res.push_back(i);
5156
}
5257

5358
return res;
5459
}
5560
};
61+
62+
63+
64+
65+
// A node that does not have any incoming edge can only be reached by itself.
66+
// Any other node with incoming edges can be reached from some other node.
67+
// We only have to count the number of nodes with zero incoming edges.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
There is an undirected star graph consisting of n nodes labeled from 1 to n.
2+
A star graph is a graph where there is one center node and exactly n - 1 edges
3+
that connect the center node with every other node.
4+
5+
You are given a 2D integer array edges where each edges[i] = [ui, vi]
6+
indicates that there is an edge between the nodes ui and vi.
7+
Return the center of the given star graph.
8+
9+
10+
11+
Example 1:
12+
13+
14+
Input: edges = [[1,2],[2,3],[4,2]]
15+
Output: 2
16+
Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
17+
Example 2:
18+
19+
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
20+
Output: 1
21+
22+
23+
Constraints:
24+
25+
3 <= n <= 105
26+
edges.length == n - 1
27+
edges[i].length == 2
28+
1 <= ui, vi <= n
29+
ui != vi
30+
The given edges represent a valid star graph.
31+
32+
33+
34+
35+
36+
// using indegree concept
37+
38+
class Solution {
39+
public:
40+
int findCenter(vector<vector<int>>& edges) {
41+
int n = edges.size() + 1;
42+
43+
vector <int> adj[n + 1];
44+
45+
for (auto &edge: edges) {
46+
adj[edge[0]].push_back(edge[1]);
47+
adj[edge[1]].push_back(edge[0]);
48+
}
49+
50+
for (int i = 1; i < n + 1; i++) {
51+
if (n - 1 == adj[i].size()) return i;
52+
}
53+
54+
return -1; // no star
55+
}
56+
};
57+
58+
59+
60+
61+
62+
63+
64+
65+
// star node will be a part of all the edges
66+
// star node will have (n - 1) indegree and also the edges.size() is equal to (n - 1)
67+
68+
class Solution {
69+
public:
70+
int findCenter(vector<vector<int>>& edges) {
71+
int a = edges[0][0];
72+
int b = edges[0][1];
73+
int c = edges[1][0];
74+
int d = edges[1][1];
75+
76+
if (a == c || a == d) return a;
77+
else return b;
78+
}
79+
};

0 commit comments

Comments
 (0)