Given an unsorted array of n elements, find if the element k is present in the array or not - Yourcodeway

Given an unsorted array of n elements, find if the element k is present in the array or not. Complete the findNumber function in the editor below. It has 2 parameters:

  1. An array of integers, arr, denoting the elements in the array.
  2. An integer, k, denoting the element to be searched in the array
The function must return a string "YES" or "NO" denoting if the element is present in the array or not.


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class Solution {

    // Complete the findNumber function below.
    static String findNumber(List<Integer> arr, int k) {
        String answer = "NO";
        for (Integer i : arr) {
            if (i == k) {
                answer = "YES";
        return answer;

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int arrCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> arrTemp = new ArrayList<>();

        IntStream.range(0, arrCount).forEach(i -> {
            try {
                arrTemp.add(bufferedReader.readLine().replaceAll("\\s+$", ""));
            } catch (IOException ex) {
                throw new RuntimeException(ex);

        List<Integer> arr = arrTemp.stream()

        int k = Integer.parseInt(bufferedReader.readLine().trim());

        String res = findNumber(arr, k);



Post a Comment