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

Commit

Permalink
feat: Two sum problem solved in C (#461) Signed-off-by: Priyanshu Sha…
Browse files Browse the repository at this point in the history
…rma <priyanshusharma72002@gmail.com> (#514)
  • Loading branch information
Priyanshu-792 authored Oct 10, 2021
1 parent 4960958 commit ca1d3bf
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Programming/C/Two sum/TwoSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;//size of the vector
printf("Enter size of the array:\n");
scanf("%d",&n);

int numbers[n];

int i;
int a=0;//counter variable to break from the loop

printf("Enter the elements of the array in sorted order: ");//since it is said non-descending order
for(i=0;i<n;i++)
{
scanf("%d",&numbers[i]);

}

int target;//sum need to be searched
printf("\nEnter the target element :");
scanf("%d",&target);

for(i=0;i<n;)
{
if(numbers[i]+numbers[n-1]==target)
{

printf("%d , %d",i+1,n);
a=1;
break;
}
else if(numbers[i]+numbers[n-1]<target)
i++;
else
n--;
}
if(a!=1)
printf("sum not found");
}

0 comments on commit ca1d3bf

Please sign in to comment.