Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// { Driver Code Starts
//Initial Template for Java

import java.io.*;
import java.util.*;

public class GFG {
public static void main (String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T>0)
{
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}

int key =sc.nextInt();
Solution g = new Solution();
System.out.println(g.binarysearch(arr,n,key));
T--;
}
}
}

// } Driver Code Ends


//User function Template for Java

class Solution {

static int bs(int a[],int l,int h,int k){
if(l<=h){
int m=(l+h)/2;
if(a[m]==k)
return m;
else if(a[m]<k)
return bs(a,m+1,h,k);
else
return bs(a,l,m-1,k);
}else
return -1;
}


int binarysearch(int arr[], int n, int k){
// code here
int ans=bs(arr,0,n-1,k);
return ans;
}
}
56 changes: 56 additions & 0 deletions Factorials of large numbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// { Driver Code Starts
//Initial Template for Java

import java.io.*;
import java.util.*;

class Hacktober
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int N = sc.nextInt();
Solution ob = new Solution();
ArrayList<Integer> ans = ob.factorial(N);
for (Integer val: ans)
System.out.print(val);
System.out.println();
}
}
}// } Driver Code Ends


//User function Template for Java

class Solution {
static ArrayList<Integer> factorial(int N){
//code here
ArrayList<Integer> a=new ArrayList<>();
a.add(1);
int i,;
for(i=2;i<=N;i++){
mul(i,a);
}
//System.out.println(a);
Collections.reverse(a);
return a;
}
static void mul(int x, ArrayList<Integer> l){
int i, prod,carry=0;

for(i=0;i<l.size();i++){
prod=l.get(i) * x +carry;
l.set(i,prod%10);
carry=prod/10;
}
while (carry!=0)
{
l.add(carry % 10);
carry = carry / 10;
}

}
}
70 changes: 70 additions & 0 deletions Middle Element LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Given a singly linked list of N nodes.
// The task is to find the middle of the linked list. For example, if the linked list is
// 1-> 2->3->4->5, then the middle node of the list is 3.
// If there are two middle nodes(in case, when N is even), print the second middle element.
// For example, if the linked list given is 1->2->3->4->5->6, then the middle node of the list is 4.



import java.util.*;
import java.io.*;

class Node{
int data;
Node next;

Node(int x){
data = x;
next = null;
}

}
class Middle{
static void printList(Node node)
{
while (node != null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0){
int n = sc.nextInt();
Node head = new Node(sc.nextInt());
Node tail = head;
for(int i=0; i<n-1; i++)
{
tail.next = new Node(sc.nextInt());
tail = tail.next;
}
Solution g = new Solution();
int ans = g.getMiddle(head);
System.out.println(ans);
//printList(head);
t--;
}
}
}

class Solution
{
int getMiddle(Node head)
{
// Your code here.
Node n=head;
int c=0;
while(n.next!=null){
c++;
n=n.next;
}
c=((c & 1)==0)?c/2:(c/2)+1;
n=head;
for(int i=0;i<c;i++)
n=n.next;
return(n.data);
}
}