Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. An array sorted in ascending order is rotated about a pivot unknown to you. Such an array is referred to as a rotated sorted array or a sorted-pivoted array. For example : [1,2,3,4,5] is a sorted array while [3,4,5,1,2] is a rotated sorted array.
  3.  
  4.  
  5. You are given a rotated sorted array, and some integer values. You have to find each value’s location in the array. If the value is present, return the index in which it is stored ( 0 based indexing) , otherwise if not found return -1.
  6.  
  7.  
  8. Assume the array doesn’t have duplicates.
  9.  
  10. Input format
  11. There are Q+3 lines of input.
  12.  
  13. First line will have a single integer N denoting the size of the array.
  14.  
  15. Second line will contain N space separated integers.
  16.  
  17. Third line will contain a single integer Q denoting the number of targets to be searched..
  18.  
  19. Next Q lines will have a single integer,X in each line denoting the target value. You have to search for each of these target values in turn.
  20.  
  21. Output format
  22. One line per output for each target search, with -1 or the index at which the integer is found.
  23.  
  24. Function Definition
  25. Complete the function search in the code editor for a language of your choice.
  26.  
  27. search has the following parameters :
  28.  
  29. nums : An array of numbers having its values in a rotated sorted order
  30.  
  31. target : An integer representing the number to be searched in nums
  32.  
  33. search returns :
  34.  
  35. int : An integer denoting the index of the target to be searched. If target not present returns -1.
  36.  
  37. Constraints
  38. 1 <= N <= 10^6 where N denotes the size of the input array
  39.  
  40. 1 <= A[i] <= 10^9 where A[i] denotes the ith element of the input array
  41.  
  42. 1 <= Q <= 10^6 where Q denotes the number of targets to be searched
  43.  
  44. 1 <= X <= 10^9 where X denotes the target element to be search
  45.  
  46. Sample Input 1
  47. 7
  48.  
  49. 4 5 6 9 10 2 3
  50.  
  51. 2
  52.  
  53. 3
  54.  
  55. 8
  56.  
  57. Sample Output 1
  58. 6
  59.  
  60. -1
  61.  
  62. Explanation 1
  63. The element 3 is found in the array at index 6. Element 8 is not found in the array, thus -1.
  64.  
  65. Sample Input 2
  66. 6
  67.  
  68. 5 6 8 1 3 4
  69.  
  70. 1
  71.  
  72. 0
  73.  
  74. Sample Output 2
  75. -1
  76.  
  77. Explanation 2
  78. The element 0 is not found in the array.
  79. */
  80.  
  81. /**
  82.  * @param {number} target
  83.  * @param {number[]} nums
  84.  * @return {number}
  85.  */
  86. // reference : https://www.youtube.com/watch?v=5qGrJbHhqFs&t=1s
  87. function search(arr, target) {
  88.   const n=arr.length;
  89.   let low=0;
  90.   let high=n-1;
  91.   while(low<=high){
  92.     let mid=low+Math.floor((high-low)/2);
  93.     if(arr[mid]==target){
  94.       return mid;
  95.     }
  96.     // either of one half will always be sorted,
  97.     // we just half to eliminate one half at a time
  98.     if(arr[low]<=arr[mid]){
  99.       // left half is sorted
  100.       if(target>=arr[low]&&target<=arr[mid]){
  101.         high=mid-1;
  102.       }
  103.       else
  104.       low=mid+1;
  105.     }
  106.     else{
  107.       if(target>=arr[mid]&&target<=arr[high]){
  108.         low=mid+1;
  109.       }
  110.       else
  111.       high=mid-1;
  112.     }
  113.   }
  114.   return -1;
  115. }
  116.  
  117. function main() {
  118.     let n = parseInt(readLine(), 10);
  119.     let nums = readIntArr();
  120.     let q = parseInt(readLine(), 10);
  121.     while (q--) {
  122.         let target = parseInt(readLine(), 10);
  123.         let result = search(nums, target);
  124.         console.log(result);
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement