Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Merging Two Sorted Arrays (#520)
Browse files Browse the repository at this point in the history
* Two Sum C solution

* Merging Two Sorted Arrays
  • Loading branch information
Sheikh-Yawar authored Oct 10, 2021
1 parent dd852eb commit c4f7b84
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Programming/C/Merging Two sorted Arrays/MergingSortedArrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>

int main()
{

int n1,n2,n3; //Array Size Declaration
printf("\nEnter the size of first array ");
scanf("%d",&n1);
printf("\nEnter the size of second array ");
scanf("%d",&n2);

n3=n1+n2;
printf("\nEnter the sorted array elements");
int a[n1],b[n2],c[n3]; //Array Declaration
for(int i=0;i<n1;i++) //Array Initialized
{
scanf("%d",&a[i]);
c[i]=a[i];
}
int k=n1;
printf("\nEnter the sorted array elements");
for(int i=0;i<n2;i++) //Array Initialized
{
scanf("%d",&b[i]);
c[k]=b[i];
k++;
}

printf("\nAfter sorting...\n");
for(int i=0;i<n3;i++) //Sorting Array
{
int temp;
for(int j=i+1; j<n3 ;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}

for(int i=0 ; i<n3 ; i++) //Print the sorted Array
{
printf(" %d ",c[i]);
}
return 0;
}
29 changes: 29 additions & 0 deletions Programming/C/Two Sum/TwoSum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nums[100],i,j,size,target,sum=0;
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter an array elements\n");
for(i=0;i<size;i++)
{
scanf("%d",&nums[i]);
}
printf("Enter the value for target\n");
scanf("%d",&target);

//Logic to find indices of two numbers with given target as sum
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
sum=nums[i]+nums[j];
if(sum==target)
{
printf("Output : [%d, %d]",i,j);
}
}
}
return 0;
}

0 comments on commit c4f7b84

Please sign in to comment.