sj
BAN USERthere are three ways of aproach
int[] input={2,7,5,3,0,8,1};
int traget=9;
//if no extra memmory is allowed
for(int i=0;i<input.length;i++)
{
for(int j=i+1;j<input.length;j++)
{
if(traget==input[i]+input[j])
{
System.out.println("sol 1 Pair with given sum " +
traget + " is (" + input[i] +
", "+input[j]+")");
}
}
}
//if no idea about maximum element
Hashtable<Integer,Integer>indexvaluemap=new Hashtable<Integer, Integer>();
for (int i = 0; i < input.length; i++) {
indexvaluemap.put(input[i],i);
}
for(int i=0;i<input.length;i++)
{
if(indexvaluemap.get(traget-input[i])!=null&&indexvaluemap.get(traget-input[i])!=i)
{
System.out.println("sol2 Pair with given sum " +
traget + " is (" + input[i] +
", "+(traget-input[i])+")");
}
}
//if maximum value of elements in an array is known
int MAX = 100000;
boolean[] binmap = new boolean[MAX];
for (int i=0; i<input.length; ++i)
{
int temp = traget-input[i];
// checking for condition
if (temp>=0 && binmap[temp])
{
System.out.println("sol3 Pair with given sum " +
traget + " is (" + input[i] +
", "+temp+")");
}
binmap[input[i]] = true;
}
add values between all the time ranges i.e(starttime to endtime-1 ) to hashset .
..size of the hashset will be the tottal working time of the television
import java.util.ArrayList;
import java.util.HashSet;
public class tottalrunningtime {
public static class time {
int start;
int end;
time(int startm, int end) {
this.start = startm;
this.end = end;
}
}
public static void main(String[] args) {
ArrayList<time> timeranges = new ArrayList<time>();
timeranges.add(new time(1, 4));
timeranges.add(new time(6, 8));
timeranges.add(new time(2, 4));
timeranges.add(new time(7, 9));
timeranges.add(new time(10, 15));
HashSet<Integer> ontimecounter = new HashSet<Integer>();
for (int i = 0; i < timeranges.size(); i++) {
for (int j = timeranges.get(i).start; j < timeranges.get(i).end; j++) {
ontimecounter.add(j);
}
}
System.out.println(ontimecounter.size());
}
}
assuming lists are sorted in ascending order
public static int[] getmergedarray(ArrayList<Integer> l1,
ArrayList<Integer> l2, int k) {
int[] merged = new int[k];
int l1_index = 0;
int l2_index = 0;
for (int i = 0; i < k; i++) {
if (l1.size() > l1_index && l2.size() > l2_index
&& l1.get(l1_index) <= l2.get(l2_index)) {
merged[i] = l1.get(l1_index);
l1_index++;
} else if (l1.size() > l1_index && l2.size() > l2_index) {
merged[i] = l2.get(l2_index);
l2_index++;
} else if (l1.size() > l1_index) {
merged[i] = l1.get(l1_index);
l1_index++;
}
else if (l2.size() > l2_index) {
merged[i] = l2.get(l2_index);
l2_index++;
}
}
return merged;
}
}
- sj September 17, 2016public static boolean istergetfound(int target ,ArrayList<Integer> list1,ArrayList<Integer> list2)
{
for(int i=0;i<list1.size();i++)
{
if(list1.get(i)>target)
continue;
int l1=list1.get(i);
for(int j=0;j<list2.size();j++)
{
int l2=list2.get(j);
if(l2+l1==target)
{
System.out.println("result found"+l1+"+"+l2+"="+target);
return true; }
}
}
return false;
}
public class Myclass {
public static int find(int [] A, int k){
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i=0;i<A.length;i++){
pq.offer(A[i]);
}
int n = -1;
k=pq.size()-k+1;
while(k>0){
n = pq.poll();
k--;
}
return n;
}
public static void main(String[] args) {
int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 };
int k = 4;
System.out.println("4th biggest element:" + find(A,4));
}
}
1.add all the given nodes to a Que,store the number of nodes given in separate variable
2.count the nodes of tree using recursion method by starting from each element on the Que
3.at any stage if count equals the total number of elements then that element is the root
4.else return null
1.check if the difference between the length of two strings are grater than 1
2.if true return false
3.else they are equal or differ in one character length
4.compare each character of one string with another and count the dissimilarity if found
5.if count=1;true
6.else false
public void riverse(int n)
{
for(i=1;i<=size;i++)
{
if(i==n)
break;
ptr=ptr.getlink();
}
firstprev=ptr;
first=ptr.getlink();
i=1;
while(ptr!=null&&i<=n)
{
ptr=ptr.getlink();
}
if(ptr==null)
last=end;
lastnext=null;
else
last=ptr;
lastnext=ptr.getlink();
i=1;
n=first;
do{
next=ne.getlink();
nnext=next.getlink();
next.setlink(ne);
next=nnext;
n=next;
}
firstprev.setlink(last);
first.setlink(lastnext);
}
3 steps pf weighing required
1 .devide group of 12 stones to group of 6
2 .find the highest weighing group
3.devide the most weghted group to group of 3
4.find most weighted among these two groups having 3 stones
5.in that three stones weigh about any two stones if they are equal the third stone have higher weight. else stone in weigh scale have higher weight.
two solutions: (1) using brute force check having time complexity O(n2)
2. add elements two a binary search tree and count number of elements in each node
- sj October 30, 2016complexity will be nlogn