Skip to content

Instantly share code, notes, and snippets.

@namthatman
Created April 2, 2019 05:55
Show Gist options
  • Save namthatman/c1b2acece9800c2d736329471b50a60c to your computer and use it in GitHub Desktop.
Save namthatman/c1b2acece9800c2d736329471b50a60c to your computer and use it in GitHub Desktop.
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";
break;
}
}
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()
.map(String::trim)
.map(Integer::parseInt)
.collect(toList());
int k = Integer.parseInt(bufferedReader.readLine().trim());
String res = findNumber(arr, k);
bufferedWriter.write(res);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
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.
@MagDag05
Copy link

MagDag05 commented Oct 3, 2019

Like

@maheshgoud619
Copy link

Can you telle me the video to explain this programs

@namthatman
Copy link
Author

There is no video. This problem is from Hackerrank.com

@maheshgoud619
Copy link

Can you suggest me some coding questions to prepare ibm

@namthatman
Copy link
Author

Hackerrank.com, you can practice coding for interview in here: https://www.hackerrank.com/interview/interview-preparation-kit

@ciaranmckenna
Copy link

Why does this solution work for all test cases presented by Hackerrank but the same solution using a for iteration loop fails for 5 out of 13 test cases?

` public static String findNumber(List arr, int k) {

    for (int i = 0; i < arr.size() ; i++) {
        if (i == k){
            return "YES";
        }
    }

    return "NO";
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment