Uff... Getting error in my solution !?!?!

Here's the question :-
Problem 1: Book Sorting, (K Narayan Kumar, CMI)
Indraneel has to sort the books in his library. His library has one long shelf. His books are numbered 1 through N and he wants to rearrange the books so that they appear in the sequece 1,2, ..., N.
He intends to do this by a sequence of moves. In each move he can pick up any book from the shelf and insert it at a different place in the shelf. Suppose Indraneel has 5 books and they are initially arranged in the order
2 1 4 5 3
Indraneel will rearrange this in ascending order by first moving book 1 to the beginning of the shelf to get
1 2 4 5 3
Then, moving book 3 to position 3, he gets
1 2 3 4 5
Your task is to write a program to help Indraneel determine the minimum number of moves that are necessary to sort his book shelf.
Input format
The first line of the input will contain a single integer N indicating the number of books in Indraneel's library. This is followed by a line containing a permutation of 1, 2, ..., N indicating the intial state of Indraneel's book-shelf.
Output format
A single integer indicating the minimum number of moves necessary to sort Indraneel's book-shelf.
Test Data:
You may assume that 1 ≤ N ≤ 200000. You may also assume that in 50% of the inputs, 1 ≤ N ≤ 5000.
Example:
Here is the sample input and output corresponding to the example discussed above.
Sample Input
5
2 1 4 5 3
Sample Output
2

CPU Timelimit: 3 seconds
Memory limit: 64M

And here's my soltuion:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main(){
    int n,i,t,o,co=0;
    cin>>n;
    int *bl=new int[n];
    for (i=1;i<=n;i++) cin>>bl[i];
    for (i=1;i<=n;i++) {
        if (bl[i]!=i) {
           for (o=1;o<=n;o++) if (bl[o]==i) {t=bl[o];co++;}
           bl[i]=bl[i];
           bl[i]=i;
           
        }
    }
    cout<<co;
}

But i can't understand where i am going wrong ???
Can you Please help me out ?
Thanks in advanced.
1
2
    int *bl=new int[n];
    for (i=1;i<=n;i++) cin>>bl[i];
Your index is out of range. That valid range is [0, n) not (0, n].
Ok , so how to correct it ?
1
2
    int *bl=new int[n];
    for (i=0;i<n;++i) cin>>bl[i];
Last edited on
array indexes are starting with 0, so: for (i=0;i<n;i++)

either that or int *bl=new int[n+1];
Hmm...
I changed my program according to you kbw , but it is still giving me wrong answer when i submit the code (though the example given , it works correctly )
Here's my corrected code :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main(){
    int n,i,t,o,co=0;
    cin>>n;
    int *bl=new int[n];
    for (i=1;i<=n;i++) cin>>bl[i];
    for (i=1;i<=n;i++) {
        if (bl[i]!=i) {
           for (o=1;o<=n;o++) if (bl[o]==i) {t=bl[o];co++;}
           bl[i]=bl[i];
           bl[i]=i;
           
        }
    }
    cout<<co;
}

So now can you tell me why is it wrong or give me corrected code ?
When you access b1[n], you're going outside the bounds of the array.
I corrected what you said kbw & coder777 , but it is still saying "Wrong Answer" !
by replacing int *bl=new int[n]; with int *bl=new int[(n+1)];
You're not getting it. You have to change your for prescription. It's [0,n[, not ]0,n].
Maybe you are right gaminic , i am still not getting what you said !
Which for prescription i have to change , line 7 or 8 ????
And i really dont get what you mean by "It's [0,n[, not ]0,n]."
And i really dont get what you mean by "It's [0,n[, not ]0,n]."

See http://en.wikipedia.org/wiki/Interval_%28mathematics%29.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main(){
    int n,i,t,o,co=0;
    cin>>n;
    int *bl=new int[n];
    for (i=1;i<=n;i++) cin>>bl[i];
    for (i=1;i<=n;i++) {
        if (bl[i]!=i) {
           for (o=1;o<=n;o++) if (bl[o]==i) {t=bl[o];co++;}
           bl[i]=bl[i];  /// what does this line do?
           bl[i]=i;
           
        }
    }
    cout<<co;
}
Last edited on
Oh yes , you are correct , what i am doing is very wrong .
Topic archived. No new replies allowed.