Skip to content
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

A3 #250

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

A3 #250

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
36 changes: 36 additions & 0 deletions binary search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<stdio.h>

int binary_search(int a[],int l,int u,int choice)//assumes array is sorted
{
int m;
while(l<u)
{
m=(l+u)/2;

if(a[m]==choice)
return m;

if(choice<a[m])
u=m-1;
else
l=m+1;
}

if(a[l]==choice)
return l;
else
return -1;
}

int main()
{
int a[10]={1,2,2,3,4,5,6,7,8,9};

int c=binary_search(a,0,9,2);
if(c==-1)
printf("NON existing\n");
else
printf("EXISTING at index %d\n",c);

return 0;
}
28 changes: 28 additions & 0 deletions d_discrete.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<stdio.h>

int main()
{
unsigned long long p,test,x,y,m,n;

scanf("%llu",&test);

for(;test>0;test--)
{
scanf("%llu%llu%llu",&x,&y,&m);

x%=m;

p=1;

for(;y>0;y/=2)
{
if(y & 1)
p=(p*x)%m;

x=(x*x)%m;
}
printf("%llu ",p);
}

return 0;
}
43 changes: 43 additions & 0 deletions e.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<stdio.h>
#include<stdlib.h>
int a[1001];
int b[1001];

int main()
{
int n,x,count=0,i,t;
scanf("%d%d",&n,&x);

for(i=0;i<n;i++)
{
scanf("%d",&t);
if(t<0)
{
t*=(-1);
a[t]++;
}
else if (t>0)
{
/* code */
b[t]++;
}
}

for(i=1;i<=x;i++)
{
if(a[i]==b[i])
continue;
else
{
t=abs(a[i]-b[i]);
if(t<0)
t*=(-1);
count+=t;
//count+=abs(a[i]-b[i]);
}
}

printf("%d",count);

return 0;
}
89 changes: 89 additions & 0 deletions h_merge.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include<stdio.h>
unsigned long long a[10000001];

void merge(unsigned long long a[],unsigned long long l,unsigned long long m,unsigned long long u)
{
unsigned long long i,j,n1,n2,k;

n1=m-l+1;
n2=u-m;

unsigned long long a1[n1],a2[n2];

for(i=0;i<n1;i++)
a1[i]=a[l+i];

for(i=0;i<n2;i++)
a2[i]=a[m+i+1];

i=j=0;

k=l;

while (i < n1 && j < n2)
{
if (a1[i] <= a2[j])
{
a[k] = a1[i];
i++;
}
else
{
a[k] = a2[j];
j++;
}

k++;
}

while (i < n1)
{
a[k] = a1[i];
i++;
k++;
}

while (j < n2)
{
a[k] = a2[j];
j++;
k++;
}

}

void merge_sort(unsigned long long a[],unsigned long long l,unsigned long long u)
{
unsigned long long m;
if(l<u)
{
m=l+(u-l)/2;

merge_sort(a,l,m);
merge_sort(a,(m+1),u);
merge(a,l,m,u);

}

}

int main()
{
unsigned long long n,k,i,j;

scanf("%llu%llu",&n,&k);
for(i=0;i<=n;i++)
scanf("%llu",&a[i]);

for(;i<10000001;i++)
a[i]=0;

merge_sort(a,0,n);

for(i=0;a[i]==0;i++);

for(;a[i]==a[i+1];i+=k);

printf("%llu\n",a[i]);
return 0;
}