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.
This code is working for all test cases...if any1 hav any otr idea plz share...thanks.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int t[] = new int[n];
for(int i=0; i < n; i++){
t[i] = in.nextInt();
}
int sum=t[0];
for(int j=1;j=t[j])
sum=sum+1;
else
sum=t[j];
}
System.out.println(sum);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Party
You are viewing a single comment's thread. Return to all comments →
This code is working for all test cases...if any1 hav any otr idea plz share...thanks.
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int t[] = new int[n]; for(int i=0; i < n; i++){ t[i] = in.nextInt(); } int sum=t[0];
for(int j=1;j=t[j]) sum=sum+1; else sum=t[j]; } System.out.println(sum); }