๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
bool prime[100005];
int dp[100005];
ll answer;
bool visited[100005];
vector<int>adjacent[100005];
void dfs1(int u)
{
visited[u] = true;
int sum = prime[u] ? 0 : 1;
for(int i = 0 ; i < adjacent[u].size() ; i++)
{
int v = adjacent[u][i];
if(!visited[v])
{
dfs1(v);
sum += dp[v];
}
}
dp[u] = sum;
if(prime[u]) dp[u] = 0;
}
void dfs2(int u, int p, int dv)
{
visited[u] = true;
if(prime[u])
{
vector<ll>pp;
ll sum = dv;
pp.push_back(dv);
for(int i = 0 ; i < adjacent[u].size() ; i++)
{
int v = adjacent[u][i];
if(!visited[v] && v != p)
{
dfs2(v, u, 0);
pp.push_back(dp[v]);
sum += dp[v];
}
}
ll val = 0;
for(int i = 0 ; i < pp.size() ; i++)
{
val += (sum-pp[i])*pp[i];
}
val /= 2;
answer += val;
answer += sum;
}
else
{
for(int i = 0 ; i < adjacent[u].size() ; i++)
{
int v = adjacent[u][i];
if(!visited[v] && v != p)
{
dfs2(v, u, dv + dp[u] - dp[v]);
}
}
}
}
void Sieve()
{
for(int i = 1 ; i <= 100000 ; i++)
{
prime[i] = true;
}
prime[1] = false;
for(int i = 2 ; i*i <= 100000 ; i++)
{
if(prime[i])
{
for(int j = i*i ; j <= 100000 ; j += i)
{
prime[j] = false;
}
}
}
}
int main(){
Sieve();
int n;
cin>>n;
assert(1 <= n && n <= 100000);
for(int i = 0 ; i < n - 1 ; i++)
{
int u,v;
cin>>u>>v;
assert(1 <= u && u <= n);
assert(1 <= v && v <= n);
adjacent[u].push_back(v);
adjacent[v].push_back(u);
}
dfs1(1);
for(int i = 1 ; i <= n ; i++) visited[i] = false;
dfs2(1,0,0);
cout<<answer<<endl;
}
prime pairsโ
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Coinswitch is hiring SDE Intern (Apply Fast!)
For 2025 grads
https://recruiterflow.com/coinswitch/jobs/496
For 2025 grads
https://recruiterflow.com/coinswitch/jobs/496
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Pando is hiring for QA Manual Intern
Experience: 0 - 1 year
Expected Stipend: competitive
Apply here: https://careers.pando.ai/jobs/Careers/727224000009432292/QA-Manual-Intern?source=CareerSite
Experience: 0 - 1 year
Expected Stipend: competitive
Apply here: https://careers.pando.ai/jobs/Careers/727224000009432292/QA-Manual-Intern?source=CareerSite
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Company: Zenskar
Role: AI Engineering Intern
Remote
Apply: https://wellfound.com/jobs/3150781-ai-engineering-intern
Role: AI Engineering Intern
Remote
Apply: https://wellfound.com/jobs/3150781-ai-engineering-intern
Wellfound
AI Engineering Intern at Zenskar โข India โข Bengaluru โข Remote (Work from Home)
Zenskar is hiring a AI Engineering Intern in India and Bengaluru - Apply now on Wellfound!
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
https://surveys.infosysapps.com/r/a/SPCampusregistration_NATCOLLEGES25batch
Company:-Infosys
Package:- 9.5 lpa
Role:- SP
Good opportunity โ โ๏ธ
Apply assap โ โ๏ธ
Batches:-2023/2024/2025
Company:-Infosys
Package:- 9.5 lpa
Role:- SP
Good opportunity โ โ๏ธ
Apply assap โ โ๏ธ
Batches:-2023/2024/2025
Infosysapps
Infosys | Surveys
Create Surveys. Collect feedback through a computer assisted, Mobile friendly method. Design Surveys, Send Request , Analyze response and more ...
Forwarded from OffCampus Jobs | OnCampus Jobs | Daily Jobs Updates | Lastest Jobs | All Jobs | CSE Jobs | Fresher Jobs โฅ (Dushyant)
Instahyre
SDE 1 job at GreenStitch.io - Instahyre
GreenStitch.io is looking for a SDE 1 in Bangalore with 2-6 years of experience in Full-Stack Development, Node.js, React.js, etc. Apply today and get your dream job at GreenStitch.io!
Today anyone give exam Visa /Google just React
#include <vector>
#include <iostream>
using namespace std;
int countConnections(const vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
int count = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 1) {
if (i - 1 >= 0 && j - 1 >= 0 && matrix[i - 1][j - 1] == 1) {
count++;
}
if (i - 1 >= 0 && matrix[i - 1][j] == 1) {
count++;
}
if (i - 1 >= 0 && j + 1 < n && matrix[i - 1][j + 1] == 1) {
count++;
}
if (j + 1 < n && matrix[i][j + 1] == 1) {
count++;
}
}
}
}
return count;
}
Counting Connections in matrix โ
def solve(transactions, userHistory):
preferredRegion, minValue, maxValue, minScore, maxScore, accountLimit = userHistory
scrutinized = []
for transaction in transactions:
_, region, value, score, _ = transaction
if value <= accountLimit:
match_score = 0
if region == preferredRegion:
match_score += 2
if minValue <= value < maxValue:
match_score += 3
if minScore <= score <= maxScore:
match_score += 2
scrutinized.append(match_score)
scrutinyScore = 0
n = len(scrutinized)
for i in range(n):
for j in range(i + 1, n):
scrutinyScore += abs(scrutinized[i] - scrutinized[j])
return scrutinyScore
Banking Management -I โ
UKG
def solve():
n = int(input())
a = list(map(int, input().split()))
from statistics import median as mm
pos = {}
for i, v in enumerate(a):
if v not in pos:
pos[v] = []
pos[v].append(i)
result = []
for v, indices in pos.items():
indices.sort()
m = len(indices)
mid_index = mm(indices)
s = sum(a[i] for i in indices)
result.append((mid_index, s))
result.sort()
print([x[1] for x in result])
solve()
Shrink the array โ
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int largestSquareArea(vector<int>& cityLine) {
int n = cityLine.size();
stack<int> st;
int maxSquareArea = 0;
vector<int> left(n), right(n, n);
for (int i = 0; i < n; ++i) {
while (!st.empty() && cityLine[st.top()] >= cityLine[i]) {
st.pop();
}
left[i] = st.empty() ? -1 : st.top();
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n - 1; i >= 0; --i) {
while (!st.empty() && cityLine[st.top()] >= cityLine[i]) {
st.pop();
}
right[i] = st.empty() ? n : st.top();
st.push(i);
}
for (int i = 0; i < n; ++i) {
int height = cityLine[i];
int width = right[i] - left[i] - 1;
int side = min(height, width);
maxSquareArea = max(maxSquareArea, side * side);
}
return maxSquareArea;
}
Visa โ
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int largestSquareArea(vector<int>& cityLine) {
int n = cityLine.size();
stack<int> st;
int maxSquareArea = 0;
vector<int> left(n), right(n, n);
for (int i = 0; i < n; ++i) {
while (!st.empty() && cityLine[st.top()] >= cityLine[i]) {
st.pop();
}
left[i] = st.empty() ? -1 : st.top();
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n - 1; i >= 0; --i) {
while (!st.empty() && cityLine[st.top()] >= cityLine[i]) {
st.pop();
}
right[i] = st.empty() ? n : st.top();
st.push(i);
}
for (int i = 0; i < n; ++i) {
int height = cityLine[i];
int width = right[i] - left[i] - 1;
int side = min(height, width);
maxSquareArea = max(maxSquareArea, side * side);
}
return maxSquareArea;
}
Visa โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
while (T-- > 0) {
int N = scanner.nextInt();
int M = scanner.nextInt();
int K = scanner.nextInt();
Map<String, Integer> rowCount = new HashMap<>();
for (int i = 0; i < N; i++) {
StringBuilder rowPattern = new StringBuilder();
for (int j = 0; j < M; j++) {
int val = scanner.nextInt();
rowPattern.append(val);
}
String pattern = rowPattern.toString();
rowCount.put(pattern, rowCount.getOrDefault(pattern, 0) + 1);
}
int maxEqualRows = 0;
Set<String> processedPatterns = new HashSet<>();
for (String pattern : rowCount.keySet()) {
if (processedPatterns.contains(pattern)) {
continue;
}
StringBuilder complementBuilder = new StringBuilder();
for (int i = 0; i < pattern.length(); i++) {
complementBuilder.append(pattern.charAt(i) == '0' ? '1' : '0');
}
String complement = complementBuilder.toString();
int countPattern = rowCount.get(pattern);
int countComplement = rowCount.getOrDefault(complement, 0);
int inversionsNeeded = Math.min(countPattern, countComplement);
if (inversionsNeeded <= K) {
int totalRows = countPattern + countComplement;
maxEqualRows = Math.max(maxEqualRows, totalRows);
}
processedPatterns.add(pattern);
processedPatterns.add(complement);
}
System.out.println(maxEqualRows);
}
scanner.close();
}
}
Equal Rows โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T-- > 0) { int N = scanner.nextInt(); int Mโฆ
from collections import defaultdict
def solve(N, M, K, mat):
rf, irf = defaultdict(int), defaultdict(int)
for i in range(N):
r = tuple(mat[i])
ir = tuple(1 - x for x in mat[i])
rf[r] += 1
irf[ir] += 1
res = 0
for r, c in rf.items():
res = max(res, c)
if K > 0:
res = max(res, c + min(K, irf[r]))
return res
Equal Rows โ
#include <vector>
#include <iostream>
using namespace std;
int longestDiagonalSegment(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
int longest = 0;
int dR[] = {-1, 1, -1, 1};
int dC[] = {1, 1, -1, -1};
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
for (int dir = 0; dir < 4; ++dir) {
int len = 0;
int rPos = r, cPos = c;
int pattern[] = {1, 2, 0, 2, 0, 2, 0};
int pIdx = 0;
while (rPos >= 0 && rPos < rows && cPos >= 0 && cPos < cols) {
if (matrix[rPos][cPos] == pattern[pIdx]) {
++len;
pIdx = (pIdx + 1) % 7;
} else {
break;
}
rPos += dR[dir];
cPos += dC[dir];
}
longest = max(longest, len);
}
}
}
return longest;
}
Visa โ
def canonical_form(s):
mapping = {}
form = []
next_index = 0
for char in s:
if char not in mapping:
mapping[char] = next_index
next_index += 1
form.append(mapping[char])
return tuple(form)
def solve(N, Q, S, K):
canonical_map = {}
for string in S:
form = canonical_form(string)
if form not in canonical_map:
canonical_map[form] = 0
canonical_map[form] += 1
result = []
for query in K:
query_form = canonical_form(query)
result.append(canonical_map.get(query_form, 0))
return result
Google โ
mapping = {}
form = []
next_index = 0
for char in s:
if char not in mapping:
mapping[char] = next_index
next_index += 1
form.append(mapping[char])
return tuple(form)
def solve(N, Q, S, K):
canonical_map = {}
for string in S:
form = canonical_form(string)
if form not in canonical_map:
canonical_map[form] = 0
canonical_map[form] += 1
result = []
for query in K:
query_form = canonical_form(query)
result.append(canonical_map.get(query_form, 0))
return result
Google โ