diff --git a/README.md b/README.md index abd044d..f7fda78 100644 --- a/README.md +++ b/README.md @@ -208,3 +208,10 @@ This is a repository containing various python programs to understand the basic Python Code for solving a popular problem of the Pavement of Roads to connect to Libraries in the City/Cities. +* [Minimum Swapping Problem](https://github.com/altruistcoder/Data-Structures-Python/blob/master/minimum_swapping.py): + + Python Code for finding the minimum number of swapping of numbers required to sort the array given as input. + +* [Consecutive Ones in Zeroes Ones Array Problem](https://github.com/altruistcoder/Data-Structures-Python/blob/master/max_consecutive_ones_zeroes.py): + + Python Code for finding the count of maximum number of consecutive 1's present in an array consisting of a 1's and 0's. \ No newline at end of file diff --git a/max_consecutive_ones_zeroes.py b/max_consecutive_ones_zeroes.py new file mode 100644 index 0000000..f59b5b7 --- /dev/null +++ b/max_consecutive_ones_zeroes.py @@ -0,0 +1,16 @@ +def getMaxConsecutiveOne(a, n): + c = 0 + res = 0 + + for i in range(0, n): + if (a[i] == 0): + c = 0 + else: + c+= 1 + res = max(res, c) + return res + +a = list(map(int, input("Enter a list of elements: ").split())) +n = len(a) +maxOnes = getMaxConsecutiveOne(a, n) +print("The maximum number of consecutive ones in the array is:", maxOnes) diff --git a/minimum_swapping.py b/minimum_swapping.py new file mode 100644 index 0000000..f2bda76 --- /dev/null +++ b/minimum_swapping.py @@ -0,0 +1,20 @@ +def minSwapping(a, n): + res = 0 + temp = a.copy() + ihash = {} + temp.sort() + for i in range(n): + ihash[a[i]] = i + start = 0 + for i in range(n): + if (a[i] != temp[i]): + res += 1 + start = a[i] + a[i], a[ihash[temp[i]]] = a[ihash[temp[i]]], a[i] + ihash[start] = ihash[temp[i]] + ihash[temp[i]] = i + return res + +a = list(map(int, input("Enter the elements of the input array: ").split())) +res = minSwapping(a, len(a)) +print("The minimum number of steps required for sorting:", res) \ No newline at end of file