r/Competitive_Coding • u/Big-Ear6238 • Nov 29 '24
Need help
I am doing a research on competitive coding Please fill this form
r/Competitive_Coding • u/Big-Ear6238 • Nov 29 '24
I am doing a research on competitive coding Please fill this form
r/Competitive_Coding • u/No_Bath5504 • Oct 02 '24
https://www.codechef.com/START154C/problems/GCDXOR this is the link of the question
now a code is :
using namespace std;
// Function to compute the gcd of two numbers
long long gcd(long long a, long long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
int test;
cin >> test; // Number of test cases
while (test--)
{
int n;
long long k;
cin >> n >> k; // Input size of array and target value k
vector<long long> arr(n); // Input array
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
// Check if all elements are already equal to k
bool same = true;
for (int i = 0; i < n; i++)
{
if (arr[i] != k)
{
same = false;
break;
}
}
if (same)
{
cout << 0 << endl; // No operation needed
}
else
{
// Find the start and end of the range where arr[i] != k
int start = -1, end = n;
for (int i = 0; i < n; i++)
{
if (arr[i] != k)
{
if (start == -1)
start = i;
end = i;
}
}
// If there's only one element not equal to k, we need just one operation
if (start == end)
{
cout << 1 << endl;
}
else
{
bool allXORSame = true, allDivisibleByK = true;
long long firstXOR = (arr[start] ^ k);
// Check if all elements in the range either XOR to the same value or are divisible by k
for (int i = start; i <= end; i++)
{
if (arr[i] % k != 0)
allDivisibleByK = false;
if ((arr[i] ^ k) != firstXOR)
allXORSame = false;
}
if (allXORSame || allDivisibleByK)
{
cout << 1 << endl;
}
else
{
cout << 2 << endl;
}
}
}
}
return 0;
}
now my code is :
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define ll long long
#define vvll vector<vector<ll>>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vpii vector<pii>
#define vll vector<ll>
#define vi vector<int>
#define sum(v) accumulate(all(v), 0LL)
#define f(i, s, e) for (long long int i = s; i < e; i++)
#define cf(i, s, e) for (long long int i = s; i <= e; i++)
#define rf(i, e, s) for (long long int i = e - 1; i >= s; i--)
#define pb push_back
#define ppb pop_back
#define ceildiv(a, b) ((a + b - 1) / b)
#define maxe(v) *max_element(v.begin(), v.end())
#define mine(v) *min_element(v.begin(), v.end())
/* UTILS */
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define py cout << "YES\n"
#define pm cout << "-1\n"
#define pn cout << "NO\n"
#define pl cout << "\n"
#define sp << " "
#define pb push_back
#define po pop_back
#define MOD 1000000007
#define fora(a) for (auto u : a)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define print(var) cout << var << endl
#define take_v(v, a) \
vector<long long> v; \
for (long long i = 0; i < a; i++) \
{ \
long long x; \
cin >> x; \
v.push_back(x); \
}
void solve()
{
ll n, k;
cin >> n >> k;
take_v(v, n);
bool flag = true;
for (auto it : v)
{
if (it != k)
{
flag = false;
}
// else{
// flag = true;
// }
}
if (flag)
{
print(0);
return;
}
ll l = INT_MIN, r = n;
for (int i = 0; i < n; i++)
{
if (v[i] != k)
{
if (l == INT_MIN)
l = i;
r = i;
}
}
if (l == r)
{
cout << 1 << endl;
return;
}
ll firstXOR = (v[l] ^ k);
bool divbyk = true;
bool xsame = true;
for (int i = l; i <= r; i++)
{
if (v[i] % k != 0)
divbyk = false;
if ((v[i] ^ k) != firstXOR)
xsame = false;
if (!xsame && !divbyk)
break;
}
if (xsame)
{
print(1);
return;
}
if (divbyk)
{
print(1);
return;
}
print(2);
return;
}
int main()
{
ios_base
::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++)
{
// cout << "Case #" << t << ": ";
solve();
}
}
here the word firstXOR is same as the code given upper portion.Now please help me if it can give penalty or not?r/Competitive_Coding • u/Aleezapeeza • Apr 22 '24
Hello fellow coders, I have been tasked with hosting a sql query master contest. I am unable to figure out how to create challenges. Can someone please guide me about creating databases for challenges and how will the platform run the participants code against the database. Your assistance would help alot!
r/Competitive_Coding • u/[deleted] • Mar 08 '24
This Lazy Problem setter, decided not to bore you with any story and directly come up to the point. You are given a string consisting of only a
s and b
s , you will have 3 types of queries:
Given 1 L R
all characters in range [L,R], change all a
s to b
s and b
s to a
s .
Given 2 L R
, print YES
if every pair of adjecent characters in range [L,R]
is different.
Given 3 L R
, print YES
if every pair of adjecent characters in range [L,R]
is same.
Note: since length 1 has no adjecent element, the answer for it will always be Yes
First line contains a single integer N
denoting the length of string.
Second line contains the string consisting of only a
and b
.
Third line contains a single integer Q
denoting number of queries.
Next Q lines contain 3 integers - Type, L, R
.
For every Query of Type 2 and Type 3 , Print Yes
or No
respectively. Note That: print "Yes" with all capital and same goes for "No"
1 ≤ N ≤ 2 X 10^5
string S consisting of only a
and b
of length N
1 ≤ Q ≤ 2 X 10^5
1 ≤ Type ≤ 3
1 ≤ L ≤ R ≤ N
5 ababb 8 2 1 3 2 1 5 1 1 4 2 1 5 1 3 3 2 2 4 3 1 3 3 1 1
Yes No Yes No No Yes
r/Competitive_Coding • u/math_code_nerd5 • Feb 25 '24
I mean something that's a bit like a hybrid between "code golf" and demoscene, but rather than being about the code/executable itself being small, are about making the code computationally efficient by making clever approximations.
To illustrate the difference, it may well be that to render a complex scene in a realistic manner, it's shortest in terms of code to "brute force" cast rays at the scene, than it is to utilize symmetries in the scene geometry, cull objects using some sort of tree structure, find patterns in screen space in the way that shadows appear next to objects and attempt to hardcode them into the code, etc. Whereas, the latter might be take more lines of code, but do fewer actual math operations, especially if the answer does not need to be exact and can be approximated. Similarly, there are more complex, but more efficient, ways to factorize, or even multiply, certain matrices than the "default" way, even though that default way is probably still the shortest code because you're literally repeating the same simple operation over and over.
I'm going for something more like the latter--where people go over the top trying to find essentially the "most compact mathematical approximation" of solutions, ways to generate patterns by emergent properties, etc.
r/Competitive_Coding • u/NoField4732 • Aug 29 '23
the link for the problem I'm trying to solve is this: https://codeforces.com/problemset/problem/1399/A
import java.util.*; // Import the Scanner class
import java.util.Arrays;
import java.util.Collections;
import java.lang.Math;
public class MyClass {
public static void main(String args[]) {
int t, t1;
int cnt = 0;
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
t = sc.nextInt();
for(int i = 0; i < t && sc.hasNextInt();i++)
{
cnt = 0;
t1 = 0;
t1 = sc.nextInt();
System.out.println(t1);
int [] arr = new int[t1];
if(t1 == 1) {
System.out.println("YES");
break;
}
else {
for(int l = 0; l < t1; l++)
{
arr[l] = sc.nextInt();
System.out.println(arr[l]);
}
Arrays.sort(arr);
for(int l = 0; l < t1; l++)
{
if(l != 0)
{
if(arr[l] == arr[l -1])
{
arr[l-1] = 0;
cnt++;
Arrays.sort(arr);
l = cnt;
}
else if(arr[l] == (arr[l-1] -1))
{
arr[l-1] = 0;
cnt++;
Arrays.sort(arr);
l = cnt;
}else if(arr[l-1] == (arr[l] -1))
{
arr[l] = 0;
cnt++;
Arrays.sort(arr);
l = cnt;
}
}
}
if((cnt) == (t1 - 1)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
//
}
}
}
r/Competitive_Coding • u/Cool_Boy997 • Jul 10 '23
Hello all, the link for the question is here:
https://codeforces.com/problemset/problem/1845/F
And my code in python is here:l, t = map(int, input().split())n = int(input())velocities = list(map(int, input().split()))
swimmers = list(range(n))positions = [0] * n
my_set = set(positions)
going_forward = Truecounter = 0meet = 0while counter < t:for i in swimmers:if going_forward:if positions[i] + velocities[i] >= l:positions[i] = lgoing_forward = Falseelif 0 <= positions[i]:positions[i] += velocities[i]if not going_forward: # This means that going_forward == Falseif positions[i] - velocities[i] <= 0:positions[i] = 0going_forward = Trueelif positions[i] <= l:positions[i] -= velocities[i]my_set = set(positions)if len(my_set) != len(positions):meet += 1counter += 1print(meet)
Could someone paste and/or explain the solution and/or tell me why i am wrong?
r/Competitive_Coding • u/Capital1586 • Feb 27 '23
Hello!
I have come across this very interesting programming challenge I'm trying to solve, and I have come here to see if anyone had any ideas on how to proceed.
You're tasked to decode an encoding of a square bit matrix of certain size, which contains the number of 0 cells per line, column, diagonal (main and anti-diagonal) and quadrant, as well as the number of transitions per line and column. This encoding isn't injective, that is, it doesn't perfectly map one encoding to one matrix. Thus, your decoder should output how many matrices can be decoded from the given input encoding, and in case there's only one, display it. Note that there may be zero solutions.
The quadrants are defined by the side length divided by two, floored. Here's an example of a 5×5 matrix, with quadrant boundaries highlighted by different digits:
11222
11222
33444
33444
33444Here's an example encoding:
size: 4
line 0s (left to right): 1, 1, 1, 1
columns 0s (left to right): 1, 1, 1, 1
quadrant 0s: top-right=2 top-left=0, bottom-right=2, bottom-left=0
diagonal 0s: main=0, anti=4
line transitions (left to right): 1, 2, 2, 1
column transitions (left to right): 1, 2, 2, 1 And its decoded matrix:
0001
0010
0100
1000The first idea was a simple cell-by-cell backtracking algorithm, however that is extremely slow, as it would have to test an absurd amount of permutations.
I then thought about going line-by-line, generating all the permutations with the given 0s count for each line, and then filtering by the number of transitions. After some thinking and digging, though, I believe I found a neat way of generating just the permutations I need, but I'm not entirely certain I'm on the right track, I will have to think more about it (it does seem feasible, although nontrivial). Either way, it would still be a prohibitive algorithm and wouldn't make the decoder execute under a second for larger matrices (like 20x20). There's a lot of information I'm unable to find a clear use for besides just checking the current state after each try. Columns, diagonals and quadrants are all being underused, it seems.
I feel like this is a fairly standard problem, but I cannot find anything exactly like it. I have tried looking at other problems like sudoku/crosswords solving, 8-queens and the like, to no avail.
What kind of strategies could I employ to make an optimized algorithm? My main issue isn't the implementation details, I find that magnitudes easier to get right than algorithm design.
Thank you.
r/Competitive_Coding • u/Due_Championship_381 • Jan 29 '23
📷
long long int countSubarrWithEqualZeroAndOne(int arr[], int n)
{
long long int count=0;
unordered_map<int, int> mp;
int sum=0;
for(int i=0;i<n;i++)
{
sum+=arr[i];
mp[sum]++;
}
for(auto i:mp)
{
if( i.first!=0 && i.second>1 )
{
count+=i.second-1;
}
if(i.first==0) count+=i.second;
}
return count;
}
r/Competitive_Coding • u/[deleted] • Nov 09 '22
Dear sisters and brothers, i am stuck to this problem on hackerrank , can anyone tell me just algorithm ?
https://www.hackerrank.com/challenges/game-with-cells/problem?isFullScreen=false
r/Competitive_Coding • u/[deleted] • Oct 19 '22
I have been given an n length string and I need to find its rank in the alphabetical ordering of all the n length strings. Like for example, let's say I have been given, "ABC" then as n=3 then ordering will be as {ABC, ABD, ABE ....} thus the rank of "ABC" is 0 (considering we start from 0). Similarly, for "ABD" it will be 1. Now, how do I find the rank of "ZXY"?
On first sight, it looks a recursive problem where we can create a dictionary for the given length of the string (like n=3 for "ZXY" and dict={ABC, ABD, ....}) and then search for the string in the dictionary and return the index as it dict is ordered. My problem is how do I code this up? and Is there a better way to go about this?
Edit: the listing in the question is the alphabetical listing of all n-length strings of letters from {A,...,Z} composed of distinct letters.
r/Competitive_Coding • u/LegComplete2726 • May 25 '21
Hello, i need a good coder that can solve advance programming question. I will pay Rs.500 /hour