Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
itsgaurav355 committed Mar 30, 2023
0 parents commit 108bff5
Show file tree
Hide file tree
Showing 499 changed files with 10,225 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Coding.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="com" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
67 changes: 67 additions & 0 deletions DSA/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.DSA;

public class BinarySearch {

public static void main(String[] args) {
int[] arr = {-18, -12, -4, 0, 2, 3, 4, 15, 16, 18, 22, 45, 89};
int target = 22;
int ans = binarySearch(arr, target);
System.out.println(ans);
}

// return the index
// return -1 if it does not exist
static int binarySearch(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;

while(start <= end) {
// find the middle element
// int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java
int mid = start + (end - start) / 2;

if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
// ans found
return mid;
}
}
return -1;
}

static int orderAgnosticBS(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;

// find whether the array is sorted in ascending or descending
boolean isAsc = arr[start] < arr[end];

while(start <= end) {
// find the middle element
// int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java
int mid = start + (end - start) / 2;

if (arr[mid] == target) {
return mid;
}

if (isAsc) {
if (target < arr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if (target > arr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
return -1;
}
}
35 changes: 35 additions & 0 deletions DSA/BinarySearchSqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.DSA;

public class BinarySearchSqrt {
public static void main(String[] args) {
int n = 40;
int p = 3;
//pretty painting or formatting
System.out.printf("%.3f",sqrt(n , p));

}
//Time\: O(log(n))
static double sqrt(int n , int p){
int s = 0;
int e = n;

double root = 0.0 ;
while ( s <= e) {
int m =s +(e -s) /2 ;
if(m*n>n){
e= m-1;
}else{
s = m+1;
}
}
double incr = 0.1;
for(int i=0;i< p; i++){
while(root * root <=n){
root += incr;
}
root -= incr;
incr /= 10;
}
return root;
}
}
75 changes: 75 additions & 0 deletions DSA/BitwiseOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.DSA;

public class BitwiseOperator {
public static void main(String[] args) {
// System.out.println(isOdd(10));
// int arr[] ={2,3,3,4,2,6,4};
// System.out.println(isUnique(arr));
// int base =2;
// int power =4;
//
// int ans = power(base,power);
// System.out.println(ans);
// System.out.println(magicNumber(n));
int n= 45;
System.out.println(Integer.toBinaryString(n));
System.out.println(setBits(n));

}
static int setBits(int n)
{
//setBits == 1
int count= 0;
// while(n > 0){
// count++;
// n -= (n & -n);
// } OR
while(n > 0)
{
count++;
n = n & (n-1);

}

return count;
}
static int power(int base, int power)
{
int ans =1;
while(power >0 ){
if((power & 1) == 1)
{
ans *=base;
}
base *= base;
power =power >> 1;
}
return ans;
}
static boolean isOdd(int n){
return (n & 1) == 1;
}
static int isUnique(int arr[]){
//Debug for step-by-step solution
int unique=0;
for(int n: arr){
unique ^= n;
}
return unique;
}
static int magicNumber(int n){
int ans =0;
int base = 5 ;
while(n > 0){
int last = n & 1;
n = n >> 1 ;
ans +=last *base;
base = base * 5;
}
return ans;
}
static int noOfDigits(int n,int base)
{
return (int)(Math.log(n)/Math.log(base))+1;
}
}
30 changes: 30 additions & 0 deletions DSA/CyclicSorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.DSA;
import java.util.Arrays;

public class CyclicSorting {
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1};
sort(arr);
System.out.println(Arrays.toString(arr));
}

static void sort(int[] arr) {
int i = 0;
while (i < arr.length) {
int correct = arr[i] - 1;
if (arr[i] != arr[correct]) {
swap(arr, i , correct);
} else {
i++;
}
}
}

static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}


}
Loading

0 comments on commit 108bff5

Please sign in to comment.