diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..f912847 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x86", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "C:/MinGW/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x86", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c90ad5c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/Users/akans/Desktop/hacktoberfest", + "program": "c:/Users/akans/Desktop/hacktoberfest/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/binary search/Squareroot.java b/binary search/Squareroot.java new file mode 100644 index 0000000..19dfd3f --- /dev/null +++ b/binary search/Squareroot.java @@ -0,0 +1,30 @@ +/* +Given an integer X, find its square root. If X is not a perfect square, +then return floor(√x). + +Time Complexity: O(√X) +Auxiliary Space: O(1)*/ +class Squareroot { + + static int floorSqrt(int x) + { + + if (x == 0 || x == 1) + return x; + + int i = 1, result = 1; + + while (result <= x) { + i++; + result = i * i; + } + return i - 1; + } + + + public static void main(String[] args) + { + int x = 11; + System.out.print(floorSqrt(x)); + } +} \ No newline at end of file diff --git a/binary search/fixed_point_equal_to_index.java b/binary search/fixed_point_equal_to_index.java new file mode 100644 index 0000000..8c7575e --- /dev/null +++ b/binary search/fixed_point_equal_to_index.java @@ -0,0 +1,28 @@ +/* + QUESTION:Given an array of n distinct integers sorted in ascending order, + write a function that returns a Fixed Point in the array, + if there is any Fixed Point present in array, else returns -1. + Fixed Point in an array is an index i such that arr[i] is equal to i. + Note that integers in array can be negative. + + Time Complexity: O(n) + Auxiliary Space: O(1) */ + class fixed_point_equal_to_index { + static int linearSearch(int arr[], int n) + { + int i; + for (i = 0; i < n; i++) { + if (arr[i] == i) + return i; + } + return -1; + } + + public static void main(String args[]) + { + int arr[] = { -10, -1, 0, 3, 10, 11, 30, 50, 100 }; + int n = arr.length; + System.out.println("Fixed Point is " + + linearSearch(arr, n)); + } +} \ No newline at end of file diff --git a/binary search/search_in_rotated_sorted_array.java b/binary search/search_in_rotated_sorted_array.java new file mode 100644 index 0000000..108a07d --- /dev/null +++ b/binary search/search_in_rotated_sorted_array.java @@ -0,0 +1,72 @@ +/*Given a sorted and rotated array arr[] of size N and a key, + the task is to find the key in the array. + +Time Complexity: O(log N) +Auxiliary Complexity: O(1)*/ + + +import java.io.*; +class search_in_rotated_sorted_array{ + + + static int pivotedBinarySearch(int arr[], int n, + int key) + { + int pivot = findPivot(arr, 0, n - 1); + + + if (pivot == -1) + return binarySearch(arr, 0, n - 1, key); + + + if (arr[pivot] == key) + return pivot; + if (arr[0] <= key) + return binarySearch(arr, 0, pivot - 1, key); + return binarySearch(arr, pivot + 1, n - 1, key); + } + + + static int findPivot(int arr[], int low, int high) + { + + if (high < low) + return -1; + if (high == low) + return low; + int mid = (low + high) / 2; + if (mid < high && arr[mid] > arr[mid + 1]) + return mid; + if (mid > low && arr[mid] < arr[mid - 1]) + return (mid - 1); + if (arr[low] >= arr[mid]) + return findPivot(arr, low, mid - 1); + return findPivot(arr, mid + 1, high); + } + + + static int binarySearch(int arr[], int low, int high, + int key) + { + if (high < low) + return -1; + + int mid = (low + high) / 2; + if (key == arr[mid]) + return mid; + if (key > arr[mid]) + return binarySearch(arr, (mid + 1), high, key); + return binarySearch(arr, low, (mid - 1), key); + } + + + public static void main(String args[]) + { + int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 }; + int n = arr1.length; + int key = 3; + System.out.println( + "Index of the element is : " + + pivotedBinarySearch(arr1, n, key)); + } +} \ No newline at end of file