Skip to content
20 changes: 20 additions & 0 deletions Decimal_2_Binary
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/stdc++.h>
using namespace std;

string decimal_to_binary(int n)
{
string ans;
while(n>0){
int current_bit = n&1;
ans+=current_bit+'0';
n>>=1;
}
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
string binary = decimal_to_binary(244);
cout << binary << endl;
return 0;
}
52 changes: 52 additions & 0 deletions Jump_Search
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;

int jumpSearch(int arr[], int x, int n)
{
// Finding block size to be jumped
int step = sqrt(n);

// Finding the block where element is
// present (if it is present)
int prev = 0;
while (arr[min(step, n)-1] < x)
{
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}

// Doing a linear search for x in block
// beginning with prev.
while (arr[prev] < x)
{
prev++;

// If we reached next block or end of
// array, element is not present.
if (prev == min(step, n))
return -1;
}
// If element is found
if (arr[prev] == x)
return prev;

return -1;
}


int main()
{
int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610 };
int x = 55;
int n = sizeof(arr) / sizeof(arr[0]);

// Find the index of 'x' using Jump Search
int index = jumpSearch(arr, x, n);

// Print the index where 'x' is located
cout << "\nNumber " << x << " is at index " << index;
return 0;
}
85 changes: 85 additions & 0 deletions LinkedList_Pallindrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* next;
node(){}
node(int val)
{
data=val;
next=NULL;
}
};
node* insertlist(node* head)
{
int c=1,i=1,val;node* temp=head;
while(c!=0)
{
cout<<"ENTER DATA OF NODE "<<i<<" : ";
cin>>val;
node *newnode=new node(val);
temp->next=newnode;
temp=newnode;
cout<<"ENTER 1 IF YOU WANT MORE NODES ELSE 0: ";
cin>>c;
i++;
}
head=head->next;
return head;
}
node* reverselist(node* head,node* newlist)
{
node* temp=head->next;node* prev=head;
while(temp!=NULL)
{
node* head2=new node();
head2->data=prev->data;
head2->next=newlist;
newlist=head2;
prev=temp;
temp=temp->next;
}
node* head2=new node();
head2->data=prev->data;
head2->next=newlist;
newlist=head2;
return newlist;
}
void printlist(node* head)
{
node* temp=head;
while(temp->next!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<temp->data;
}
void pallindromecheck(node* head,node* newlist)
{
node* temp=head;node* temp2=newlist;int c=0;
while(temp!=NULL&&c==0)
{
if(temp->data!=temp2->data)
{
c++;
}
temp=temp->next;
temp2=temp2->next;
}
if(c==0)
cout<<"THE NUMBER IS PALLINDROME";
else
cout<<"THE NUMBER IS NOT PALLINDROME";
}
int main()
{
node* newlist=NULL;
node *head=new node(0);
head=insertlist(head);
newlist=reverselist(head,newlist);
//printlist(head);
pallindromecheck(head,newlist);
}
14 changes: 14 additions & 0 deletions Ternary_Search
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
double ternary_search(double l, double r) {
double eps = 1e-9; //set the error limit here
while (r - l > eps) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
double f1 = f(m1); //evaluates the function at m1
double f2 = f(m2); //evaluates the function at m2
if (f1 < f2)
l = m1;
else
r = m2;
}
return f(l); //return the maximum of f(x) in [l, r]
}
21 changes: 21 additions & 0 deletions Year_month_day
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package clz;
import java.util.Scanner;
public class YearMonthDay {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Days : ");
int n = sc.nextInt();

int year = n/365;
System.out.print("Your result is : "+year+" years ");
int year_rem = n%365;

int month = year_rem/30;
System.out.print(month+" months ");
int month_rem = year_rem%30;

System.out.println(month_rem+" days.");

sc.close();
}