allcoding1
27.7K subscribers
2.2K photos
2 videos
74 files
851 links
Download Telegram
Minimum unique sum
Short String
2
All codes are available

Once check it 👇👇


https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
👍1
Sending Largest Array with Equal Number of zeroes code
👎2
// coins game

import java.util.*;

class HelloWorld {

static int helper(int a, int b, int i ,int[]x,int n){

if(i==x.length) return 0;

int left=(x[i]>n)?0:Math.abs(x[i]-a)+helper(x[i],b,i+1,x,n);
int right=(x[i]>n)?0:Math.abs(x[i]-b)+helper(a,x[i],i+1,x,n);
return Math.min(left,right);
}

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int Q=sc.nextInt();
int A=sc.nextInt();
int B=sc.nextInt();
int[]x=new int[Q];
for(int i=0;i<Q;i++){
x[i]=sc.nextInt();
}

System.out.println(helper(A,B,0,x,N));
}
}
👍2
//Equilibrium path


#include <bits/stdc++.h>

using namespace std;

string trim(string str) {
str.erase(0, str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ') + 1);
return str;
}

int solve(int N, vector<int> A) {
int total_sum = accumulate(A.begin(), A.end(), 0);
int left_sum = 0;
int equilibrium_count = 0;

for (int i = 0; i < N; ++i) {
int right_sum = total_sum - left_sum - A[i];
if (left_sum == right_sum) {
equilibrium_count++;
}
left_sum += A[i];
}

return equilibrium_count;
}

int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

string inputline;
getline(cin, inputline);
int N = stoi(trim(inputline));

vector<int> A(N);
for (int j = 0; j < N; j++) {
getline(cin, inputline);
A[j] = stoi(trim(inputline));
}

int result = solve(N, A);
cout << result << endl;

return 0;
}
///find largest swuare linked in

public static int findLargestSquareSize(int[][] samples) {
if (samples == null || samples.length == 0) return 0;

int n = samples.length;
int maxSize = 0;

int[][] dp = new int[n][n];

for (int i = 0; i < n; i++) {
dp[i][0] = samples[i][0];
dp[0][i] = samples[0][i];
maxSize = Math.max(maxSize, dp[i][0]);
maxSize = Math.max(maxSize, dp[0][i]);
}

for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (samples[i][j] == 1) {
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
maxSize = Math.max(maxSize, dp[i][j]);
} else {
dp[i][j] = 0;
}
}
}

return maxSize;
}
👍1
Python
XR Forming code in python
All codes are available

To easily find out ur codes

Once check it 👇👇


https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
Python
periodic strings
👎2
Dividing array
Lighting lamp
👍1
Search ur code

Share with your friends
👍1
def get_answer(N, K, A, S):
from collections import Counter
def min_deletions_to_palindrome(piece):
count = Counter(piece)
odd_count = sum(1 for freq in count.values() if freq % 2 == 1)
return max(0, odd_count - 1)

start = 0
total_deletions = 0

for length in A:
piece = S[start:start + length]
total_deletions += min_deletions_to_palindrome(piece)
start += length

return total_deletions

MINIMAL PALINDROME
👍1
Shortest string code in python

Infosys
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void dfs(int node, int parent, const vector<vector<int>> &adj, const vector<int> &A, int length, int &maxLength) {
maxLength = max(maxLength, length);
for (int neighbor : adj[node]) {
if (neighbor == parent) continue;
if ((A[node] ^ A[neighbor]) < min(A[node], A[neighbor])) {
dfs(neighbor, node, adj, A, length + 1, maxLength);
}
}
}

int main() {
int N;
cin >> N;
vector<int> A(N), P(N);
for (int i = 0; i < N; ++i) cin >> A[i];
for (int i = 1; i < N; ++i) cin >> P[i];

vector<vector<int>> adj(N);
for (int i = 1; i < N; ++i) {
int parent = P[i];
adj[parent].push_back(i);
adj[i].push_back(parent);
}

int maxLength = 0;
dfs(0, -1, adj, A, 1, maxLength);

cout << maxLength << endl;
return 0;
}
Nodes
👍1
Coins game
All codes are available

To easily find out ur codes

Once check it 👇👇


https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>

using namespace std;

int maxTreeScore(int node_count, int edge_count, vector<pair<int, int>>& edges, vector<int>& colors) {
// Step 1: Parse input and create adjacency list
unordered_map<int, vector<int>> adjacency_list;
for (const auto& edge : edges) {
int start = edge.first;
int end = edge.second;
adjacency_list[start].push_back(end);
adjacency_list[end].push_back(start);
}

// Step 2: Calculate depth of each node using BFS
vector<int> node_depth(node_count + 1, -1);
node_depth[1] = 0;
queue<int> bfs_queue;
bfs_queue.push(1);

while (!bfs_queue.empty()) {
int current_node = bfs_queue.front();
bfs_queue.pop();
int current_depth = node_depth[current_node];

for (int neighbor : adjacency_list[current_node]) {
if (node_depth[neighbor] == -1) { // unvisited
node_depth[neighbor] = current_depth + 1;
bfs_queue.push(neighbor);
}
}
}

// Step 3: Group nodes by depth
unordered_map<int, vector<int>> nodes_grouped_by_depth;
for (int node = 1; node <= node_count; ++node) {
nodes_grouped_by_depth[node_depth[node]].push_back(node);
}

// Step 4: Calculate distinct colors per depth
unordered_map<int, int> distinct_colors_at_depth;
for (const auto& pair : nodes_grouped_by_depth) {
int depth = pair.first;
const vector<int>& nodes = pair.second;
unordered_set<int> unique_colors;
for (int node : nodes) {
unique_colors.insert(colors[node - 1]);
}
distinct_colors_at_depth[depth] = unique_colors.size();
}

// Step 5: Dynamic programming to calculate max score
int max_depth = max_element(nodes_grouped_by_depth.begin(), nodes_grouped_by_depth.end(),
[](const auto& a, const auto& b) {
return a.first < b.first;
})->first;
vector<int> dp_score(max_depth + 2, 0);

for (int depth = max_depth; depth >= 0; --depth) {
// Option 1: Move to the next depth without adding score
dp_score[depth] = dp_score[depth + 1];

// Option 2: Add the distinct colors to score and move to depth depth + unique_colors_count
if (distinct_colors_at_depth.find(depth) != distinct_colors_at_depth.end()) {
int unique_colors_count = distinct_colors_at_depth[depth];
if (depth + unique_colors_count <= max_depth) {
dp_score[depth] = max(dp_score[depth], dp_score[depth + unique_colors_count] + unique_colors_count);
} else {
dp_score[depth] = max(dp_score[depth], unique_colors_count);
}
}
}

return dp_score[0];
}
diving in a tree , all test cases are passing

@allcoding1
👍2