All codes are available
Once check it 👇👇
https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
Once check it 👇👇
https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
👍1
// 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));
}
}
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;
}
#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;
}
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
All codes are available
To easily find out ur codes
Once check it 👇👇
https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
To easily find out ur codes
Once check it 👇👇
https://www.instagram.com/allcoding1_official?igsh=ZHJpNXdpeWh1d2No
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
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