Company:
MakemyTrip, Intuit, Hike, Flipkart, De-Shaw, BankBazaar, Amazon, Adobe, TimesInternet, Snapdeal, Paytm, Microsoft.
Question:
Given a sorted and rotated array (rotated at some point) A[ ], and given an element K, the task is to find the index of the given element K in the array A[ ]. The array has no duplicate elements. If the element does not exist in the array, print -1.
Input:
The first line of the input contains an integer T, depicting the total number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the size of the given array. Second line of each test case contains N space separated integers denoting the elements of the array A[ ]. Third line of each test case contains an integer K denoting the element to be searched in the array.
The first line of the input contains an integer T, depicting the total number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the size of the given array. Second line of each test case contains N space separated integers denoting the elements of the array A[ ]. Third line of each test case contains an integer K denoting the element to be searched in the array.
Output:
Corresponding to each test case, print in a new line, the index of the element found in the array. If element is not present, then print -1.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100005
0 ≤ A[i] ≤ 10000005
1 ≤ k ≤ 100005
1 ≤ N ≤ 100005
0 ≤ A[i] ≤ 10000005
1 ≤ k ≤ 100005
Example:
Input
3
9
5 6 7 8 9 10 1 2 3
10
3
3 1 2
1
4
3 5 1 2
6
3
9
5 6 7 8 9 10 1 2 3
10
3
3 1 2
1
4
3 5 1 2
6
Output
5
1
-1
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
//code
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for(int i=0;i<m;i++){
int n = sc.nextInt();
int arr[]= new int [n];
for (int j=0;j<n;j++){
arr[j]=sc.nextInt();
}
int element = sc.nextInt();
int count=0;
for(int k=0;k<n;k++){
if(arr[k]==element){
System.out.println(k);
}else {count++;}
}
if(count==n){System.out.println("-1");}
}
}
}
Comments
Post a Comment