-
Notifications
You must be signed in to change notification settings - Fork 0
/
min1swap.cpp
111 lines (102 loc) · 2.38 KB
/
min1swap.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by Ankush on 19/06/21.
//
#include <iostream>
#include <math.h>
using namespace std;
//min swaps required to group all 0s and 1s together
int minSwaps(int arr[], int n) {
bool isOnePresent=false;
for(int i=0;i<n;i++){
if(arr[i]==1) {
isOnePresent=true;
break;
}
}
if (!isOnePresent) {
return -1;
}
//try for all one at left
int i=0,j=n-1;
int tempAns=0, tempAns1=0;
while(i<j) {
if (arr[j]==1) {
if (arr[i]==0) {
//swap(arr[i],arr[j]);
tempAns++;
i++;j--;
} else {
i++;
}
} else {
j--;
}
}
i=0;j=n-1;
//try for all one at right
while(i<j) {
if (arr[i]==1) {
if (arr[j]==0) {
//swap(arr[i],arr[j]);
tempAns1++;
i++;j--;
} else {
j--;
}
} else {
i++;
}
}
cout<<tempAns<<tempAns1;
return min(tempAns,tempAns1);
}
//TODO ************************* Always it has a very great approach find the subarray having largest 1s of size equal to total number of 1s in array and count 0s in it
//min swaps required to group all 1s together -<slight diff in both>-
//it has a very great aproach
int minSwaps2(int arr[], int n) {
int cnt=0;
for(int i=0;i<n;i++){
if(arr[i]==1) {
cnt++;
}
}
if (cnt==0) {
return -1;
}
int windowSize=cnt;
int zeroInWindow=0, oneInWindow=0;
for(int i=0;i<windowSize;i++) {
if(arr[i]==0) {
zeroInWindow++;
} else {
oneInWindow++;
}
}
int ans = INT_MIN, finalAns=0;
if(oneInWindow>ans) {
ans = oneInWindow;
finalAns = zeroInWindow;
}
for(int i=windowSize;i<n;i++) {
if(arr[i]==1) {
oneInWindow++;
} else {
zeroInWindow++;
}
if (arr[i-windowSize]==0) {
zeroInWindow--;
} else {
oneInWindow--;
}
if(oneInWindow>ans) {
ans = oneInWindow;
finalAns = zeroInWindow;
}
}
return finalAns;
}
//int main() {
// int arr[] = {1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1};
// cout<<minSwaps2(arr,16);
// return 0;
//}