Find Nth largest and smallest SAP labs Coding Question 2023

Sept. 19, 2023 by shrikant patel Posted in programming Category 0 Comments 170 Views

Find Nth largest and smallest SAP labs Coding Question 2023

Problem Statement  :

Write a program to find the nth largest and nth smallest item in the array and print them in the same line.

Input Format
The first line has the following –
Size of array, value of n
2nd line has the array

Output Format
Nth largest and nth smallest respectively

Input :                                 Output :
6 3                                       3 4
1 2 3 4 5 6

 

PYTHON

size, n = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
print(arr[n - 1], arr[-n])

 

Solution 

Certainly! The code is written in Python and appears to be used for processing a list of integers. Let's break it down line by line:

1. size, n = map(int, input().split())
   - This line takes input from the user. The `input()` function reads a line of text from the user, and `split()` is used to split that line into a list of words (based on spaces by default).
   - `map(int, ...)` is used to convert each word (which is initially a string) into an integer.
   - The first integer entered by the user is assigned to the variable `size`, and the second integer is assigned to the variable `n`.

2. arr = list(map(int, input().split()))
   - This line takes another input from the user, which is a space-separated list of integers.
   - `input()` reads the line of text, `split()` splits it into words, and `map(int, ...)` converts each word into an integer.
   - The result is a list of integers, which is assigned to the variable `arr`.

3. arr.sort()
   - This line sorts the list `arr` in ascending order. After this line executes, `arr` will be in non-decreasing order, meaning the elements will be arranged from smallest to largest.

4. print(arr[n - 1], arr[-n])
   - This line prints two values from the sorted list `arr`.
   - `arr[n - 1]` retrieves the (n-1)th element from the list `arr`, which corresponds to the nth smallest element in the list. It's because Python uses 0-based indexing, so the first element is at index 0.
   - `arr[-n]` retrieves the nth element from the end of the list `arr`, which corresponds to the nth largest element in the list. Using negative indexing, -1 refers to the last element, -2 to the second-to-last, and so on.
   - The two values are separated by a space and printed to the console.

So, in summary, this code takes two inputs: `size` (which is likely the number of elements in the list) and `n` (which is used to find the nth smallest and nth largest elements from the list). It then sorts the list of integers entered by the user and prints the nth smallest and nth largest elements from the sorted list.

 

JAVA

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int arr[] = new int[m];
        for (int i = 0; i < m; i++)
            arr[i] = sc.nextInt();
        Arrays.sort(arr);
        System.out.println(arr[n - 1] + " " + arr[m - n]);
    }
}

 

Solution

This Java code reads input integers from the user, sorts them, and then prints the nth smallest and nth largest elements from the sorted array. Let's break down the code step by step:

1. `import java.util.*;`
   - This line imports the `java.util` package, which includes various utility classes, including `Scanner` and `Arrays`, used in this program.

2. `public class Main {`
   - This line declares a class named `Main`, which is the main class of the program.

3. `public static void main(String[] args) {`
   - This line declares the `main` method, which is the entry point of the program.

4. `Scanner sc = new Scanner(System.in);`
   - This line creates a `Scanner` object named `sc`, which is used to read input from the user through the standard input stream (`System.in`).

5. `int m = sc.nextInt();`
   - This line reads an integer `m` from the user. It is expected that `m` represents the number of elements in the array.

6. `int n = sc.nextInt();`
   - This line reads another integer `n` from the user. It is expected that `n` represents the value used to find the nth smallest and nth largest elements.

7. `int arr[] = new int[m];`
   - This line creates an integer array `arr` of size `m`, where `m` is the number of elements entered by the user. This array will store the integers provided by the user.

8. `for (int i = 0; i < m; i++)`
   - This line starts a loop that iterates `m` times, where `m` is the number of elements in the array.

9. `arr[i] = sc.nextInt();`
   - Inside the loop, this line reads an integer from the user using the `nextInt()` method of the `Scanner` object and stores it in the `i`-th element of the `arr` array.

10. `Arrays.sort(arr);`
    - After reading all the integers, this line sorts the `arr` array in ascending order using the `Arrays.sort()` method.

11. `System.out.println(arr[n - 1] + " " + arr[m - n]);`
    - Finally, this line prints the nth smallest and nth largest elements from the sorted array. 
    - `arr[n - 1]` retrieves the (n-1)th element from the sorted array, representing the nth smallest element.
    - `arr[m - n]` retrieves the (m-n)th element from the sorted array, representing the nth largest element.
    - The two elements are printed to the console with a space in between.

In summary, this Java program takes two input integers, creates an array to store the user's input, sorts the array, and then prints the nth smallest and nth largest elements from the sorted array.

 

 

 

C++

#include <bits/stdc++.h>

using namespace std;

int main() {
    int size, n;
    cin >> size >> n;
    vector < int > a(size);
    for (int i = 0; i < size; i++) cin >> a[i];
    sort(a.begin(), a.end());
    cout << a[n - 1] << " " << a[size - n];
}

 

Sloution 

This C++ code reads input integers, sorts them, and then prints the nth smallest and nth largest elements from the sorted vector. Let's break down the code step by step:

1. `#include <bits/stdc++.h>`
   - This line includes a preprocessor directive that includes a commonly used header file in competitive programming, which contains various standard library headers. It's a shortcut to include a large set of standard libraries.

2. `using namespace std;`
   - This line indicates that you are using the `std` namespace, so you don't need to prefix standard library functions and objects with `std::`.

3. `int main() {`
   - This line declares the `main` function, which is the entry point of the program.

4. `int size, n;`
   - This line declares two integer variables, `size` and `n`, which will be used to store the size of the vector and the value used to find the nth smallest and nth largest elements.

5. `cin >> size >> n;`
   - This line reads two integers, `size` and `n`, from the standard input stream (`cin`). `size` represents the number of elements in the vector, and `n` represents the value used to find the nth smallest and nth largest elements.

6. `vector<int> a(size);`
   - This line declares a vector of integers named `a` with a size of `size`. This vector will store the integers provided by the user.

7. `for (int i = 0; i < size; i++) cin >> a[i];`
   - This loop reads `size` integers from the standard input stream and stores them in the elements of the vector `a`.

8. `sort(a.begin(), a.end());`
   - After reading all the integers, this line sorts the vector `a` in ascending order using the `sort` function from the C++ Standard Library. It sorts the elements from the beginning (`a.begin()`) to the end (`a.end()`).

9. `cout << a[n - 1] << " " << a[size - n];`
   - Finally, this line prints the nth smallest and nth largest elements from the sorted vector.
   - `a[n - 1]` retrieves the (n-1)th element from the sorted vector, representing the nth smallest element.
   - `a[size - n]` retrieves the (size-n)th element from the sorted vector, representing the nth largest element.
   - The two elements are printed to the standard output stream (`cout`) with a space in between.

In summary, this C++ program takes two input integers, creates a vector to store the user's input, sorts the vector, and then prints the nth smallest and nth largest elements from the sorted vector.

Do you dream of turning your thoughts and words into income ? join us now and become blogger now.
PUBLICBLOGS.IN


programming coding question
If you went to earn online by just sharing your thoughts then join us now with Google and become blogger now.
PUBLICBLOGS.IN
Related Story
0 Comments

Leave a Comment