Help rahul pivot solution

https://hack.codingblocks.com/app/practice/2/1044/problem

here is my different approach to solve the question but my

test case 0 got passed

test case 1 is showing run error

#include<iostream>
#include<algorithm>

using namespace std;

int main() {
	int n;
	cin>>n;
	int arr[100];
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	int key;
	cin>>key;

	auto it=find(arr,arr+n,key);
	int index=it-arr;

	    if(index==n)
        cout<<-1<<endl;
    else
        cout<<index;
    
	return 0;
}

@jdgsijafw print endl after you print index

I changed

int arr[100];

to

int arr[n];

It worked!!!

hi @jdgsijafw since constraints are not given you dont know how many elements there will be in the array, so it is always safer to use dynamic memory allocation or vectors.
Please mark your doubt as resolved in case of no further queries :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.