Skip to content

Commit 303092f

Browse files
committed
Merge branch 'main' into MaximumRecursive
2 parents a99d2a3 + c0ff148 commit 303092f

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar mais pessoas a co
319319
</a>
320320
</td>
321321
<td> <!-- Go -->
322-
<a href="./CONTRIBUTING.md">
323-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
322+
<a href="./src/go/buscasequencial.go">
323+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/go/go-original.svg" />
324324
</a>
325325
</td>
326326
<td> <!-- Ruby -->
@@ -3437,8 +3437,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar mais pessoas a co
34373437
<tr>
34383438
<td>Soma de 2 Números</td>
34393439
<td> <!-- C -->
3440-
<a href="./CONTRIBUTING.md">
3441-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
3440+
<a href="./src/c/TwoSum.c">
3441+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/c/c-original.svg" />
34423442
</a>
34433443
</td>
34443444
<td> <!-- C++ -->

src/c/TwoSum.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
TwoSum Algorithm in C
3+
Ricardo Ferreira Carvalheira - 2023
4+
https://github.com/ricardocarva
5+
6+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
7+
8+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
9+
10+
You can return the answer in any order.
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
int* twoSum(int* nums, int numsSize, int target){
17+
18+
for(int i=0;i<numsSize-1;i++)
19+
{
20+
for( int j=i+1;j<numsSize;j++)
21+
{
22+
if(nums[i]+nums[j]==target)
23+
{
24+
25+
int* x = (int*) malloc(2*sizeof(int));
26+
x[0]=i;
27+
x[1]=j;
28+
return x;
29+
30+
}
31+
}
32+
}
33+
return NULL;
34+
35+
}
36+
37+
int main() {
38+
int nums[] = {2, 11, 7, 15};
39+
int target = 9;
40+
int numsSize = sizeof(nums) / sizeof(nums[0]);
41+
42+
printf("The target number is %d.", target);
43+
printf("The list has the following items: ");
44+
45+
for(int i=0; i< numsSize; i++)
46+
{
47+
printf("%d ", nums[i]);
48+
}
49+
50+
printf("\n");
51+
52+
int* result = twoSum(nums, numsSize, target);
53+
54+
if (result != NULL) {
55+
printf("The two numbers that add up to %d are: %d and %d\n", target, nums[result[0]], nums[result[1]]);
56+
printf("Indices of the two numbers that add up to %d are: %d and %d\n", target, result[0], result[1]);
57+
free(result); // Don't forget to free the allocated memory
58+
} else {
59+
printf("No solution found.\n");
60+
}
61+
62+
return 0;
63+
}

src/go/buscasequencial.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
myArray := []string{"c", "f", "g", "u"}
7+
fmt.Println("Element found: ", SearchPosition(myArray, "g"))
8+
fmt.Println("Element not found: ", SearchPosition(myArray, "i"))
9+
}
10+
11+
func SearchPosition(myArray []string, element string) int {
12+
for i, v := range myArray {
13+
if v == element {
14+
return i
15+
}
16+
}
17+
return -1
18+
}

0 commit comments

Comments
 (0)