Interview Question
Country: United States
Its actually pretty straight forward. Even sum can either be obtained by adding 2 odd or 2 even numbers. So determine how many even and odd number in the array. That is simple.
And then to find total even sums of two numbers, all you need is a simple formula.
package com.google.practice;
public class EvenSum {
/**
* @param args
*/
public static void main(String[] args) {
int[] arr = {1,2,5,3,6,7,1,4,0,-4,1,-2,1,0,1};
int oddCount=0,evenCount=0;
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0)
evenCount++;
else
oddCount++;
}
System.out.println(((evenCount-1)*(evenCount)/2)+((oddCount-1)*(oddCount)/2));
}
}
#include<stdio.h>
- Arvind June 25, 2014#include<conio.h>
void main()
{
int a[10],cnt=0,q;
printf("Enter the Length");
scanf_s("%d", &q);
printf("Enter the nos in the array \n");
for (int i = 0; i < q; i++)
{
printf("\n");
scanf_s("%d", &a[i]);
}
for (int j = 0; j < q; j++)
{
for (int k = j + 1; k < q; k++)
{
if ((a[j] + a[k]) % 2 == 0)
{
cnt += 1;
}
}
}
printf("\n%d\n", cnt);
_getch();
}