You are on page 1of 1

import java.util.

*;
public class greedyactivityselection {
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter Number of activity you want to perform: ");
int n = sc.nextInt();
int[] start = new int[n];
int[] end = new int[n];
System.out.println("Enter starting and ending time of each job:");
for(int i=0;i<n;i++){
start[i]=sc.nextInt();
end[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(end[j]<end[i]){
int temp = end[j];
end[j] = end[i];
end[i] = temp;
temp = start[j];
start[j] = start[i];
start[i] = temp;
}
else if(end[j]==end[i]&& start[j]<start[i]){
int temp = end[j];
end[j] = end[i];
end[i] = temp;
temp = start[j];
start[j] = start[i];
start[i] = temp;
}
}
}
int[] selected = new int[n];
selected[0]=1;
int j=0,p=1;
for(int i=1;i<n;i++){
if(start[i]>=end[j]){
selected[p]=i+1;
++p;
j=i;
}
}
System.out.println("Selected activities are:");
for(int i=0;selected[i]!=0;i++){
System.out.print(selected[i]+" ");
}
}
}

You might also like