Unacademy Hiring challenge [Debugging - Cpp - Q2]

I got left by 2 points…AHHH
should have given Lunchtime. :’(

anyone has done debugging java ? what was the problem in that code? is it Multiplication overflow ?

Exchange min and max, and change N to long to prevent overflow.

import java.util.*;
import java.io.*;
class Solution{
	static long find_cost(long N, int hcost, int vcost){
		long res = N*(N-1);
		res *= Math.min(hcost,vcost);
		long temp = (N-1)*(Math.max(hcost,vcost));
		return res + temp;
	}
	public static void main(String[] args)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		while(T-- > 0){
			int[] arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
			int N = arr[0];
			int Ch = arr[1];
			int Cv = arr[2];
			System.out.println(find_cost(N, Ch, Cv));
		}
	}
}

algorithm for minimum removal of edges so that no cycle ?

i got only 40 pts by doing this
how many pts did you got??

and i thought 486 is a good score :frowning:

Same happened with me,I got the verdict before contest was finished but my score didn’t update.

I tried multiple times and eventually got 100. The key is that since score is based on edit distance, I tried changing the minimum number of ints to longs so that the code would not overflow. I ended up having to only change int N to long N in the function prototype, and Java’s implicit typecasting took care of the rest.

The key idea is that an acyclic graph is essentially a forest (i.e.) a bunch of trees. Each tree with x nodes has x-1 edges. So the final number of edges required is
number of vertices - number of trees in final graph

The number of trees in the final graph is equal to the number of connected components in the initial graph. Use DSU to count them.

kk got it i changed all int to long

I think it’s not bad. Have no idea about the score distribution/ranklist though.

i only count the no of back edges and got 100 pts

bool vis[MAX];
void dfs(lli src)
{
    vis[src]=1;
    cnt+=1;
    for(auto xt : adj[src])
    {
        if(!vis[xt])
        {
            dfs(xt);
        }
    }
}

int main(){
fastio
lli t=1;
//cin>>t;
while(t--) {
    lli n,m,k;
    cin>>n>>m;
    loop(i,m)
    {
        lli x,y;
        cin>>x>>y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    
    FOR(i,1,n+1)
    {
        if(!vis[i])
        {
            cnt=0;
            dfs(i);
            m-=(cnt-1);
        }
    }
    print(m);

  }
return 0;
}

cnt is the size of conneted component , now after each call subtract cnt-1 from m , bcz for a tree of n nodes there must be n-1 edges .

At 380 points,do I have any chance of getting a call? I did 2 coding and 2 debugging questions.

My score was 580, no call yet.

2 Likes

Wow really cool, no chance for me then.

6 Likes

What was your score ??

Anyone got called???how many of you got 700 out of 700?

Mine 300 only :frowning_face:

1 Like