Skip to content

Commit a580391

Browse files
adding Codes
1 parent 25ddc4e commit a580391

34 files changed

+493
-11
lines changed

Leetcode/FindPeakElement.class

1.03 KB
Binary file not shown.

Leetcode/FindPeakElement.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.*;
2+
public class FindPeakElement{
3+
public static void main(String[] args){
4+
final Scanner cin=new Scanner(System.in);
5+
int n=cin.nextInt();
6+
int[] nums=new int[n];
7+
for(int i=0;i<n;i++){
8+
nums[i]=cin.nextInt();
9+
}
10+
System.out.println(peakIndex(nums));
11+
}
12+
public static int PeakIndex(int[] nums){
13+
//brute forxece -O(n)
14+
for(int i=1;i<nums.length;i++){
15+
if(nums[i]<nums[i-1]){
16+
return i-1;
17+
}
18+
}
19+
if(nums[nums.length-2]==nums[nums.length-1]){
20+
return nums.length-2;
21+
}
22+
return nums.length-1;
23+
}
24+
// 0 1 2 3 4 5
25+
// 1 2 3 4 1 5
26+
// 1 2 3 4 1 5 -> mid =2(3)
27+
// 3 > 4 and 3 > 2 -> return mid
28+
// check mid-> mid -1(2) -> left mid+1
29+
// right => mid -1
30+
public static int peakIndex(int[] nums){
31+
int left=0,right=nums.length-1;
32+
while(left<=right){
33+
int mid=(left+right)/2;
34+
if((nums[mid]>nums[mid-1] && mid>0) && (mid<nums.length && nums[mid]>nums[mid+1])){
35+
return mid;
36+
}else if(mid-1>=0 && nums[mid]>nums[mid-1]){
37+
left=mid+1;
38+
}
39+
else if(mid+1<nums.length && nums[mid]<nums[mid+1]){
40+
right=mid-1;
41+
}
42+
}
43+
return -1;
44+
}
45+
}

Leetcode/removeDuplicates.class

1.19 KB
Binary file not shown.

Leetcode/removeDuplicates.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
public class removeDuplicates{
3+
public static void main(String[] args){
4+
Scanner cin=new Scanner(System.in);
5+
int n=cin.nextInt();
6+
int[] ar=new int[n];
7+
for(int i=0;i<n;i++){
8+
ar[i]=cin.nextInt();
9+
}
10+
int index=removeTwice(ar);
11+
for(int i=0;i<index;i++){
12+
System.out.print(ar[i]+" ");
13+
}
14+
}
15+
private static int remove(int[] ar){
16+
int left=1;
17+
for(int right=0;right<ar.length-1;right++){
18+
// System.out.println(ar[i]+" "+ar[i+1]);
19+
if(ar[right]!=ar[right+1]){
20+
// System.out.println("c"+ar[i]+" "+ar[i+1]);
21+
ar[left++]=ar[right+1];
22+
23+
}
24+
}
25+
return left;
26+
}
27+
public static int removeTwice(int[] ar){
28+
int left= 2;
29+
for(int right=2;right<ar.length;right++){
30+
if((ar[left-2]!=ar[right])){
31+
ar[left++]=ar[right];
32+
// lc=1;
33+
}
34+
}
35+
//System.out.println();
36+
return left;
37+
}
38+
39+
}
-1.17 KB
Binary file not shown.
1.26 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.io.*;
2+
public class Main{
3+
public static void main(String[] args)throws IOException{
4+
final BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
5+
for(int test=Integer.parseInt(br.readLine());test>0;test--){
6+
System.out.println(getShortString(br.readLine()));
7+
}
8+
}
9+
private static String getShortString(String s){
10+
if(s.length()<=10){
11+
return s;
12+
}
13+
int len=s.length()-2;
14+
String t=s.charAt(0)+String.valueOf(len)+s.charAt(s.length()-1);
15+
return t;
16+
}
17+
}

interviewbit/maxPoint.class

987 Bytes
Binary file not shown.

interviewbit/maxPoint.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner;
2+
public class maxPoint{
3+
public static void main(String[] args){
4+
Scanner cin=new Scanner(System.in);
5+
int n=cin.nextInt();
6+
int[] x=new int[n];
7+
int[] y=new int[n];
8+
for(int i=0;i<n;i++){
9+
x[i]=cin.nextInt();
10+
}
11+
for(int i=0;i<n;i++){
12+
y[i]=cin.nextInt();
13+
}
14+
System.out.println(maxCount(x,y));
15+
}
16+
private static int maxCount(int[] x,int[] y){
17+
int count=1,newSlope=0;
18+
int slope=(y[1]-y[0])/(x[1]-x[0]);
19+
for(int i=1;i<x.length;i++){
20+
try{
21+
newSlope=((y[i]-y[0])/(x[i]-x[0]));
22+
}
23+
catch(Exception e){
24+
newSlope=((y[i]-y[1])/(x[i]-x[1]));
25+
}
26+
if(newSlope==slope){
27+
count++;
28+
}
29+
}
30+
return count;
31+
}
32+
}

practice/LinkedList/1

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.*;
2+
class LinkedList{
3+
private class node{
4+
int data;
5+
node next;
6+
node(){
7+
data=0;
8+
next=null;
9+
}
10+
node(int item){
11+
data=item;
12+
next=null;
13+
}
14+
}
15+
private node head,tail;
16+
private int size=0;
17+
public void display(){
18+
node temp=this.head;
19+
while(temp!=null){
20+
System.out.print(temp.data+" ");
21+
temp=temp.next;
22+
}
23+
System.out.println(" First: "+this.head.data+" last:"+this.tail.data);
24+
25+
}
26+
public void addFirst(int item){
27+
node temp=new node(item);
28+
temp.next=this.head;
29+
this.head=temp;
30+
if(size==0) this.tail=temp;
31+
this.size++;
32+
}
33+
public void addLast(int item){
34+
node temp=new node(item);
35+
if(this.head == null){
36+
this.head=temp;
37+
}
38+
else{
39+
this.tail.next=temp;
40+
}
41+
this.tail=temp;
42+
this.size++;
43+
temp=null;
44+
}
45+
public int middleElement(){
46+
node temp=this.head;
47+
int mid=this.size/2;
48+
int i=0,count=-1;
49+
while(i<=mid){
50+
count=temp.data;
51+
temp=temp.next;
52+
i++;
53+
}
54+
return count;
55+
}
56+
public int FasterMid(){
57+
node slow=this.head;
58+
node fast=this.head;
59+
while(fast.next!=null && fast.next.next!=null){
60+
slow=slow.next;
61+
fast=fast.next.next;
62+
}System.out.println(slow.next);
63+
return slow.data;
64+
}
65+
}
66+
public class Insertion{
67+
public static void main(String[] args){
68+
LinkedList list=new LinkedList();
69+
Scanner cin=new Scanner(System.in);
70+
int n=cin.nextInt();
71+
for(int i=0;i<n;i++){
72+
list.addLast(cin.nextInt());
73+
}
74+
list.display();
75+
System.out.println(list.middleElement());
76+
list.addFirst(100);
77+
list.addLast(200);
78+
list.display();
79+
System.out.println("Faster Mid: "+list.FasterMid());
80+
81+
}
82+
}
83+
84+

0 commit comments

Comments
 (0)