## yash.varshney05

BAN USERAlgo-

1-sort the array.

2-set i=0

3-n=arr.length

4-if n%2=0 then j=n-1 else j=n-2 //keep j to maintain

odd index and i for even index

5-if i-j =1 return

5-if i-j <1 then i++ else j--

O(n)

public static void main(String[] args) {

// TODO Auto-generated method stub

int[]a=new int[]{1,2,2,4,1,3,6,6,6,7,7,7,7,7};

int b[]=new int[]{1,2,4,3,6,7};

int[]res=new int[a.length];

int i,j,k=0;

for(i=0;i<b.length;i++)

{

for(j=0;j<a.length;j++)

{

if(b[i]==a[j])

{

res[k]=a[j];

k++;

}

}

}

for( k=0;k<res.length;k++)

System.out.println(res[k]);

}

public class FindRange {

public static void main(String[] args) {

int a[]=new int[]{0,2,3,3,3,10,10};

finRange(a,3);

}

public static void finRange(int a[], int num)

{

int lastIndex=-1;

int firstIndex=-1;

boolean flag=true;

for(int i=0;i<a.length;i++)

{

if(a[i]==num&&flag)

{

firstIndex=i;

lastIndex=i;

flag=false;

}

else

if(a[i]==num)

lastIndex=i;

}

System.out.println("firstIndex="+firstIndex+"lastIndex="+lastIndex);

}

}

public class EncodeString {

public static void main(String[] args) {

String s = "aaaabbccdd";

char a[] = s.toCharArray();

StringBuilder sb = new StringBuilder();

int count = 1;

TreeMap<Character, Integer> map = new TreeMap();

for (int i = 0; i < a.length; i++) {

if (map.containsKey(a[i])) {

count = map.get(a[i]);

count++;

map.put(a[i], count);

} else

map.put(a[i], 1);

}

for (Object key : map.keySet()) {

System.out.print(key.toString() + map.get(key).toString());

}

}

}

public static int findNumRotation(int[] a) {

if (a.length <= 1) {

return 0;

}

int beg = 0, end = a.length - 1, mid, temp, index;

temp = a[0];

index = 0;

while (beg <= end) {

mid = (beg + end) / 2;

if (a[mid] < temp) {

temp = a[mid];

index = mid;

}

if (a[mid] <= a[end]) {

end = mid - 1;

} else {

beg = mid + 1;

}

}

return index;

}

This above code is not working plz check

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Here is the pseudo code-

- yash.varshney05 June 02, 2014String[]={"ted","mark","mark",,"ted"}

String pre=s[0];

list.add(pre);

for(int i=1;i<s.len;i++)

{

if(pre!=s(i))

list.add(s(1));

pre=s(i);

}

return list;

O(n)