Skip to content

add 2 Algo makeAchange and Activity selection line #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions Others/ActivitySelectionLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

package GreedySelection;
import java.util.Scanner;

import com.sun.jdi.event.Event;
public class GreedySelection
{
public int[][] sort(int[][] event)
{
for(int i = 0; i <(event[0].length - 1) ; i++)
{
for(int j = i; j <(event[0].length - 1) ; j++){
if(event[1][i] > event[1][j+1])
{
int s = event[0][j+1];
event[0][j+1] = event[0][i];
event[0][i] = s;

int f = event[1][j+1];
event[1][j+1] = event[1][i];
event[1][i] = f;
}
}
}
return event;
}
public void display(int[][] event ,int n)
{
System.out.println();

System.out.print(" ");

for(int i = 0; i < n ; i++)
{
System.out.print((i+1) + "\t");
}
for(int i = 0; i<2;i++)
{
if (i ==0){
System.out.print("\nStart : ");
}
else{
System.out.print("\nfinish : ");

}
for (int j = 0; j<n ; j++)
{
System.out.print(event[i][j] + "\t");

}
System.out.print("\n");
}
}
public void greedy(int[][] event)
{
int [][] answer;

int l =0;

answer = new int[2][event[0].length];
answer[0][0] = event[0][0];
answer[1][0] = event[1][0];

for(int j = 1; j <=(event[0].length - 1) ; j++){
if(answer[1][l] < event[0][j]) // Here, In condition if we take <= that means between two events has no interval first event over then second event synchronousy done.
{
l ++;
answer[0][l] = event[0][j];
answer[1][l] = event[1][j];
// k = j;
}
}

this.display(answer, l+1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("How many Event Arrive");
int n = sc.nextInt();
int f;
int event[][];
event = new int[2][n];

for(int i = 0; i < n ;i ++)
{

System.out.println("please enter " + (i+1) + " event");
System.out.print("Start Time = " );
event[0][i] = sc.nextInt();
while(true)
{
System.out.print("\nFinish Time = ");

f = sc.nextInt();
if(f > event[0][i] && f < 24)
{
event[1][i] = f;
break;
}
else{
System.out.println("please enter valid finish time. it never happen start time > finish time...");
continue;
}
}

}
GreedySelection m = new GreedySelection();
m.display(event , n);
event = m.sort(event);
m.display(event, n);

m.greedy(event);
}
}
63 changes: 63 additions & 0 deletions Others/GreedyAlgorithmMakeAchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

/*clear it is poggrame for the greeding algoritham */


import java.util.Scanner; // it is for the get input from the users


class GreedyAlgorithm{
static int j = 0;
public int max(int[] candidate){
for (int i = j; i<candidate.length ; i++){
if (candidate[j] < candidate[i]){
int temp = candidate[j];
candidate[j] = candidate[i];
candidate[i] = temp;
}
}
int x = candidate[j];
j++;
return x;
}

public static void main(String[] args) {
int[] candidate;
System.out.println("please enter the number of coin ");

Scanner scan = new Scanner(System.in); // create a object of the Scanner class to get input

int n = scan.nextInt();
candidate = new int[n];

// it is for the get price of the coin in candidate array
for(int i = 0 ; i <n ; i++){
System.out.println("enter the coin price " + (i+1));
candidate[i] = scan.nextInt();
}

System.out.println("please enter the rupees");
int sum = 0;
int solution[];
int count = 0;

solution = new int[n];
int rupees = scan.nextInt();
GreedyAlgorithm m =new GreedyAlgorithm();
for(int i = 0; i < n ; i++){
int maximum = m.max(candidate);
if ((sum + maximum) <= rupees){
count++;
sum = sum + maximum;
solution[i]= maximum;
}
}
if (sum == rupees){
System.out.println("minimum = " + count + "coin required");

for(int i =0 ; i < n; i ++){
if(solution[i] == 0){continue;}
System.out.println(solution[i]);
}
}
}
}