`{"id":"c5ce3c87-4cec-40e5-884e-2806dd737703","name":"Searching An Element In A Sorted Array","description":"Given a sorted array of size N and an integer, K. Check if K is present in the array or not using ternary search.\r\nTernary Search - It is a divide and conquers algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element).","inputFormat":"The first line contains two integers N and K denoting the size of the array and the element to be searched.\r\nThe second line contains n space separated integers a[1],a[2]...a[n].","outputFormat":"If the element is present in the array then print \"Found\" else print \"Not Found\"","constraints":"1 &lt;= N &lt;= 10^6\r\n1 &lt;= K &lt;= 10^6\r\n1 &lt;= arr[i] &lt;= 10^6","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.util.Scanner;\r\n\r\npublic class Main {\r\n static void ternarySearch(int arr[], int k)\r\n {\r\n //Code Here\r\n }\r\n\r\n\r\n //================================//\r\n public static void main(String[] args) {\r\n Scanner sc = new Scanner(System.in);\r\n\r\n int n = sc.nextInt();\r\n int k = sc.nextInt();\r\n\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = sc.nextInt();\r\n }\r\n\r\n ternarySearch(arr, k);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"5 2\r\n1 2 3 4 5","sampleOutput":"Found","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ccdbbb91-d392-43f3-8065-0ecd4a077535","name":"Ternary Search For Experts","slug":"ternary-search-for-experts-998","type":0},{"id":"86633a85-3aac-464d-abc2-414f046cfbf2","name":"Searching An Element In A Sorted Array","slug":"searching-an-element-in-a-sorted-array","type":1}],"next":{"id":"fa9e6a05-a9a3-4337-bf90-ae28f5ad6ae8","name":"Finding Mino","type":1,"slug":"finding-mino"},"prev":null}}`

# Searching An Element In A Sorted Array

Given a sorted array of size N and an integer, K. Check if K is present in the array or not using ternary search. Ternary Search - It is a divide and conquers algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element).

`{"id":"c5ce3c87-4cec-40e5-884e-2806dd737703","name":"Searching An Element In A Sorted Array","description":"Given a sorted array of size N and an integer, K. Check if K is present in the array or not using ternary search.\r\nTernary Search - It is a divide and conquers algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element).","inputFormat":"The first line contains two integers N and K denoting the size of the array and the element to be searched.\r\nThe second line contains n space separated integers a[1],a[2]...a[n].","outputFormat":"If the element is present in the array then print \"Found\" else print \"Not Found\"","constraints":"1 &lt;= N &lt;= 10^6\r\n1 &lt;= K &lt;= 10^6\r\n1 &lt;= arr[i] &lt;= 10^6","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.util.Scanner;\r\n\r\npublic class Main {\r\n static void ternarySearch(int arr[], int k)\r\n {\r\n //Code Here\r\n }\r\n\r\n\r\n //================================//\r\n public static void main(String[] args) {\r\n Scanner sc = new Scanner(System.in);\r\n\r\n int n = sc.nextInt();\r\n int k = sc.nextInt();\r\n\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = sc.nextInt();\r\n }\r\n\r\n ternarySearch(arr, k);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"5 2\r\n1 2 3 4 5","sampleOutput":"Found","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ccdbbb91-d392-43f3-8065-0ecd4a077535","name":"Ternary Search For Experts","slug":"ternary-search-for-experts-998","type":0},{"id":"86633a85-3aac-464d-abc2-414f046cfbf2","name":"Searching An Element In A Sorted Array","slug":"searching-an-element-in-a-sorted-array","type":1}],"next":{"id":"fa9e6a05-a9a3-4337-bf90-ae28f5ad6ae8","name":"Finding Mino","type":1,"slug":"finding-mino"},"prev":null}}`

Editor

# Searching An Element In A Sorted Array

easy

Given a sorted array of size N and an integer, K. Check if K is present in the array or not using ternary search. Ternary Search - It is a divide and conquers algorithm that can be used to find an element in an array. It is similar to binary search where we divide the array into two parts but in this algorithm, we divide the given array into three parts and determine which has the key (searched element).

## Constraints

1 <= N <= 10^6 1 <= K <= 10^6 1 <= arr[i] <= 10^6

## Format

### Input

The first line contains two integers N and K denoting the size of the array and the element to be searched. The second line contains n space separated integers a[1],a[2]...a[n].

### Output

If the element is present in the array then print "Found" else print "Not Found"

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 2 1 2 3 4 5```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}Found`

Discussions

Show Discussion

Related Resources