r/Competitive_Coding • • Nov 08 '20

Google Hash Code 2018 Solutions

3 Upvotes

Where can I find Google hash code solutions - specifically 2018 final round : Urban Planning. I am able to see qualifier round solutions in plenty on GitHub but not final round.


r/Competitive_Coding • • Jan 20 '21

Google hash-Code 2021 search for TEAMMATES

5 Upvotes

Hi, I'm Rohan a CS undergrad, and I just started learning and doing coding and DSA and all from last year, So this year as the Google hashcode is coming, so, I and one of my friends are searching for a teammate to join us as a team in the Hash Code, for experience and connecting with new people and to learn a hell lot of things. If you are also up for a new experience as a beginner too, then please dm me, and let's join in this journey together.


r/Competitive_Coding • • Jul 10 '23

Coding help required.

1 Upvotes

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 = True
counter = 0
meet = 0
while counter < t:
for i in swimmers:
if going_forward:
if positions[i] + velocities[i] >= l:
positions[i] = l
going_forward = False
elif 0 <= positions[i]:
positions[i] += velocities[i]
if not going_forward: # This means that going_forward == False
if positions[i] - velocities[i] <= 0:
positions[i] = 0
going_forward = True
elif positions[i] <= l:
positions[i] -= velocities[i]
my_set = set(positions)
if len(my_set) != len(positions):
meet += 1
counter += 1
print(meet)
Could someone paste and/or explain the solution and/or tell me why i am wrong?


r/Competitive_Coding • • Feb 25 '24

Contests that reward clever approximations, "magic" non-straightforward algorithms, etc.

1 Upvotes

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 • • Sep 06 '24

How do i solve this, got this problem in a recent interview, i want to upsolve this.

Thumbnail
gallery
3 Upvotes

r/Competitive_Coding • • Oct 02 '24

i found one code in codechef similar to my code.Especially one term of that code like firstXOR is exactly similar to my code? Is that may lead to penalty or not for plaggrism?

1 Upvotes

https://www.codechef.com/START154C/problems/GCDXOR this is the link of the question

now a code is :

include <iostream>

include <vector>

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 • • Jan 29 '23

Why is this code for finding count of subarrays with zero sum giving wrong answer?Please help me identify where I am going wrong

1 Upvotes

📷

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 • • Feb 27 '23

Decoding a non injective bit matrix encoding

1 Upvotes

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.

The problem

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
33444

Here'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
1000

What I've thought of

The 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 • • Mar 08 '24

Please solve fast

1 Upvotes

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:

  1. Given 1 L R
    all characters in range [L,R], change all a
    s to b
    s and b
    s to a
    s .

  2. Given 2 L R
    , print YES
    if every pair of adjecent characters in range [L,R]
    is different.

  3. 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

Input Format

  • 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
    .

Output Format

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"

Constraints

  • 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

Sample 0

Input

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

Output

Yes No Yes No No Yes


r/Competitive_Coding • • Apr 22 '24

Creating a Contest

1 Upvotes

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 • • Aug 29 '23

Please tell me why my code is not working!

1 Upvotes

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 • • Aug 21 '24

sum of unitary fraction as close as possible to 1

2 Upvotes

hi, i'm struggling with trying to code this problem without using an algortihm that is O(2**n).
the problem is: you're given the first n unitary fraction (1/2, 1/3, 1/4......1/n) and you need to find a way to sum them so as you can make the greater number smaller than one. for example, if n is 3 the answer is 1/2+1/3, for n=5 the answer is 1/3+1/4+1/5+1/5 (you can use the same fraction as many times as you want)
i really tried but got only solution in O(2**n), and can't really see a way to resolve this in a polinomial complexity (i don't even know if it's possible)
every help would be appreciate (actually i mind only the first 100 fractions but if someone can generalize the problem it would be great, i personally think the answer rely probably in some mathematical properties that i'm unaware of)


r/Competitive_Coding • • Feb 19 '23

Competitive chess coding

Thumbnail chess.dev
1 Upvotes

r/Competitive_Coding • • Oct 19 '22

How do I find the position of a string in the modified alphabetical ordering?

1 Upvotes

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 • • Jan 09 '22

Hey everyone, I am from India, I have created a community for healthy discussions on competitive programming, I'd love to have u guys there too, right now there are only 2 members.

Thumbnail reddit.com
3 Upvotes

r/Competitive_Coding • • Jan 07 '22

How to build a solid foundation for competitive programming (books preferred)

2 Upvotes

Hello ! I got interested in competitive programming pretty late i.e after 4 months joining my first software engineering job . I realised that I suck at implementation of algorithms , so even though I know djisktra will be useful to solve this , i cant implement it even though I understand it.
I want to build a solid foundation where I thoroughly understand and can implement important Data Structures and Algorithms in C++ .I would prefer a book , I like using pen and paper to solve so kindly recommend such books .
Thanks a ton !!
I am comfortable with Python but C++ is preferred and it would make me write better programs I believe.


r/Competitive_Coding • • Jun 03 '22

Doubt! Please someone help

1 Upvotes

I was solving this problem on CF.

I didn't got the idea so i looked the leaderboard.

I saw this solution but I am unable to get what this guy has done.

Can someone please tell me what this guy is doing to solve this problem??


r/Competitive_Coding • • May 09 '21

Need help with this GFG problem

2 Upvotes

I am trying to write the memoized or recursive code for this gfg problem which asks us to find find-length-longest-subsequence-one-string-substring-another-string. I did manage to find this -

static int count(String a, String b, int m, int n)
{ 
    if (n==0 || m == 0) return 0;
    
    count(a,b,m-1,n); 
    count(a,b,m, n-1);

    if(t[m][n] != -1) return t[m][n];

    if (a.charAt(m - 1) == b.charAt(n - 1)) 
        t[m][n] = 1 + count(a, b, m - 1, n - 1); 
    else 
        t[m][n] =  count(a, b, m-1, n);
    
    if(max > t[m][n])
         max = t[m][n];
    
    return t[m][n]; 
}

I think this may cross O(m*n) too right? Is there a better approach?

count(a,b,m-1,n);
count(a,b,m, n-1);

If there is a better memoized or recursive solution, kindly share the same. Thanks in advance!


r/Competitive_Coding • • May 25 '21

Help in competitive programming

1 Upvotes

Hello, i need a good coder that can solve advance programming question. I will pay Rs.500 /hour


r/Competitive_Coding • • Jul 14 '21

FaaS Wars II - Serverless Robot Coding Challenge

2 Upvotes

Hi guys,

Here is a serverless robot coding hackathon by Nimbella where you can code your robots in Java and defeat other robots. The final round will take place on the 26th of July and the prize amount is $800. It is a good chance to learn serverless, participate in a hackathon, and win cash prizes.

You can participate here: https://www.hackerearth.com/challenges/hackathon/faas-wars-season-2/ or code your robots directly here: https://nimbots-apigcp.nimbella.io/

To learn how to code your robot here is a tutorial:https://www.youtube.com/watch?v=sh8iAE5hrt0


r/Competitive_Coding • • Nov 09 '22

Stuck with Army Game Coding Problem (Help me please !)

1 Upvotes

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 • • Jan 10 '22

Hey everyone, hope a lot of u are giving Google Hashcode. I do not have a team yet but I wanna take part. Also, I would like to mention that I am a Div 3, still learning things. Please dm me if I can join ur team. I'll be grateful, it will be a learning opportunity for me.

5 Upvotes