.Net Interview Questions and Answers

Search:

# C# Interview question :-Find Pair Of Integers in Array whose Sum is Given Number.

By : Shiv Prasad Koirala | Oct 18th, 2016 | Views : 876 | Comments : 0

Recently, I have been to an interview and was asked to solve a problem with solution not having complexity of O(N square).

The problem was as follows :-

A[0]=5

A[1]=4

A[2]=-3

A[3]=1

A[4]=2

A[5]=9

Now we need to find the complementary set of elements having sum of 6.

That is combination of 2 elements should give a sum of 6, so we had to write an algorithm to get the total number of such pairs in above example the total number of pairs will be 3.

I could achieve this using 2 for loops but the interviewer wanted a better way.

Below is the code with the comments

```int[] arr = newint[]  5, 4, -3, 1, 2, 9 };
// Step 1 :- Sort the array in ascending manner
Array.Sort(arr);
int k = 6;
int left = 0;
int right = arr.Length - 1;
// Step 2 :- Take two pointers one which moves from
// the left of the array and the second from right of the array
while (left < right)

int sum = arr[left] + arr[right];
// Step 3 :- If sum is proper then add from the left
// substract from the right
if (sum == k)

Console.WriteLine(arr[left] + "  " +  arr[right]);
left = left + 1;
right = right - 1;
}
elseif (sum < k)

// Step 4 :- If target total is greater than
// the sum then advance from the left
left = left + 1;
}
elseif (sum > k)

// Step 5 :- If target total is less than
// the sum then substract from the right
right = right - 1;
}
}

1 1 1 2 1

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi