Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#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();
}

- Arvind June 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is O(n^2)

- AlgoAlgae June 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Give an example,please.

- glebstepanov1992 June 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
	}

}

- AlgoAlgae June 26, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More