Created
April 14, 2020 04:44
-
-
Save SumitJainUTD/d8e09e9baf3838abf0950ee09de88405 to your computer and use it in GitHub Desktop.
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
import java.util.Arrays; | |
public class CountPairsWithEvenSum { | |
public static void countPairs(int [] input){ | |
System.out.println("Given Input: " + Arrays.toString(input)); | |
int evenCount=0; | |
int oddCount=0; | |
for (int i = 0; i <input.length ; i++) { | |
if(input[i]%2==0) | |
evenCount++; | |
else | |
oddCount++; | |
} | |
int evenPairs = evenCount*(evenCount-1)/2 + oddCount*(oddCount-1)/2; | |
System.out.println("Number of even pairs: " + evenPairs); | |
} | |
public static void main(String[] args) { | |
int [] input = {6, 7, 1, 3, 2, 5, 4}; | |
countPairs(input); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment