id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "using ll = long long;\nclass Solution {\npublic:\n ll dfs(int cur, vector<vector<ll>>& adj, vector<bool>& vis, int& mini){\n ll count = 0;\n if(vis[cur]) return count;\n vis[cur] = true;\n if(cur>mini) count++;\n for(ll num: adj[cur]){\n count+=dfs(num, adj,...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(int current, int& count, unordered_set<int> &visited, vector<vector<int>> &neighbours)\n {\n visited.insert(current);\n count++;\n\n vector<int>& currentNeighbours = neighbours[current];\n for(int neighbour : currentNeighbours)\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<int>> graph;\n vector<int> visited;\n int dfs(int node){\n visited[node]=1;\n int cnt=1;\n for(auto &x: graph[node]){\n if(!visited[x]) cnt+=dfs(x);\n }\n return cnt;\n }\n long long countPair...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(int node,unordered_map<int,vector<int>>&graph,int &count,vector<int>&vis){\n vis[node]=1;\n count++;\n for(auto &child:graph[node]){\n if(!vis[child]){\n dfs(child,graph,count,vis);\n }\n }\n }\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "unordered_set<int> adj[100000];\nbool vis[100000];\nstack<int> stk;\nclass Solution {\npublic:\n long long countPairs(const int& n, const vector<vector<int>>& edges) {\n for(int i = 0; i < n; i++) adj[i].clear(), vis[i] = false;\n for(const vector<int> &edge : edges) adj[edge[0]].insert(ed...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n int dfs(int i,vector<bool>&vis,unordered_map<int,vector<int>>&adj)\n {\n vis[i]=1;\n\n int ans=1;\n\n for(auto x:adj[i])\n {\n if(!vis[x])\n ans=ans+dfs(x,vis,adj);\n }\n\n return ans;\n }\n\n long l...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long dfs(unordered_map<int,vector<int>>& adj, int u,vector<bool> &visited){\n long long k=1;\n visited[u]=true;\n for(auto &v:adj[u]){\n if(!visited[v]){\n k = dfs(adj,v,visited)+k;\n }\n }\n return...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, int> mp;\n void dfs(vector<vector<int>>& connect, int node){\n mp[node]++;\n for(auto n : connect[node]){\n if(!mp.count(n)){\n dfs(connect, n);\n }\n }\n }\n\n long long countPairs(int n, v...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n vector<int>parent;\n vector<int>rank;\n int find(int u){\n if(parent[u]==u){\n return u;\n }\n return parent[u]=find(parent[u]);\n }\n void Union(int u, int v){\n int parent_u=find(u);\n int parent_v=find(v);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n vector<int>parent;\n vector<int>rank;\n int find(int u){\n if(parent[u]==u){\n return u;\n }\n return parent[u]=find(parent[u]);\n }\n void Union(int u, int v){\n int parent_u=find(u);\n int parent_v=find(v);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void helper(int node, unordered_map<int,vector<int>>&mp, vector<bool>&vis,long long & count){\n vis[node]=true;\n count++;\n for(auto i:mp[node]){\n if(!vis[i]){\n helper(i,mp,vis,count);\n }\n }\n }\n lon...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class disjointset {\npublic:\n vector<int> rank, parents;\n\n disjointset(int n) {\n rank.resize(n + 1, 0);\n parents.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parents[i] = i;\n }\n }\n\n int findupr(int node) {\n if (node == parents[node]) ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\n using BigIntTy = long long;\n map<int, vector<int>> graph;\npublic:\n void dfs(int root, vector<bool>& visited, BigIntTy& count) {\n visited[root] = true; \n for(auto e : graph[root]) {\n if(!visited[e]) dfs(e, visited, count);\n }\n count = count+1;\...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "// class Solution {\n// public:\n// vector<list<int>> graph;\n// unordered_set<int> visited;\n \n// void add_edge(int s, int d, bool dir) {\n// graph[s].push_back(d);\n// if(dir) graph[d].push_back(s);\n// }\n \n// void calculate_connected_components(int source, in...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int>& component, unordered_map<int, vector<int>>& adj, vector<bool>& visited) {\n visited[node] = true;\n component.push_back(node);\n \n for (auto v : adj[node]) {\n if (!visited[v]) {\n dfs(v, c...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n\n int bfs(vector<int> adj[], int src, vector<int>& vis){\n\n queue<int> q;\n q.push(src);\n unordered_set<int> st;\n\n while(!q.empty()){\n int node= q.front();\n q.pop();\n vis[node]=1;\n st.insert(node)...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n#define ll long long\nunordered_map<int,vector<int>>adj;\nll solve(int node,vector<int>&visited){\n visited[node]++;\n ll lol=0;\n for(auto ele:adj[node]){\n if(visited[ele]==0){\n ll temp=1+solve(ele,visited);\n lol+=temp;\n }\n }\...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n#define ll long long\nunordered_map<int,vector<int>>adj;\nll solve(int node,vector<int>&visited){\n visited[node]++;\n ll lol=0;\n for(auto ele:adj[node]){\n if(visited[ele]==0){\n ll temp=1+solve(ele,visited);\n lol+=temp;\n }\n }\...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n\n void dfs(int node ,vector<int>&visited,unordered_map<int,vector<int>>&adj,int &size){\n visited[node]=1;\n size++;\n \n for(auto it:adj[node]){\n if(!visited[it]){\n dfs(it,visited,adj,size);\n }\n }\n\...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\n\npublic:\n int countConnectedComponent(int node, map<int, vector<int>>& graph,\n vector<int>& seen) {\n int count = 1;\n seen[node] = 1;\n for (auto neighbour : graph[node]) {\n if (!seen[neighbour])\n count...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n vector<bool>vis(n+1,false);\n unordered_map<int,vector<int>>map;\n\n for(auto t : edges)\n {\n map[t[0]].emplace_back(t[1]);\n map[t[1]].emplace_back(t[0]);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void DFS(unordered_map<int, vector<int>> &adj, int u, vector<bool> &visited, long long &Size){\n visited[u] = true;\n Size++;\n for(int &v: adj[u]){\n if(!visited[v]){\n //Size++;\n DFS(adj, v, visited, Size);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n map <int,vector<int>> adj;\n int ct;\n void dfs(int node , vector <int> &vis) {\n vis[node] = 1;\n\n for(auto child : adj[node]) {\n if(vis[child]) continue;\n ct++;\n dfs(child , vis);\n }\n }\n long long coun...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n vector<list<int>> graph;\n unordered_set<int> visited;\n \n void add_edge(int s, int d, bool dir) {\n graph[s].push_back(d);\n if(dir) graph[d].push_back(s);\n }\n \n void calculate_connected_components(int source, int &total_node) {\n v...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n int dfs(vector<list<int>>&graph,int src,unordered_set<int>&visited){\n visited.insert(src);\n int size = 1;\n for(auto neigh : graph[src]){\n if(not visited.count(neigh)){\n size+= dfs(graph,neigh,visited);\n }\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n\n void dfs(int node,vector<bool> &visited,long long &count,unordered_map<int,list<int>> &adj){\n if(visited[node]) return;\n visited[node] = true;\n count++;\n\n for(auto i :adj[node]){\n dfs(i,visited,count,adj);\n }\n }\n\...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n vector<list<int>> graph;\n int v;\n\n void add_edge(int src, int dest, bool bi_dir = true) {\n graph[src].push_back(dest);\n if (bi_dir) {\n graph[dest].push_back(src);\n }\n }\n\n int dfs(int src, unordered_set<int>& visited) {\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\nvector<list<long long>> graph;\nunordered_set<long long> visited;\nlong long numOfEleConnectedComp(long long i, long long count){\n visited.insert(i);\n count++;\n for(auto neighbour: graph[i]){\n if(!visited.count(neighbour)) count=numOfEleConnectedComp(neighbour...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\nvoid dfs(int src, unordered_map<int, list<int>> &mp, vector<bool>&visited,long long &nodec){\n visited[src]=true;\n nodec++;\n for(auto i: mp[src]){\n if(!visited[i]){\n dfs(i,mp,visited,nodec);\n }\n }\n}\n long long countPairs(int n, vec...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void helper(auto & adj , auto&vis ,int src ,long long &count ) {\n vis[src] = true;\n count++;\n for(auto &nbr : adj[src]){\n if(!vis[nbr])\n helper(adj,vis,nbr,count);\n }\n }\n long long countPairs(int n, vector<ve...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n map<long long int,vector<long long int>>mp;\n for(auto it:edges){\n mp[it[0]].push_back(it[1]);\n mp[it[1]].push_back(it[0]);\n }\n vector<int>visited(n+1,0);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(int node, unordered_map<int, vector<int>>&adj, vector<bool>&visited, vector<int>&component){\n visited[node] = true;\n component.push_back(node);\n\n for(auto v : adj[node]){\n if(!visited[v]){\n dfs(v, adj, visited, com...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> map;\n unordered_set<int> vis;\n adj(map, edges);\n\n vector<int> cSizes;\n for(auto el : map) {\n if(!vis.count(el.first)) {\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, bool> visited;\n unordered_map<int, vector<int>> adj;\n\n long long dfs(int node) {\n if(visited.count(node))\n return 0;\n \n visited[node] = true;\n long long total = 1;\n for(auto neighbour : adj[node])...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(unordered_map<int, vector<int>>& adjList, int src,\n unordered_map<int, bool>& visited, long long& num) {\n visited[src] = true;\n num++;\n\n for (auto v : adjList[src]) {\n if (!visited[v])\n dfs(adjList, v,...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(int& src, long long &count, unordered_map<int, list<int>>& adjList, unordered_map<int, bool>& vis) {\n vis[src] = true;\n count++; \n\n for (auto& nbr : adjList[src]) {\n if (!vis[nbr]) {\n dfs(nbr, count, adjList, vis);...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\nprivate:\n void dfs(unordered_map<int,vector<int>>& graph, int node, unordered_set<int>& visited, long* nof_nodes_p){\n if(visited.find(node) != visited.end()){\n return;\n }\n *nof_nodes_p = *nof_nodes_p + 1;\n visited.insert(node);\n for(...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n\n void dfs(int root, int &freq, unordered_set<int> &visited, unordered_map<int, vector<int>> &adjList){\n\n if(visited.find(root) != visited.end()) return; // already visited\n\n freq++;\n\n visited.insert(root);\n\n for(int node: adjList[root]){\n...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n\n void dfs(int root, int &freq, unordered_set<int> &visited, unordered_map<int, vector<int>> &adjList){\n\n if(visited.find(root) != visited.end()) return; // already visited\n\n freq++;\n\n visited.insert(root);\n\n for(int node: adjList[root]){\n...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "using namespace std;\nclass Solution {\n \npublic:\n void dfs (int node, unordered_map<int, vector<int>>& graph, set<int> &seen, long long& componentSize) {\n seen.insert(node);\n componentSize++;\n\n for (auto& n: graph[node]) {\n if (!seen.contains(n)) {\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(int node, vector<int> &visited, unordered_map<int, list<int>> &adj, long long &count){\n visited[node] = true;\n count++;\n\n for(auto nbr : adj[node]){\n if( !visited[nbr] ){\n dfs(nbr, visited, adj, count);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(const int& n, const vector<vector<int>>& edges) {\n vector<bool> visited(n, false);\n vector<unordered_set<int>> adjNodes(n);\n stack<int> dfs;\n for(const vector<int> &edge : edges){\n const int a = edge[0], b = edg...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n int helper(unordered_map<int, list<int>>& arr, int i, vector<bool>& visit) {\n\n visit[i] = true;\n int ans = 1;\n for (auto x : arr[i]) {\n\n if (!visit[x] ) {\n\n ans += helper(arr, x, visit);\n }\n }\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long dfs(map<int, vector<int>> &graph, int node, set<int> &visited) {\n visited.insert(node);\n long long deps = 1;\n for(int next : graph[node]) {\n if (!visited.count(next)) {\n deps += dfs(graph, next, visited);\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n vector<pair<bool, unordered_set<int>>> adj(n, {false, unordered_set<int>()});\n stack<int> dfs;\n for(const vector<int> &edge : edges){\n const int a = edge[0];\n const int ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\n\n void dfs(unordered_map<int,list<int> >&adj,unordered_map<int,bool >&vis,int vertex,long long &sizeCC){\n vis[vertex]=true;\n\n for(auto neighbour : adj[vertex]){\n if(!vis[neighbour]){\n sizeCC++;\n dfs(adj,vis,neighbour, sizeCC...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void dfs(unordered_map<int,list<int>>& adj,unordered_map<int,bool>& visited,int node,long long& cnt){\n visited[node]=true;\n cnt++;\n for(auto i:adj[node]){\n if(!visited[i]){\n dfs(adj,visited,i,cnt);\n }\n }\...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n\n vector<unordered_set<int>> adjList(n);\n for (const auto &edge : edges) {\n int u = edge[0];\n int v = edge[1];\n adjList[u].insert(v);\n adjList[v].insert(...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n #define ll long long\n const int N = 1e5 + 10;\n vector<ll> parent = vector<ll>(N), size = vector<ll>(N);\n\n void make(ll v) {\n parent[v] = v;\n size[v] = 1;\n }\n\n ll find(ll v) {\n if (...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n vector<int> adjList[n];\n bool visited[n];\n for(int i=0;i<n;i++)\n visited[i]=false;\n for(int i=0;i<edges.size();i++){\n adjList[edges[i][0]].push_back(edges[i][1])...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void BFS(int u, vector<int> adj[], vector<bool>& visited, long long &size){\n queue<int> q;\n q.push(u);\n\n visited[u] = true;\n size++;\n\n while(!q.empty()){\n int x = q.front();\n q.pop();\n\n for(int &v:...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "//Burak Emre Polat\nclass Solution {\npublic:\n vector<vector<int>> adjacency;\n void maker(vector<vector<int>> &edges, int n) {\n unordered_map<int, vector<int>> um;\n for (auto x: edges) {\n um[x[0]].push_back(x[1]);\n um[x[1]].push_back(x[0]);\n }\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "//Burak Emre Polat\nclass Solution {\npublic:\n vector<vector<int>> adjacency;\n void maker(vector<vector<int>> &edges, int n) {\n unordered_map<int, vector<int>> um;\n for (auto x: edges) {\n um[x[0]].push_back(x[1]);\n um[x[1]].push_back(x[0]);\n }\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> adjacency;\n void maker(vector<vector<int>> &edges, int n) {\n unordered_map<int, vector<int>> um;\n for (auto x: edges) {\n um[x[0]].push_back(x[1]);\n um[x[1]].push_back(x[0]);\n }\n for (int i = 0; i ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "//Burak Emre Polat\nclass Solution {\npublic:\n vector<vector<int>> adjacency;\n void maker(vector<vector<int>> &edges, int n) {\n unordered_map<int, vector<int>> um;\n for (auto x: edges) {\n um[x[0]].push_back(x[1]);\n um[x[1]].push_back(x[0]);\n }\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n int bfs(vector<int>adj[], int src, vector<int>&vis)\n {\n queue<int>q;\n q.push(src);\n int cnt=1;\n vis[src]=1;\n while(!q.empty())\n {\n int temp=q.front();\n q.pop();\n for(auto it:adj[temp])\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long bfs( vector<vector<long long>> &adj, vector<long long>&visited, int n, int i){\n queue<int>qu;\n qu.push(i);\n long long cnt=0;\n while(!qu.empty()){\n long long ele = qu.front();\n qu.pop();\n if(!visited...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n\n void bfs(vector<int> adj[],int u,vector<bool> &visited,long long &comp){\n queue<int> q;\n visited[u]=true;\n q.push(u);\n comp++;\n while(!q.empty()){\n int v=q.front();\n q.pop();\n for(int a:adj[v]){\n ...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n int fun(int i,vector<int>&visited,vector<int>adj[]){\n int count=0;\n queue<int>q;\n q.push(i);\n visited[i]=1;\n while(!q.empty()){\n int ni=q.front();\n count++;\n // visited[ni]=1;\n q.pop();\n for(...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n long long countPairs(int n, vector<vector<int>>& edges) {\n vector<int>adj[n];\n for(auto edge:edges){\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n vector<bool>vis(n);\n vector<long long>c...
2,403
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>u...
3
{ "code": "class Solution {\npublic:\n void bfs(vector<int> adj[], vector<long long>& ans, vector<int>& vis, int index) {\n int count = 0; \n vis[index] = 1;\n queue<int> q;\n q.push(index);\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n count++;\n\n for (au...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "template <class K, class T, K maxsize, unsigned char bucket_bits = 1>\nclass static_flat_hash_map {\npublic:\n typedef pair<K, T> value_type;\n\n static constexpr K ZERO_VAL = sizeof(K) == 1 ? CHAR_MIN :\n sizeof(K) == 2 ? SHRT_MIN : sizeof(K) == 4 ? INT_MIN : LONG_LONG_MIN;\n\n static bool...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n\nint reverse(int num){\n int rev=0;\n while(num>0){\n rev=rev*10+num%10;\n num/=10;\n }\n return rev;\n }\n int countNicePairs(vector<int>& nums) {\n int mod=1000000007;\n int len=nums.size();\n for...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\nint MOD=1000000007;\nprivate:\n int reverse(int x)\n {\n int num=0;\n while(x>0)\n {\n num*=10;\n int temp=x%10;\n num+=temp;\n x/=10;\n }\n return num;\n }\npublic:\n int countNicePairs(vector<int>...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n int countNicePairs(std::vector<int>& nums) {\n const int mod = 1000000007;\n int len = nums.size();\n for (int i = 0; i < len; i++) {\n nums[i] = nums[i] - reverse(nums[i]);\n }\n std::sort(nums.begin(), nums.end());\n long...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\nprivate:\n int toMod = 1e9 + 7;\npublic:\n int reverse(int num) {\n int res = 0;\n while(num) { res = res*10 + num%10; num /= 10; }\n return res;\n }\n\n int countNicePairs(vector<int>& nums) {\n ios_base::sync_with_stdio(0),cout.tie(0),cin.tie(0);...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\nclass Solution {\npublic:\n int rev(int n){\n int ans = 0;\n while(n != 0){\n ans = ans * 10 + n % 10;\n n /...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\nprivate:\n int toMod = 1e9 + 7;\npublic:\n inline int reverse(int num) {\n int res = 0;\n while(num) { res = res*10 + num%10; num /= 10; }\n return res;\n }\n\n int countNicePairs(vector<int>& nums) {\n ios_base::sync_with_stdio(0),cout.tie(0),cin....
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\n static const int mod=1e9+7;\npublic:\n int countNicePairs(vector<int>& nums) {\n ios_base::sync_with_stdio(0),cout.tie(0),cin.tie(0);\n vector<int>dif(nums.size());\n int rev,cur,cnt,ans=0;\n for(int i=0;i<nums.size();i++){\n cur=nums[i];\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\n static const int mod=1e9+7;\npublic:\n int countNicePairs(vector<int>& nums) {\n ios_base::sync_with_stdio(0),cout.tie(0),cin.tie(0);\n vector<int>dif(nums.size());\n int rev,cur,cnt,ans=0;\n for(int i=0;i<nums.size();i++){\n cur=nums[i];\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\n static const int mod=1e9+7;\npublic:\n int countNicePairs(vector<int>& nums) {\n ios_base::sync_with_stdio(0),cout.tie(0),cin.tie(0);\n vector<int>dif(nums.size());\n int rev,cur,cnt,ans=0;\n for(int i=0;i<nums.size();i++){\n cur=nums[i];\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n int countNicePairs(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cout.tie(0);\n cin.tie(0);\n const int MOD = 1e9 +7;\n int ans = 0;\n unordered_map<int, long> freq;\n for (int num : nums) {\n int tmp = num -...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n int rev(int n){\n int z=0;\n while(n){\n int d=n%10;\n z=z*10+d;\n n/=10;\n }\n return z;\n }\n int countNicePairs(vector<int>& nums) {\n int c=0;\n for(int i=0;i<nums.size();i++){\n n...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n\nint rev(int n){\n int r=0;\n while(n>0){\n r *= 10;\n r += (n%10);\n n /= 10;\n }\n return r;\n}\n\n int countNicePairs(vector<int>& nums) {\n \n unordered_map<int, int>mp;\n for(int i=0; i<nums.size(); i++){\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n int rev(int n){\n int r =0;\n while(n>0){\n r*=10;\n r+= (n%10);\n n/=10;\n }\n return r;\n }\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
0
{ "code": "class Solution {\npublic:\n int rev(int n)\n {\n int r = 0;\n while(n>0)\n {\n r = r * 10;\n r += (n%10);\n n = n/10;\n }\n return r;\n }\n\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n int co...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
1
{ "code": "class Solution {\npublic:\n\n int rev(int n) {\n int r = 0;\n while (n > 0) {\n r = r * 10 + (n % 10);\n n /= 10;\n }\n return r;\n }\n\n int fact(int n) {\n int f = 1;\n for (int i = 2; i <= n; i++) {\n f *= i;\n }\...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
1
{ "code": "class Solution {\npublic:\n int rev(int n){\n int r=0;\n while(n>0){\n r*=10;\n r+=(n%10);\n n/=10;\n }\n return r;\n }\n int countNicePairs(vector<int>& nums) {\n int n=nums.size();\n int count=0;\n unordered_map<in...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
2
{ "code": "class Solution {\npublic:\n int rev(int n){\n long long int r=0;\n while(n>0){\n r = r*10 + n%10;\n n = n/10;\n }\n return r;\n }\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n //nums[i] + rev(nums[j]) == nums[j] +...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
2
{ "code": "class Solution {\npublic:\n int rev(int n){\n int r =0;\n while(n>0){\n r*=10;\n r+= (n%10);\n n/=10;\n }\n return r;\n }\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n int countNicePairs(vector<int>& arr) {\n int n = arr.size();\n int ans = 0;\n int mod = 1e9 + 7;\n unordered_map<int, int> mp;\n for (int i = 0; i < n; i++)\n {\n string s =...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int countNicePairs(vector<int>& nums) {\n int mod=1e9+7;\n map<int,int>mp;\n for(auto it:nums){\n string s=to_string(it);\n reverse(s.begin(),s.end());\n int rev=stoi(s);\n mp[it-rev]++;\n }\n int ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n int countNicePairs(vector<int>& nums) {\n int res = 0;\n unordered_map<int, int> map;\n int len = nums.size();\n\n for(int i = 0; i < len; i++){\n string str = to_string(nums[i]);\n reverse(str.begin(),...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n long long mod = 1e9+7;\n\n int reversal(int x) {\n string r = to_string(x);\n reverse(r.begin(), r.end());\n return stoi(r);\n }\n\n int countNicePairs(vector<int>& nums) {\n unordered_map<int, int> mpp;\n long long counter = 0; // ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n \n #define ll long long int\n ll mod =1e9+7;\n\n int countNicePairs(vector<int>& nums) {\n \n unordered_map<int,int>mp;\n int ans = 0;\n\n for(auto it : nums)\n {\n int num1 = it;\n string s = to_string(it);\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int countNicePairs(vector<int>& nums) {\n map<int,int> m;\n int n=nums.size();\n int ans=0;\n int mod=1e9+7;\n for(int i=0;i<n;i++){\n int k=nums[i];\n string s=to_string(k);\n reverse(s.begin(),s.end());\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "typedef long long int ll;\n\nconst ll mod = 1e9+7;\n\nclass Solution {\npublic:\n int countNicePairs(vector<int>& arr) {\n \n ll ans = 0;\n unordered_map<ll,ll> mp;\n\n for( ll val: arr) {\n string s = to_string(val);\n reverse( s.begin(), s.end());\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n \n int reverse(int ash)\n {\n string srk=to_string(ash);\n :: reverse(srk.begin(),srk.end());\n int ashna=stoi(srk);\n return ashna;\n }\n \n int countNicePairs(vector<int>& nums) \n {\n long long int kk=0;\n long long int yash=0;\n unor...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int countNicePairs(vector<int>& nums) {\n unordered_map<int,long long>mp;\n for(auto&x:nums)\n {\n string res=to_string(x);\n reverse(res.begin(),res.end());\n mp[x-stoi(res)]++;\n }\n int...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int helper(int n){\n string tem=to_string(n);\n int sz=tem.size();\n sz--;\n int newnum=0;\n while(n>0){\n int ld=n%10;\n newnum += ld*pow(10,sz);\n sz--;\n n/=10;\n }\n return newnum...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\nconst int MOD=1e9+7;\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n vector<int>rev(n);\n int ans=0;\n for(int i=0;i<n;i++){\n int val=nums[i];\n int newval=0,flg=0,p=0;\n while(val){\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n vector<int> rev(n);\n for(int i=0; i<n; i++){\n int m = nums[i];\n int x = 0;\n while(m){\n x *= 10;\n x += m % 10;\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int rev(int n){\n long long int r=0;\n while(n>0){\n r = r*10 + n%10;\n n = n/10;\n }\n return r;\n }\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n vector<int> temp(n);\n //nums[...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\nconst int MOD=1e9+7;\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n vector<int>rev(n);\n int ans=0;\n for(int i=0;i<n;i++){\n int val=nums[i];\n int newval=0,flg=0,p=0;\n while(val){\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\n int reverse(int num) {\n int revNum = 0;\n while (num != 0) {\n revNum = (revNum * 10) + (num % 10);\n num = num / 10;\n }\n return revNum;\n }\n\npublic:\n int countNicePairs(vector<int>& nums) {\n vector<int> revNums(num...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "typedef long long int lli;\nclass Solution {\npublic:\n const int mod=1e9+7;\n int rev(int i){\n int ans=0;\n while(i>0){\n int rem=i%10;\n ans=ans*10+rem;\n i=i/10;\n }\n return ans;\n }\n \n int countNicePairs(vector<int>& nums) ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "typedef long long int lli;\nclass Solution {\npublic:\n const int mod=1e9+7;\n int rev(int i){\n string s=to_string(i);\n reverse(s.begin(),s.end());\n return stoi(s);\n }\n \n int countNicePairs(vector<int>& nums) {\n lli ans=0;\n vector<int> r(nums.size()...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\nint solve(int n){\n string s=to_string(n);\n reverse(s.begin(),s.end());\n int a=stoi(s);\n return a;\n}\n int countNicePairs(vector<int>& nums) {\n int ct=0;\n int n=nums.size();\n vector<int>v(n);\n int mod=1e9+7;\n map<int,int>...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int countNicePairs(vector<int>& nums) {\n vector<int> rev(nums.size());\n for(int i=0;i<nums.size();i++){\n int h=nums[i];\n int k=0;\n while(h>0){\n k=k*10+h%10;\n h=h/10;\n }\n ...
1,925
<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it ...
3
{ "code": "class Solution {\npublic:\n int reverse(int n){\n int r = 0;\n while(n > 0){\n int dig = n%10;\n r = r*10 + dig;\n n /= 10;\n }\n return r;\n }\n\n int countNicePairs(vector<int>& nums) {\n int n = nums.size();\n vector<int...