Skip to content

Commit 4c178cf

Browse files
authored
Merge branch 'codemistic:main' into main
2 parents 553b8b5 + 1db6543 commit 4c178cf

File tree

106 files changed

+6154
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+6154
-0
lines changed

121 Leetcode/121 leetcode.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
maxProfit = 0
4+
currentMax = 0
5+
6+
for i in reversed(prices):
7+
currentMax = max(currentMax, i)
8+
profit = currentMax - i
9+
maxProfit = max(profit, maxProfit)
10+
return maxProfit

Arrays/01_static_and_dynamicArray.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
int main()
4+
{
5+
int A[5] = { 1,2,3,4,5 }; // this is allocated inside stack memory
6+
int* p; // pointer of array
7+
int i;
8+
p = (int*)malloc(5 * sizeof(int)); // dynamically createing array inside heap
9+
p[0] = 1; // initializing all the terms in array
10+
p[1] = 2;
11+
p[2] = 3;
12+
p[3] = 4;
13+
p[4] = 5;
14+
15+
for (i = 0; i < 5; i++) // iteration for normal array
16+
{
17+
printf("%d ", A[i]);
18+
}
19+
printf("\n");
20+
for (i = 0; i < 5; i++) // iteration for the dynamic array
21+
{
22+
printf("%d ",p[i]);
23+
}
24+
return 0;
25+
}

Arrays/01_static_and_dynamicArray.exe

40.4 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Normal array is created in stack
2+
and stack used memory array cannot be change
3+
tht is we can not incresae the size of array */
4+
5+
#include <stdio.h>
6+
#include<stdlib.h>
7+
int main ()
8+
{
9+
int *p,*q; // Taking two pointers
10+
int i;
11+
p=(int *)malloc(5*sizeof(int)); // array is created in heap memory
12+
p[0]=9;p[1]=7;p[2]=22;p[3]=11;p[4]=12;
13+
14+
q= (int *) malloc(10*sizeof(int)); // increasing size of array in heap memory
15+
16+
for (i=0;i<5;i++)
17+
q[i]=p[i];
18+
19+
free(p);
20+
p=q;
21+
q=NULL;
22+
23+
for (i=0;i<5;i++)
24+
printf("%d \n",p[i]);
25+
return 0;
26+
}
Binary file not shown.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Creating 2-D array
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
int main ()
5+
{ // normal delaration and initialization
6+
int A[3][4]={{1,2,3,4},{2,4,6,8},{1,3,5,7}}; // stack memory
7+
8+
// pointer decleration
9+
int *B[3];
10+
int **C; // double pointer , completly in heap memory
11+
int i,j;
12+
13+
B[0]=(int *)malloc(4*sizeof(int)); // Creating memory in heap
14+
B[1]=(int *)malloc(4*sizeof(int));
15+
B[2]=(int *)malloc(4*sizeof(int));
16+
17+
C=(int **)malloc(3*sizeof(int *));
18+
C[0]=(int *)malloc(4*sizeof(int));
19+
C[1]=(int *)malloc(4*sizeof(int));
20+
C[2]=(int *)malloc(4*sizeof(int));
21+
22+
for (i=0;i<3;i++)
23+
{
24+
for (j=0;j<4;j++)
25+
printf("%d ",A[i][j]);
26+
printf("\n");
27+
}
28+
return 0;
29+
}
30+
40.4 KB
Binary file not shown.

Arrays/04_Array_ADT.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include<stdlib.h>
3+
struct Array
4+
{
5+
int* A;
6+
int size;
7+
int length;
8+
};
9+
10+
// fxn for display all the elements of the array
11+
12+
void Display(struct Array arr)
13+
{
14+
int i;
15+
printf("Elements Are\n");
16+
for (i = 0; i < arr.length; i++)
17+
printf("%d ",arr.A[i]); // it will display all the elements
18+
}
19+
int main()
20+
{
21+
struct Array arr;
22+
int n; // how many no is going to be inserted
23+
int i;
24+
printf("Enter size of an array ");
25+
scanf_s("%d", &arr.size);
26+
arr.A = (int*)malloc(arr.size * sizeof(int)); // dynamically created array
27+
arr.length = 0;// intially no elements
28+
printf("Enter number of numbers ");
29+
scanf_s("%d", &n);
30+
printf("Enter All the elements ");
31+
for (i = 0; i < n; i++)
32+
scanf_s("%d", &arr.A[i]); // enter the elements of an array
33+
arr.length = n; // set length as n
34+
35+
36+
Display(arr);
37+
38+
39+
return 0;
40+
}

