Solving a question from codechef..runtime error :/

All bandits are afraid of Sheriff. Sheriff constantly fights crime, but when bandits lay low, he gets bored and starts to entertain himself.
This time Sheriff gathered all the bandits in his garden and ordered them to line up. After the whistle all bandits should change the order in which they stand.
Sheriff gave all the bandits numbers from 1 to N. For each place i he determined the unique position j. After whistling the bandit staying on position i should run to the j-th position. Sheriff loved seeing how the bandits move around, and he continued whistling until the evening. He finished the game only when he noticed that the bandits are in the same order in which they were standing originally.
Now the Sheriff asks the question: How many times has he whistled?

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of bandits. The second line contains N space-separated integers A1, A2, ..., AN denoting that the bandit staying on position i should run to the Ai-th position after the whistle.

Output

For each test case, output a single line containing number of times the sheriff had to whistle, print it modulo 10^9 + 7.

Constraints

1 ≤ T ≤ 5
1 ≤ N ≤ 100000
All Ai are distinct numbers from 1 to N

Example
Input:

2
3
1 2 3
5
2 3 1 5 4

Output:

1
6


Explanation



Example case 2.
the bandits positions are:
0. 1 2 3 4 5
1. 3 1 2 5 4
2. 2 3 1 4 5
3. 1 2 3 5 4
4. 3 1 2 4 5
5. 2 3 1 5 4
6. 1 2 3 4 5.

Now, I did not incorporate the "number of test cases option & have taken input of integers in a diff order..Pls help me with the code..my code is as follows, it has a runtime error..
#include <iostream>
#include <string>
using namespace std;
class bandits
{
public:
int *a,*ac,*t;
int size;

int count;
bandits()
{
count=0;
}
void makecopy(int sz)
{
size=sz;
a=new int[size+1];
a[0]=0;ac[0]=0;
for(int i=0;i<(size+1);i++)
{
cout<<"enter no"<<endl;
cin>>a[i+1];
a[i+1]=ac[i+1];
}
findcount();
}
int check()
{
int i=1,j=1;
for(int x=0;x<(size+1);x++)
{
if(a[i]==ac[j])
{
i++,j++;
}
else{ return 0;}

}
return 1;
}
void findcount()
{
for(int x=0;x<(size+1);x++)
{
if(count==0 || check()!=1)
{
int i=0,j=0,k=0;
j=a[i];
k=ac[i];
t[k]=j;
}
else{ cout<<count<<endl;
break;}
for(int y=0;y<(size+1);y++)
{
a[y+1]=t[y+1];
}
count++;
}
cout<<count<<endl;
}
};
int main()
{
bandits b;

int s;
cout<<"how many numbers do you wanna enter?"<<endl;
cin>>s;
b.makecopy(s);



return 0;
}
Line 16: You're trying to store into ac[0], but ac has never been allocated.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Thank you, now the code is as follows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
using namespace std;
class bandits
{
public:
int *a,*ac,*t;
int size;
int count;
bandits()
{
 count=0,size=0;
}
void makecopy(int si)
{
size=si;
a=new int[size];
t=new int[size];
ac=new int[size];
a[0]=0;ac[0]=0;
for(int i=0;i<(size);i++)
{
cout<<"enter no"<<endl;
cin>>a[i+1];
ac[i+1]=a[i+1];
}
findcount();
}
int check()
{
 int i=1,j=1;
for(int x=0;x<(size+1);x++)
 {
    if(a[i]==ac[j])
	{
	  i++,j++;
	}
	else{ return 0;}
	
  }
  return 1;
}
void findcount()
{
  int i=1,j=0,k=0,f=0;;
  do{
 for(int x=0;x<(size);x++)
 {
    if(count==0 || check()!=1)
     {
		j=a[i];
		k=ac[i];
		t[k]=j;
		i++;
     }	
    else { f=1; }	 
 }
	 for(int y=0;y<(size+1);y++)
	 {
	   a[y+1]=t[y+1];
	 }
	 count++;}while(f!=1);
  
  cout<<count<<endl;
}
};
int main()
{
bandits b;

int s;
cout<<"how many numbers do you wanna enter?"<<endl;
cin>>s;
b.makecopy(s+1);



	return 0;
	}
Is there a question here?

Does the code work? If so, please mark the thrad as solved.
If not, please be specific about what is not working.
To be specific, the code has no compile time errors...as soon as take the input input at line 24, the loop is working fine..just after the execution of the loop, a runtime error is occurring.
http://www.cplusplus.com/forum/general/112111/

1
2
3
4
5
6
7
8
9
10
11
cin>>s;
b.makecopy(s+1); //¿?


ac=new int[size];
for(int i=0;i<(size);i++)
{
   cout<<"enter no"<<endl;
   cin>>a[i+1]; //¿?
   ac[i+1]=a[i+1];
}
please explain your intention in the lines marked with ¿?
I suppose that you do know that valid index goes from 0 to size-1

http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.