AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Write a program to find the largest LCM

Write a program to input n numbers and find the largest LCM among all the subsets of those numbers where the length of each subset is defined by a variable k(k is a user input).The output of the program should display the largest LCM value amongst all the subsets.

Note: 2<=n<=10 and 2<=k<=n

Input Specification:

First input will be an integer n indicating the total number of elements. Second line of input will contain n integers Third input will be an integer k indicating the length of each subset.

Output Specification: A single integer specifying the largest LCM.

Sample Input:

3

2 3 5

2

Sample Output:

15

corejava x 333
programming x 150
Posted On : 2017-12-07 17:39:38.0
profile Divesh - anyforum.in Divesh
8800
up-rate
5
down-rate

Answers


import java.util.*;
import java.util.Arrays;
public class LcmOfSubset{
public static int getLCM1(int...a){
int lcm=0;
boolean found;
for(int i=1;;i++){
found=true;
for(int x=0;x<a.length;x++){
if(i%a[x]!=0){
found=false;
break;
}
}
if(found){
lcm=i;
break;
}
}
return lcm;
}


public static void main(String[]args){

int n,k;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
if(n>=2 && n<=10){
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
k=sc.nextInt();
if(k>=2&&k<=n){
int b[]=new int[k];
Arrays.sort(a);
int l=n;
for(int i=0;i<k;i++){
b[i]=a[l-1];
l--;
}

System.out.println(getLCM1(b));
}else{
System.out.print("Invalid Choice");

}

}else{
System.out.print("Invalid Choice");
}
}
}

Posted On : 2017-12-09 21:54:37
Satisfied : 2 Yes  0 No
profile sandeep - anyforum.in sandeep
000
Reply This Thread
up-rate
0
down-rate
Comments
if anyone facing any problem then comment
profile sandeep - anyforum.in sandeep
0  0  0
Posted On :2017-12-09 21:57:06.0
Leave a Comment
can not understand this question will you please explain what this question exactly mean and what steps I should take to solve it.
thank you...
profile Akanksha Gupta - anyforum.in Akanksha Gupta
0  0  0
Posted On :2017-12-13 08:53:40.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in