Arrays/05_inserting_an_element.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
using namespace std;
3+
struct Array
4+
{
5+
int A[10];
6+
int size;
7+
int length;
8+
};
9+
void Display(struct Array arr)
10+
{
11+
int i;
12+
cout<<"printing all the elements "<<endl;
13+
for (i = 0; i < arr.length; i++)
14+
cout << arr.A[i] << " ";
15+
}
16+
void Add(struct Array *arr , int x) // element to be inserted
17+
{
18+
if (arr->length < arr->size)
19+
arr->A[arr->length++] = x; // increment the size of array and fit the elements // it will take the constant time
20+
}
21+
void Insert(struct Array* arr, int index, int x) // the index at which we want to enter // and the value we want tpo enter
22+
{
23+
int i;
24+
for (i = arr->length; i > index; i--) // we will strat from last of the array and shift the elements upto the given index
25+
{
26+
arr->A[i] = arr->A[i - 1]; // shifting the elements to next // i is on some index and copy the value from previous index
27+
}
28+
arr->A[index] = x; // set the value at the particular index
29+
arr->length++; // increment the size of length
30+
// work done shifting of elelmets and copying elements
31+
// best case : --------------- min : O(1) // there may be zero shifting
32+
// worst case : --------------- max : O(n)// there may be n shifing
33+
34+
}
35+
36+
int main() {
37+
int no;
38+
struct Array arr;
39+
cout << "Enter the size of the array " << endl;
40+
cin >> arr.size;
41+
arr.A = new int[arr.size];
42+
arr.length = 0;
43+
cout << "Enter the length of array" << endl;
44+
cin >> no;
45+
cout << "Enter all the elements of array" << endl;
46+
for (int i = 0; i < no; i++)
47+
cin >> arr.A[i];
48+
arr.length = no;
49+
Add(&arr, 10);
50+
Insert(&arr,3,25);
51+
Display(arr);
52+
53+
return 0;
54+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
3+
struct Array
4+
{
5+
int A[10];
6+
int length;
7+
int size;
8+
};
9+
10+
int Display(struct Array arr)
11+
{
12+
int i;
13+
printf("Elements Are\n");
14+
for (i=0;i<arr.length;i++)
15+
printf("%d ",arr.A[i]);
16+
return 0;
17+
}
18+
int Delete (struct Array *arr, int index)
19+
20+
/* Difference between Dot(.) and Arrow(->) operator:
21+
22+
The Dot(.) operator is used to normally access members of a structure or union.
23+
The Arrow(->) operator exists to access the members of the structure or the unions using pointers. */
24+
{
25+
int x=0;
26+
int i;
27+
if (index>=0 && index <arr->length)
28+
{
29+
x=arr->A[index]; // first of all take out the element from the particular index you want to del---------------------> taking 1 unit of time
30+
for (i=index;i<arr->length-1;i++) // shift the element in forward direnction and stop at length -1 so that array can not have any vacant space
31+
arr->A[i]=arr->A[i+1]; // at the place of a[i] copy the next element i.e a[i+1]
32+
arr->length--;//at last the length will be decreatmented ----------------> 1 unit of time -------> so best time is 1+1 =2
33+
34+
// min shifting is 0 and max shifting is n ----> Best case time is O(1) -> Worst Case time is ----> O(n)
35+
return x;
36+
}
37+
return 0;
38+
}
39+
int main ()
40+
{
41+
struct Array arr1={{1,2,3,4,56,5,2},10,5};
42+
printf("%d\n",Delete(&arr1,0));
43+
Display(arr1);
44+
return 0;
45+
}

Arrays/07_Linearsearch.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<stdio.h>
2+
struct Array {
3+
int A[20];
4+
int length;
5+
int size;
6+
7+
};
8+
void Display (struct Array arr)
9+
{
10+
int i;
11+
printf("\nEnter elements\n");
12+
for (i=0;i<arr.length;i++)
13+
printf("%d ",arr.A[i]);
14+
}
15+
int LinearSearch (struct Array arr, int key)
16+
{
17+
int i;
18+
for (i=0;i<arr.length;i++)
19+
{
20+
if (key==arr.A[i]) // formula
21+
return i;
22+
}
23+
return -1;
24+
}
25+
int main ()
26+
{
27+
struct Array arr1= {{2,3,4,2,3,4,5,88,9 },20,5 };
28+
printf("The element is at %dth index",LinearSearch(arr1,88));
29+
Display(arr1);
30+
return 0;
31+
}

Arrays/08_Improving_Linear_search.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<stdio.h>
2+
struct Array {
3+
int A[20];
4+
int length;
5+
int size;
6+
7+
};
8+
void Display (struct Array arr)
9+
{
10+
int i;
11+
printf("\nEnter elements\n");
12+
for (i=0;i<arr.length;i++)
13+
printf("%d ",arr.A[i]);
14+
}
15+
void swap(int *x,int *y)
16+
{
17+
int temp=*x;
18+
*x=*y;
19+
*y=temp;
20+
}
21+
int LinearSearch(struct Array *arr,int key)
22+
{
23+
int i;
24+
for(i=0;i<arr->length;i++) // iterating through the list
25+
{
26+
if(key==arr->A[i]) // checking whether elements is equal to the key elements or not
27+
{
28+
swap(&arr->A[i],&arr->A[0]); // this is move to 1st method // from this we can reduce time to O(1) // when we will search the same element again
29+
return i;
30+
}
31+
}
32+
return -1;
33+
}
34+
35+
int main ()
36+
{
37+
struct Array arr1= {{2,3,4,2,3,4,5,88,9 },20,5 };
38+
printf("The element is at %dth index",LinearSearch(&arr1,9));
39+
Display(arr1);
40+
return 0;
41+
}

Arrays/09_Binary_Search_using_loop.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<stdio.h>
2+
struct Array
3+
{
4+
int A[10];
5+
int size;
6+
int length;
7+
};
8+
void Display(struct Array arr)
9+
{
10+
int i;
11+
printf("\nElements are\n");
12+
for(i=0;i<arr.length;i++)
13+
printf("%d ",arr.A[i]);
14+
}
15+
int BinarySearch(struct Array arr,int key)
16+
{
17+
int l,mid,h;
18+
l=0;
19+
h=arr.length-1;
20+
while(l<=h)
21+
{
22+
mid=(l+h)/2;
23+
if(key==arr.A[mid])
24+
return mid;
25+
else if(key<arr.A[mid])
26+
h=mid-1;
27+
else
28+
l=mid+1;
29+
}
30+
return -1;
31+
}
32+
int main ()
33+
{
34+
struct Array arr1= {{1,2,43,55,57,87,98,444,999},10,9};
35+
printf("The element is present at %d position",BinarySearch(arr1,98));
36+
Display(arr1);
37+
return 0;
38+
39+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<stdio.h>
2+
struct Array {
3+
int A[10];
4+
int length;
5+
int size;
6+
7+
};
8+
void Display (struct Array arr)
9+
{
10+
int i;
11+
printf("Enter all the elements ");
12+
for (i=0;i<arr.length;i++)
13+
printf("%d ",arr.A[i]);
14+
15+
}
16+
int RBinSearch(int a[],int l,int h,int key)
17+
{
18+
19+
int mid=0;
20+
if(l<=h)
21+
{
22+
mid=(l+h)/2;
23+
if(key==a[mid])
24+
return mid;
25+
else if(key<a[mid])
26+
return RBinSearch(a,l,mid-1,key);
27+
}
28+
else
29+
return RBinSearch(a,mid+1,h,key);
30+
return -1;
31+
}
32+
33+
int main ()
34+
{
35+
36+
struct Array arr1={{1,3,5,6,7,8,77,777,888},10,8};
37+
printf("The element is at index %d\n",RBinSearch(arr1.A,0,arr1.length,5));
38+
Display(arr1);
39+
return 0;
40+
}

0 commit comments

Comments
 (0)