Deven Bhooshan
BAN USERsorry i forgot to mention that you have to search only in i+1 to n elements . I have edited the answer.
- Deven Bhooshan July 12, 2012You can do this in O(nlogn) time complexity.
First sort the elements [O(nlogn)]
now start from the very first element and binary search (sum-firstelement) in i+1 to n.can be done in logn
Do so for every other number.
Overall time comlexity is still O(nlogn)
Example:
a={3,2,5,1,8,0} sum to be want 8
Sorted array={0,1,2,3,5,8};
start with 0 and binary search (8-0=8) in i+1 to n
and then start with 1 binary search (8-7) in i+1 to n
and so on :)
Suppose there is a class Company and has method cometToOffice().
There are various types of employees in the company. Some(Say All Managers) are having personal Cars and others(Say Salesmen) uses public transport to go office.
Then for both (Manager and Saleman ) you will write separate method comToOfficeByCar() and ComeToOfficeByPublicTransport(). This is not we want in java. If suppose there are 100 ways by which employees go to office. Then you will have to write separate method for them too.
Using OOP what you can do is this. override the ComToOffice method in each subclass of Company and you will not need to remember the names of 100s of various functions.
And also you can put the general functionality in the super class method and call it using super keyword whenever required.
I hope this example helped you somehow :)
You can do it in NlogN using Divide and conquer method(Merge Sort).
- Deven Bhooshan July 07, 2012class dev{
public static void main(String args[]){
int a[]={1,2,3,4};
int b[]={1,3,4};
a=b;
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
}
Output-
1
3
4
Please explain what you want to tell with examples. :)
- Deven Bhooshan July 13, 2012