-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthSmallestElement.java
More file actions
56 lines (46 loc) · 1.42 KB
/
NthSmallestElement.java
File metadata and controls
56 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package array_Programming.medium.day_11;
import java.util.Arrays;
import java.util.Scanner;
//26. Print the nth smallest element in a given array.
public class NthSmallestElement
{
public static void printNthSmallest(int[] arr, int n)
{
if (n <= 0 || n > arr.length)
{
System.out.println("Invalid value of n.");
return;
}
for (int i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int nthSmallest = arr[n - 1];
System.out.println(n + "th smallest element: " + nthSmallest);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < arr.length; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("Enter value of n:");
int n = sc.nextInt();
System.out.println("Array: " + Arrays.toString(arr));
printNthSmallest(arr, n);
sc.close();
}
}