Skip to content

Commit

Permalink
Merge pull request kothariji#177 from Bhavishya2912/master
Browse files Browse the repository at this point in the history
Added few greedy approach and array algorithm competitive questions
  • Loading branch information
kothariji authored Oct 18, 2020
2 parents c7e1179 + 5f89066 commit b31ff4d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
32 changes: 32 additions & 0 deletions (HACERRANK) algorithms- diagonal difference
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
int n,i,j,d1=0,d2=0,c;
cin>>n;
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}

for(i=0;i<n;i++)
{
d1+=a[i][i];
}
for(i=0;i<n;i++)
{
j=(n-1)-i;
{
d2+=a[i][j];
}
}
c=abs(d1-d2);
cout<<c;
}
43 changes: 43 additions & 0 deletions Greedy/(HACKEREARTH) Motu and Patlu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<iostream>
using namespace std;
int main()
{ int t,i;

cin>>t;
for(i=0;i<t;i++)
{ int count=0,c=0,m=0,p=0,s,n,k,l;
cin>>n;
int a[n];
for(s=0;s<n;s++)
{
cin>>a[s];
}
k=0;
l=n-1;
while(k<=l){
if(m<=p)
{
m=m+a[k];
k++;
c++;
}
else if(m>p)
{
p=p+2*a[l];
l--;
count++;
}}
cout<<c<<" "<<count<<"\n";
if(c>count)
{
cout<<"Motu\n";
}
else if(c<count)
{
cout<<"Patlu\n";
}
else
cout<<"Tie\n";
}
return 0;
}
78 changes: 78 additions & 0 deletions Greedy/CSE 101-cookie-child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* You are baby-sitting n children and have m > n cookies to divide between them.
You must give each child exactly one cookie (of course, you cannot give the same cookie to two different children).
Each child has a greed factor gi, 1 ≤ i ≤ n which is the minimum size of a cookie that the child will be content with;
and each cookie has a size sj , 1 ≤ j ≤ m. Your goal is to maximize the number of content children, i.e.., children i assigned a cookie j with gi ≤ sj.
Give a correct greedy algorithm for this problem, prove that it finds the optimal solution, and give an efficient implementation. */


import java.io.*;
import java.util.*;
import java.util.Arrays;

public class Main
{ public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n,m,i,j;
n= sc.nextInt();
m= sc.nextInt();

int g[]= new int[n];
int s[]= new int[m];
int t=0;
// children greed factor
for(i=0;i<n;i++)
{ g[i]=sc.nextInt();
}
// cookies size
for(j=0;j<m;j++)
{ s[j]=sc.nextInt();
}
for(int k=0; k<=g.length-1;k++)
{ for( i=0; i<=g.length-2;i++)
{ if(g[i]<g[i+1])
{ int temp=g[i];
g[i]=g[i+1];
g[i+1]=temp;
}
}
}
for(int k=0; k<=s.length-1;k++)
{ for( j=0; j<=s.length-2;j++)
{ if(s[j]<s[j+1])
{ int temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
System.out.println("decsending order:");
for(i=0;i<n;i++)
{ System.out.print(g[i]);
} System.out.println();
for(j=0;j<m;j++)
{ System.out.print(s[j]);
} System.out.println();

System.out.print("greed factor \t cookie size assigned");
System.out.println();
int l=m-1;
for(i=0;i<n;i++)
{ for(j=i;j<m;j++)
{ if(g[i]<=s[j])
{ t++; //when gi<=sj, store it in t.
System.out.println(g[i]+"\t \t \t"+s[j]);
}
else if(g[i]>s[j])
{
System.out.println(g[i]+"\t \t \t"+s[l]);
l--;
t++;

}
System.out.println();
break;
}
}

System.out.println("total no. of cookies assigned: "+t);
} }

0 comments on commit b31ff4d

Please sign in to comment.