dimanche 26 mars 2017

Improving the time complexity of this java program

I have to solve the following problem: From an array a of n elements, select k elements such that the sum of the k elements selected is equal to the minimum multiple of f possible for this array and output that minimum possible multiple of f.

Sample test case:
array a = {1,2,3,4,5}
k = 3
f = 5
output:
minimum possible multiple of f = 10

I tried implementing it with my code and it passes 80% of the test cases but fails for the remaining cases because of exceeding the time limit. Please help me solve the time complexity issues of my code:

Selection.java

import java.util.*;

public class Selection
{
  public Selection()
  {
  }
  private static Random rand = new Random();
  public static int minMultiple (int[] input1, int input2, int input3)
  {
    Arrays.sort(input1);
    List<Integer> uniqueSet = new ArrayList<>();
    List<Integer> listOfMultiples = new ArrayList<>();
    int  maxSubSets = (int)Math.pow (2,input1.length);
    for (int i=0; i<maxSubSets; i++)
    {
      // Method to find the minimum possible multiple of user input value for the array 
      uniqueSet = multiple (input1, input2, input3);
      int sum =0;
      for (int j=0; j < uniqueSet.size(); j++)
      {
        sum = sum + uniqueSet.get(j);
      }

      if (sum % input2 == 0)
      {
        listOfMultiples.add (sum);
      }
    }
    if (listOfMultiples.isEmpty())
    {
      return -1;
    }
    Collections.sort (listOfMultiples);
    return listOfMultiples.get(0);
  }
  // Method to return the list of selected random values
  private static List<Integer> multiple (int [] input1, int input2, int input3)
  {
    int  size = input1.length,value =0, index=0;
    List <Integer> list = new ArrayList<>();
    List <Integer> listOfValues = new ArrayList<>();
    while (list.size()<input3)
    {
      // Generating non-repeating random numbers from the array
      index = rand.nextInt(size);
      value = input1[index];
      if (!(list.contains(index)))
      {
        list.add(index);
        listOfValues.add(value);
      }
    }      

    return listOfValues; 
  }
  // Run method
  public final void run (String []args)
  {
    // Entering the number of boxes
    Scanner input = new Scanner (System.in);
    System.out.println ("Enter the size of the array:");
    int arraySize = input.nextInt();

    // Entering the number of balls in each box
    int [] elements = new int [arraySize];
    System.out.println ("Enter the elements of the array:");
    for (int i=0; i<arraySize;i++)
    {
      elements[i] = input.nextInt();
    }
    System.out.println ("Enter the number whose multiple you want");
    int factor = input.nextInt();
    System.out.println ("Enter the no. of elements you want to select.");
    int selection = input.nextInt();
    int result = minMultiple(elements, factor, selection);
    System.out.println ("The minimum possible multiple = "+ result) ;
  }
}

Result.java

public class Result 
{

  private Result() 
  { 

  }

     public static void main (String []args) throws Exception
   {
        Selection value = new  Selection();
        value.run(args);   
   }

}




Aucun commentaire:

Enregistrer un commentaire