Skip to content

Commit

Permalink
Max sub Array.
Browse files Browse the repository at this point in the history
  • Loading branch information
PRATHAP KUDUPU authored and PRATHAP KUDUPU committed May 12, 2017
1 parent 7474f2d commit 9bcf94f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
Binary file modified bin/Algorithms/Arrays/MajorityElement.class
Binary file not shown.
Binary file added bin/Algorithms/Arrays/MaximumSubArray.class
Binary file not shown.
Binary file modified bin/Home.class
Binary file not shown.
41 changes: 28 additions & 13 deletions src/Algorithms/Arrays/MajorityElement.java
Expand Up @@ -9,18 +9,33 @@

public class MajorityElement {

public static int get(int[] num) {
public static int get(int [] arr)
{
//We are taking the first element of the array
int major=arr[0], count=1;

for(int i=1;i<arr.length;i++)
{
//If count is set to zero during iteration we need to reset the count
if (count==0)
{
count++;
major=arr[i];
}
//If current element is same as major then increment the count
else if(major==arr[i])
{
count++;
}
//If we are here than we know that the current element is not the same then we would decrement the count
else
{
count--;
}

}
return major;

}

int major=num[0], count = 1;
for(int i=1; i<num.length;i++){
if(count==0){
count++;
major=num[i];
}else if(major==num[i]){
count++;
}else count--;

}
return major;
}
}
19 changes: 19 additions & 0 deletions src/Algorithms/Arrays/MaximumSubArray.java
@@ -0,0 +1,19 @@
package Algorithms.Arrays;

public class MaximumSubArray {
public static int get(int arr [])
{
int maxsoFar=arr[0], maxEndingHere=arr[0];
for (int i=1;i<arr.length;i++)
{
//Get the max in the iteration
maxEndingHere=Math.max(maxEndingHere+arr[i], arr[i]);
//Get the max so far
maxsoFar=Math.max(maxEndingHere, maxsoFar);

}
return maxsoFar;

}

}
19 changes: 16 additions & 3 deletions src/Home.java
Expand Up @@ -343,13 +343,26 @@ public static void main(String args[]){
{
System.out.println("Pascal Triangle : "+ i);
}
*/
/***
* Majority element
*
*/
/*
/*
int [] arr= {3, 3, 4, 2, 4, 4, 2};
int maj=Algorithms.Arrays.MajorityElement.get(arr);
System.out.println("Majority Element : "+ maj);



*/
/***
* Maximum Sub Array
*
*/
int [] arr= {-2,-3,4,-1,-2,1,5,-3};
int max=Algorithms.Arrays.MaximumSubArray.get(arr);
System.out.println("Maximum SubArray : "+ max);




Expand Down

0 comments on commit 9bcf94f

Please sign in to comment.