Give me code in cpp I am giving flipkart grid
Créé le : 13 juillet 2025
Créé le : 13 juillet 2025
Below is a clean, self-contained C++17 solution that:
cpp#include <bits/stdc++.h> using namespace std; class ArticulationPoints { public: explicit ArticulationPoints(int n) : n(n), g(n), disc(n, -1), low(n, 0), parent(n, -1), isAP(n, false), timer(0) {} void addEdge(int u, int v) { g[u].push_back(v); g[v].push_back(u); // undirected } const vector<bool>& run() { for (int v = 0; v < n; ++v) if (disc[v] == -1) dfs(v); return isAP; } private: int n; vector<vector<int>> g; vector<int> disc, low, parent; vector<bool> isAP; int timer; void dfs(int u) { disc[u] = low[u] = ++timer; int childCnt = 0; for (int v : g[u]) { if (disc[v] == -1) { // tree edge parent[v] = u; ++childCnt; dfs(v); low[u] = min(low[u], low[v]); // Articulation-point rules if (parent[u] != -1 && low[v] >= disc[u]) isAP[u] = true; if (parent[u] == -1 && childCnt > 1) // root isAP[u] = true; } else if (v != parent[u]) { // back edge low[u] = min(low[u], disc[v]); } } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; if (!(cin >> n) || n <= 1 || n >= 100) return 0; vector<vector<int>> mat(n, vector<int>(n)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> mat[i][j]; vector<long long> cost(n); for (int i = 0; i < n; ++i) cin >> cost[i]; ArticulationPoints ap(n); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (mat[i][j]) ap.addEdge(i, j); const auto &isAP = ap.run(); long long total = 0; for (int i = 0; i < n; ++i) if (isAP[i]) total += cost[i]; cout << total << '\n'; return 0; }
Step | Purpose |
---|---|
Build graph | Converts the n × n matrix to an adjacency list (undirected). |
DFS / Tarjan | Tracks each vertex’s discovery time (disc ) and the lowest reachable ancestor (low ). |
Articulation checks | A non-root vertex u is a super tunnel if it owns a child v with low[v] ≥ disc[u] ; a root is super if it has >1 DFS children. |
Cost sum | Adds maintenance costs for every vertex flagged as super. |
The algorithm runs in O(n²) time for matrix read/build and O(V + E) for DFS (≤ 10,000 operations here), well within the given constraints.
I have given A function named findtunnelmantenance with int **arr ,int n ,int cost[]
Here’s a drop-in implementation for the function you were given.
It takes the n × n
adjacency matrix arr
, the vertex count n
, and the maintenance-cost array cost[]
, then returns the total cost of every super tunnel (articulation point).
cpp#include <vector> #include <algorithm> #include <functional> using namespace std; /* * arr : adjacency-matrix pointer (0/1 entries, undirected) * n : number of tunnels (1 < n < 100) * cost : maintenance cost of each tunnel (0 < cost[i] < 1e6) * * returns: total cost of all super tunnels */ int findTunnelMaintenanceCost(int **arr, int n, int cost[]) { /* 1. build adjacency list */ vector<vector<int>> g(n); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (arr[i][j]) { // arr is symmetric g[i].push_back(j); g[j].push_back(i); } /* 2. Tarjan DFS to mark articulation points */ vector<int> disc(n, -1), low(n, 0), parent(n, -1); vector<char> isAP(n, 0); int timer = 0; function<void(int)> dfs = [&](int u) { disc[u] = low[u] = ++timer; int children = 0; for (int v : g[u]) { if (disc[v] == -1) { // tree edge parent[v] = u; ++children; dfs(v); low[u] = min(low[u], low[v]); // articulation rules if (parent[u] != -1 && low[v] >= disc[u]) isAP[u] = 1; if (parent[u] == -1 && children > 1) isAP[u] = 1; } else if (v != parent[u]) { // back edge low[u] = min(low[u], disc[v]); } } }; for (int i = 0; i < n; ++i) if (disc[i] == -1) dfs(i); /* 3. accumulate maintenance cost of super tunnels */ long long total = 0; for (int i = 0; i < n; ++i) if (isAP[i]) total += cost[i]; return static_cast<int>(total); // safe: constraints keep sum < 1e9 }
Phase | Purpose |
---|---|
Graph build | Converts the matrix arr into an adjacency list for time. |
Tarjan DFS | Runs once per connected component; marks a tunnel as super if its removal disconnects the graph. |
Cost sum | Adds cost[i] for every articulation point, returning the project total. |
The overall complexity remains with — easily within limits for Flipkart Grid coding rounds.
pythonfrom PIL import Image img = Image.open('/mnt/data/IMG20250713185901.jpg') # Let's crop center left rectangular coordinates maybe the text area left side w,h = img.size crop = img.crop((0,0,int(w*0.6),int(h*0.8))) crop.save('/mnt/data/crop.jpg')
Below is a self-contained C++17 implementation of the function you were asked to fill in.
cpp/**************************************************************** * Problem recap (for context) * ─────────────────────────── * • We have N (2 ≤ N ≤ 1 000 000) candy-variety batch sizes. * • Let arr[i] be the number of candies produced in one batch * of variety i (1 ≤ arr[i] ≤ 10 000 000). * • Avish chooses a positive integer x and sends exactly one * box of x candies from every variety whose batch size is * divisible by x, **provided at least two varieties qualify**. * • Goal: maximise x × (count of arr[i] that are multiples of x). * * The function below returns that maximum total. ****************************************************************/ #include <bits/stdc++.h> using namespace std; long long int getresult(long long int n, long long int arr[]) { /*---------------------------------------------------------- 1. Pre-compute frequency of every batch size value. We also keep track of the maximum value seen so that our auxiliary arrays are no larger than necessary. ----------------------------------------------------------*/ int maxVal = 0; for (long long i = 0; i < n; ++i) maxVal = max(maxVal, static_cast<int>(arr[i])); /* freq[v] = how many varieties have batch-size == v */ vector<int> freq(maxVal + 1, 0); for (long long i = 0; i < n; ++i) ++freq[ static_cast<int>(arr[i]) ]; /*---------------------------------------------------------- 2. Sieve-style scan: For every possible box-size x (1 … maxVal) we sum the counts of all batch-sizes that are multiples of x. Complexity: Σ_{x=1..maxVal} (maxVal / x) ≤ maxVal · H(maxVal) ≈ 1.6·10^8 ops which is fine in C++ with O(40 MB) memory. ----------------------------------------------------------*/ long long best = 0; for (int x = 1; x <= maxVal; ++x) { long long cnt = 0; // #varieties divisible by x for (int k = x; k <= maxVal; k += x) cnt += freq[k]; if (cnt >= 2) { // needs ≥2 varieties long long total = cnt * 1LL * x; // possible candies sent if (total > best) best = total; } } return best; // 0 if no valid x exists }
Step | Explanation |
---|---|
Frequency table | We only care how many varieties have a certain batch size, so freq[v] lets us answer “how many arr[i] equal v” in O(1). |
Sieve-like scan | For each candidate box size x , we iterate over its multiples (x, 2x, 3x, … ). Summing freq on those indices tells us cnt(x) — the number of varieties whose batch size is divisible by x . |
Optimality check | If cnt(x) ≥ 2 , the plan is feasible and ships x * cnt(x) candies. Keeping the maximum across all x gives the answer. |
Time / memory | ≤ 1.6 × 10⁸ primitive operations and about 40 MB RAM (freq for values up to 10⁷) — both acceptable within typical coding-round limits. |
Just paste this function into the scaffold provided by the platform; no other changes are required. Good luck!