Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 662 |

@arpitsatnalika | 629 |

@code_it_ | 591 |

@prakharxyz | 583 |

@tujhekya | 542 |

@Rahul Jadhav 1 | 458 |

@sivanandh03 | 448 |

@satyam0460 | 433 |

@iamronak | 424 |

@imnm | 424 |

@18eumc107 | 421 |

Complete Leaderboard |

Given an array **Arr** of **N** elements and a integer **K**. Your task is to return the position of **first occurence** of **K** in the given array.

**Note:** Position of first element is considered as 1.

**Example 1:**

**Input:
**N = 5, K = 16
Arr[] = {9, 7, 2, 16, 4}
**Output:** 4
**Explanation:** K = 16 is found in the
given array at position 4.

**Example 2:**

**Input:
**N = 7, K = 98
Arr[] = {1, 22, 57, 47, 34, 18, 66}
**Output:** -1
**Explanation:** K = 98 isn't found in
the given array.

**Your Task:**

Complete the function **search()** which takes an array **arr,** two integers **n **and** k**, as input parameters and returns an integer denoting the answer. Return **-1** if the number is not found in array. You don't to print answer or take inputs.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= N <= 10^{6}

1 <= K <= 10^{6}

1 <= Arr[i] <= 10^{6}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Searching a number

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.