-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add java language code solution and its image for the AdjacentElement…
…sProduct challenge
- Loading branch information
Showing
2 changed files
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Arrays; | ||
public class Adjacent_elements_product{ | ||
public static void main (String args[]){ | ||
int testArray [] = new int[] {3, 6, -2, -5, 7, 3}; | ||
System.out.format(" The max adjacent numbers product for the array %s is %d %n", Arrays.toString(testArray), solution(testArray)); | ||
int testArray2 [] = new int[] {1, 0, 1, 0, 1000}; | ||
System.out.format(" The max adjacent numbers product for the array %s is %d %n", Arrays.toString(testArray2), solution(testArray2)); | ||
} | ||
static int solution(int[] inputArray) { | ||
int maxProduct = inputArray[0]*inputArray[1]; | ||
for(int i = 2; i<inputArray.length; i++){ | ||
int currentProduct = inputArray[i]* inputArray[i-1]; | ||
if(currentProduct > maxProduct) | ||
maxProduct= currentProduct; | ||
} | ||
return maxProduct; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.