Short Palindrome Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Can you help me here . I am not passing a few test cases
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticintdfs(intnode,intn,intg[][],HashMaphm,booleanvisited[]){intc=1;visited[node]=true;for(inti=1;i<=n;i++){if(!visited[i]&&g[node][i]==1)c+=dfs(i,n,g,hm,visited);}hm.put(node,c);returnc;}publicstaticvoidmain(String[]args){intn,m;Scannersc=newScanner(System.in);n=sc.nextInt();m=sc.nextInt();intarr[][]=newint[n+1][n+1];booleanvisited[][]=newboolean[n+1][n+1];booleantest[]=newboolean[n+1];for(inti=0;i<m;i++){inta=sc.nextInt();intb=sc.nextInt();arr[a][b]=1;arr[b][a]=1;}intcount;HashMaphm[]=newHashMap[n+1];for(inti=0;i<=n;i++)hm[i]=newHashMap();//FOR LOOP 1: For Storing value of each dfs in hashmap and if odd nodes edge is only 1 cases truefor(inti=1;i<=n;i++){dfs(i,n,arr,hm[i],visited[i]);count=0;for(intj=1;j<=n;j++)if(arr[i][j]==1&&(int)(hm[i].get(j))%2==1)count++;if(count==1)test[i]=true;}//FOR LOOP 2: for each passing test convert the even node edge as -1 for(inti=1;i<=n;i++){if(test[i]){for(intj=1;j<=n;j++){if(arr[i][j]==1&&(int)(hm[i].get(j))%2==0){arr[i][j]=-1;arr[j][i]=-1;}}}}// Count all -1 and divide by 2intcountt=0;for(inti=1;i<=n;i++){for(intj=1;j<=n;j++)if(arr[i][j]==-1)countt++;}System.out.println(countt/2);/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Short Palindrome
You are viewing a single comment's thread. Return to all comments →
Can you help me here . I am not passing a few test cases