-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinSwapsBinaryArray.java
More file actions
123 lines (99 loc) · 3.12 KB
/
MinSwapsBinaryArray.java
File metadata and controls
123 lines (99 loc) · 3.12 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Account {
String accountHolder;
long accountNumber;
double balance;
// Constructor
public Account(String name, long accNo, double bal) {
this.accountHolder = name;
this.accountNumber = accNo;
this.balance = bal;
}
public void showdetails() {
System.out.println("Account Holder: " + accountHolder);
System.out.println("Account Number: " + accountNumber);
}
// Common method
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
public void showBalance() {
System.out.println("Balance: " + balance);
}
}
class SavingsAccount extends Account {
double interestRate = 5.0;
public SavingsAccount(String name, long accNo, double bal) {
super(name, accNo, bal);
}
public void addInterest() {
double interest = balance * interestRate / 100;
balance += interest;
System.out.println("Interest added: " + interest);
}
}
class CurrentAccount extends Account {
double overdraftLimit = 1000;
public CurrentAccount(String name, long accNo, double bal) {
super(name, accNo, bal);
}
public void withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Limit exceeded!");
}
}
}
class Main{
public static void main(String[] args) {
SavingsAccount s = new SavingsAccount("Charan", 1018440488, 1000);
s.showdetails();
s.deposit(500);
s.addInterest();
s.showBalance();
System.out.println("************************");
CurrentAccount c = new CurrentAccount("Kailash", 102, 2000);
s.deposit(500);
c.withdraw(2500);
c.showBalance();
s.deposit(500);
}
}
//dsa
public class MinSwapsBinaryArray {
public static int minSwaps(int[] arr) {
int n = arr.length;
// Count total number of 0s
int zeroCount = 0;
for (int num : arr) {
if (num == 0) zeroCount++;
}
// Case 1: 0s on left → count misplaced 1s
int misplacedOnes = 0;
for (int i = 0; i < zeroCount; i++) {
if (arr[i] == 1) misplacedOnes++;
}
// Case 2: 1s on left → count misplaced 0s
int misplacedZeros = 0;
for (int i = 0; i < n - zeroCount; i++) {
if (arr[i] == 0) misplacedZeros++;
}
return Math.min(misplacedOnes, misplacedZeros);
}
public static void main(String[] args) {
int[] arr = {1, 0, 1, 0, 1, 0, 1};
System.out.println(minSwaps(arr));
System.out.println("length of arr is: " + " " +arr.lencgth); // Output: 7
}
}
class cal {
public int add(int a, int b){
return a + b;
}
public static void main(String[] args){
cal obj = new cal();
obj.add(10, 20);
}
}