forked from aditya109/git-osp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging Two Sorted Arrays (aditya109#520)
* Two Sum C solution * Merging Two Sorted Arrays
- Loading branch information
1 parent
dd852eb
commit c4f7b84
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Programming/C/Merging Two sorted Arrays/MergingSortedArrays.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |