forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMaxHammingDistance.cpp
More file actions
53 lines (46 loc) · 1.27 KB
/
findMaxHammingDistance.cpp
File metadata and controls
53 lines (46 loc) · 1.27 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
#include<iostream>
#include<limits>
#include<math.h>
using namespace std;
int maxHammingDistance(int *arr,int n){
// arr[] to brr[] two times so that
// we can traverse through all rotations.
int brr[2 *n + 1];
for (int i = 0; i < n; i++)
brr[i] = arr[i];
for (int i = 0; i < n; i++)
brr[n+i] = arr[i];
// We know hamming distance with 0 rotation
// would be 0.
int maxHam = 0;
// We try other rotations one by one and compute
// Hamming distance of every rotation
for (int i = 1; i < n; i++)
{
cout<<"This is the iteration no "<<i<<endl;
int currHam = 0;
for (int j = i, k=0; j < (i + n); j++,k++){
cout<<"comparing "<<brr[j]<<" with "<<arr[k]<<endl;
if (brr[j] != arr[k]){
currHam++;
}
}
// We can never get more than n.
if (currHam == n)
return n;
maxHam = max(maxHam, currHam);
}
return maxHam;
}
int main(){
int size,*arr;
cout<<"Enter the size"<<endl;
cin>>size;
arr = new int[size];
cout<<"Enter the elements of the array"<<endl;
for(int i = 0 ; i < size ; i++){
cin>>arr[i];
}
int h_distance = maxHammingDistance(arr,size);
cout<< h_distance<<endl;
}