Kth smallest number in python assignment expert

Smallest Missing Number

Given a list of numbers, write a program to print the smallest positive integer missing in the given numbers.

Input

The input will be a single line containing numbers separated by space.

Output

The output should be a single line containing the smallest missing number from given numbers.

Explanation

For example, if the input numbers are 3, 1, 2, 5, 3, 7, 7.

The number 1, 2, 3 are present. But the number 4 is not. So 4 is the smallest positive integers that is missing from the given numbers.

Sample Input 1

3 1 2 5 3 7 7

Sample Output 1

4

Sample Input 2

5 5 2 3 1 8 8 4

Sample Output 2

6

Kth largest factor of N

Write a program to find the Kth largest factor of a number N.

Input

The first line is an integer N. The second line is an integer K.

Output

Print Kth largest factor of N. Print 1, If the number of factors of N is less than K.

Explanation

For

N = 12 and K = 3 The factors of 12 are 1, 2, 3, 4, 6, 12. The 3rd largest factor is 4.For

N = 12 and K = 7 The number of factors for 12 is only 6 and the given K is 7 [no of factors of N < K]. Hence the output should be 1.

Sample Input 1

12

3

Sample Output 1

4

Sample Input 2

12

7

Sample Output 2

1

C++ PROGRAM Write a program that will get 10 numbers[ including duplicate] between 20 and 100and store them in one dimensional array of type int. your program shall delete all ofthem duplicate values and print the final array list.

Write a program which takes 2 arrays of 10 integers each, a and b. ¢ is an array with 10 integers. The program should do the following: 1. Sort the array a using bubble sort. 2. Sort the array b using bubble sort. 3. put into an array c the first five elements from an array a after sorting ,then put into an array c the last five elements from an array b after sorting. Example : a={572,6,1,4,8,3,9,10} aafter sorting= {1, 2,3, 4,5,6,7,8,9, 10} b ={15,18, 12, 16, 19, 13, 11, 14, 17, 20} b after sorting = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20} c={1,2,3,4,5,16,17, 18, 19, 20}

Consider two arrays of Different sizes are given and your task is to multiply corresponding elements and store it into the result  array and print the result array.

We are given an array of size n containing positive integers. The absolute difference between values at indices i and j is |a[i] – a[j]|. There are n*[n-1]/2 such pairs and we are asked to print the kth [1 1

        if [countPairs[a, n, mid] < k]:

            low = mid + 1

        else:

            high = mid

    return low

k = 3

a = [1, 2, 3, 4]

n = len[a]

print[kthDiff[a, n, k]]

C#

using System;

class GFG{

static int countPairs[int[] a,

                      int n,

                      int mid]

{

  int res = 0;

  for[int i = 0; i < n; i++]

  {

    int ub = upperbound[a, n,

                        a[i] + mid];

    res += [ub - [i]];

  }

  return res;

}

static int upperbound[int []a,

                      int n,

                      int value]

{

  int low = 0;

  int high = n;

  while[low < high]

  {

    int mid = [low + high]/2;

    if[value >= a[mid]]

      low = mid + 1;

    else

      high = mid;

  }

  return low;

}

static int kthDiff[int []a,

                   int n, int k]

{

  Array.Sort[a];

  int low = a[1] - a[0];

  for [int i = 1; i > 1;

    if [countPairs[a, n, mid] < k]

      low = mid + 1;

    else

      high = mid;

  }

  return low;

}

public static void Main[String []args]

{

  int k = 3;

  int []a = {1, 2, 3, 4};

  int n = a.Length;

  Console.WriteLine[kthDiff[a, n, k]];

}

}

Javascript

function countPairs[a, n, mid] {

    let res = 0;

    for [let i = 0; i < n; i++] {

        let ub = upperbound[a, n,

            a[i] + mid];

        res += [ub - [i]];

    }

    return res;

}

function upperbound[a, n, value] {

    let low = 0;

    let high = n;

    while [low < high] {

        let mid = [low + high] / 2;

        if [value >= a[mid]]

            low = mid + 1;

        else

            high = mid;

    }

    return low;

}

function kthDiff[a, n, k] {

    a.sort[[a, b] => a - b];

    let low = a[1] - a[0];

    for [let i = 1; i > 1;

        if [countPairs[a, n, mid] < k]

            low = mid + 1;

        else

            high = mid;

    }

    return low;

}

let k = 3;

let a = [1, 2, 3, 4];

let n = a.length;

document.write[kthDiff[a, n, k]];

Time Complexity: O[nlogn]

Auxiliary Space: O[1]

Suppose, the maximum element in the array is, and the minimum element is a minimum element in the array is 

. Then time taken for the binary_search will be , and the time taken for the upper_bound function will be 
 

So, the time complexity of the algorithm is 

. Sorting takes 
. After that the main binary search over low and high takes 
 time because each call to the function countPairs takes time 

So the Overall time complexity would be 

 This article is contributed by Hemang Sarkar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.


Chủ Đề