Question

In: Computer Science

Write a program in Java with a Scanner. Given an array and a number k where...

Write a program in Java with a Scanner.

Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array.

It is given that all array elements are distinct.

Example:

Input: arr[] = {7,10,4,3,20,15}

k = 3

Output: 7

Solutions

Expert Solution

import java.util.Arrays;
import java.util.Scanner;

public class KthSmallestElement {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter all the numbers you want to put in array seperated by space: ");
        String line = in.nextLine();

        String tokens[] = line.split("\\s+");
        int arr[] = new int[tokens.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(tokens[i]);
        }

        System.out.print("Enter value of k: ");
        int k = in.nextInt();
        while(k < 1 || k > arr.length) {
            System.out.print("Invalid value.\nEnter value of k: ");
            k = in.nextInt();
        }

        // do k pass in
        for (int i = 0; i < k; i++) {
            // Last i elements are already in place
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[j] < arr[j - 1]) {
                    int l = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = l;
                }
            }
        }

        //System.out.println(Arrays.toString(arr));

        System.out.println("Kth element of array: " + arr[k-1]);

        in.close();
    }

}


Related Solutions

3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the...
In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the computer user to enter his/her: • Name: a string representing the user’s first name • month of birthday: an integer representing the month of the user’s birthday • day of birthday: an integer representing the day of the user’s birthday • year of birthday: an integer representing the year of the user’s birthday Consider the following aspects that your program shall perform: VALIDATE USER’S...
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
In Java, write a program that given an array A[1...n] and a value S finds 0...
In Java, write a program that given an array A[1...n] and a value S finds 0 < i < j < n such that A[i] + A[j] = S
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT