Question

In: Computer Science

Problem: Given an integer k, find the two closest integers in absolute difference and the whole...

Problem: Given an integer k, find the two closest integers in absolute difference and the whole product equals k+1 or k+2. Return the two integers in any order. Write the code in java.

  • You can assume the integer value will be between [1,10^9]

  • You can multiple a divisor with itself.

  • Description of a sample run 1:

    • The input number 8 hence, k+1 is 9 and k+2 is 10

    • The divisors for 9 are: 1,3,9 and for 10: 1,2,5,10

    • For both cases, we have 2 divisors that multiply exactly to the values (3*3=15 and 2*5=10), we also have 1*9 and 1*10, however, this will yield the largest difference

    • However, we want to pick the product with the lowest absolute value hence: 3-3=0 and 5-2=3 so our correct choice is 3,3

Sample Input:

Input: num=8

Output: [3,3]

Solutions

Expert Solution

import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.lang.Math;
class Factors{
    public static List<List<Integer> > factComb(int n)
    {
        List<List<Integer> > r_list =
                     new ArrayList<List<Integer> >();
        List<Integer> list = new ArrayList<Integer>();
        factorsListFunc(2, 1, n, r_list, list);
        return r_list;
    }

    public static void factorsListFunc(int first, int each_prod, int n,
    List<List<Integer> > r_list, List<Integer> s_r_list)
    {
        if (first > n || each_prod > n)
            return;
        if (each_prod == n) {
            ArrayList<Integer> t =new ArrayList<Integer>(s_r_list);
            r_list.add(t);
            return;
        }
        for (int i = first; i < n; i++) {
            if (i * each_prod > n)
                break;
            if (n % i == 0) {
                s_r_list.add(i);
                factorsListFunc(i, i * each_prod, n, r_list, s_r_list);
                s_r_list.remove(s_r_list.size() - 1);
            }
        }
    }
    static void printDivisors(int n)
    {
        for (int i=1;i<=n;i++)
            if (n%i==0)
                System.out.printf("%d ",i);
    }

    public static void main(String[] args)
    {
        //Scanner value = new Scanner(System.in);
   // System.out.println("Enter integer value: ");
    int k = 8; //value.nextInt();
    System.out.println("value of k is: " + k);
    int a=k+1, b=k+2;
    System.out.println("k+1 is: " + a);
    System.out.println("k+2 is: " + b);
        System.out.println("The divisors of k+1 are: ");
        printDivisors(a);
        System.out.println("\nThe divisors of k+2 are: ");
        printDivisors(b);
        List<List<Integer> > resultant = factComb(a);
         List<List<Integer> > resultant1 = factComb(b);
       System.out.println("\ndivisors that multiply exactly to the value:");
       int m=0, d=0,c=0;
         for (List<Integer> i : resultant1) {
            for (int j : i)
               if(i.size()==2)
                  System.out.print(j + " ");
            System.out.println();
        }
        for (List<Integer> i : resultant) {
            for (int j : i)
               if(i.size()==2)
                  System.out.print(j + " ");
            System.out.println();
        }
        for (List<Integer> i : resultant) {
            for (int j : i)
               if(i.size()==2)
               d=Math.abs(i.get(0)-i.get(1));
               if(m<=d)
               m=d;
         }
           for (List<Integer> i : resultant1) {
            for (int j : i)
               if(i.size()==2)
              { c=Math.abs(i.get(0)-i.get(1)); }
              
         }
         if(c<m)
          for (List<Integer> i : resultant1) {
            for (int j : i)
               if(i.size()==2)
              {    d=Math.abs(i.get(0)-i.get(1));
                  System.out.print(j + " "); }
                   c=Math.max(m,d);
            System.out.println("absolute value is: " + c);
            System.out.println();
        }
         else
          for (List<Integer> i : resultant) {
            for (int j : i)
               if(i.size()==2)
              { d=Math.abs(i.get(0)-i.get(1));
                  System.out.print(j + " "); }
                 c=Math.min(m,d);
            System.out.println("is the output with absolute value : " + c);
            System.out.println();
        }
    }  
}


Related Solutions

Problem 5 Given a table of n integers and an integer k, make a program in...
Problem 5 Given a table of n integers and an integer k, make a program in C++ that: a) Read n b) Read the table of numbers. c) Determine the number of elements in the table less than k d) Determine the number of elements in the table equal to k e) Determine the number of elements in the table greater than k f) That displays the values found
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
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.
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity. Please provide a solution in Java
that, given an integer N and an integer K, returns the minimum number of rounds that...
that, given an integer N and an integer K, returns the minimum number of rounds that are necessary for John to leave the casino with N chips, having played all-in no more than K times.
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that...
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that denotes distance, determine if all the 1’s are at least k spaces away from each other. Details: The distance between each 1 in the array can be greater than or equal to k places. It doesn’t have to be exactly k Assume the array will only contain 0’s and 1’s – no need to do validation checking If k is greater than the size...
Use Lagrange multipliers to find the point on the given plane that is closest to the...
Use Lagrange multipliers to find the point on the given plane that is closest to the following point. x − y + z = 6;    (2, 7, 3)
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
Given an array of integers, and given a specific value k (not equal to 0), produce...
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT