Repository navigation
Expand file tree
/
Copy pathAlgorithm.cpp
More file actions
94 lines (80 loc) · 1.33 KB
/
Copy pathAlgorithm.cpp
File metadata and controls
94 lines (80 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Algorithm.h"
#include <cstdio>
namespace algorithm
{
int fibonacci(int n)
{
if( n<=2 ) return 1;
int a = 1, b = 1;
for(int i = 3; i <= n; ++i)
{
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
int findDupCharCount(const char* str)
{
int count = 0;
for(int i = 0; i < strlen(str); ++i)
{
if(str[i] == str[i + 1] && i < strlen(str) - 2 && str[i] != str[i + 2])
count++;
}
return count;
}
int findKthMaxElement(int* arr, int n, int k)
{
}
int MaxSubsequenceSum(const int A[], int N)
{
int thisSum, maxSum, j;
thisSum = maxSum = 0;
for(j = 0; j < N; j++)
{
thisSum += A[j];
if(thisSum > maxSum)
maxSum = thisSum;
else if(thisSum < 0)
thisSum = 0;
}
return maxSum;
}
int BinarySearch(const int A[], int N, int x)
{
int low, mid, high;
low = 0; high = N - 1;
while (low <= high)
{
mid = (low + high) / 2;
if(A[mid] < x)
low = mid + 1;
else if(A[mid] > x)
high = mid - 1;
else
return mid;
}
return -1;
}
unsigned int Gcd(unsigned int M, unsigned int N)
{
unsigned int Rem;
while(N > 0)
{
Rem = M % N;
M = N;
N = Rem;
}
return M;
}
long int Pow(long int X, unsigned int N)
{
if( N ==0 ) return 1;
if(N == 1) return N;
if(N%2 == 0)
return Pow(X*X, N/2);
else
return Pow(X*X, N/2) * X;
}
}