-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from Anubhav-kr/main
search element in rotated array @hactoberfest2023 #175
- Loading branch information
Showing
6 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "windows-gcc-x64", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"compilerPath": "C:/MinGW/bin/gcc.exe", | ||
"cStandard": "${default}", | ||
"cppStandard": "${default}", | ||
"intelliSenseMode": "windows-gcc-x64", | ||
"compilerArgs": [ | ||
"" | ||
] | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/Anubhav/OneDrive/Desktop/New folder/hactoberfest2023", | ||
"program": "c:/Users/Anubhav/OneDrive/Desktop/New folder/hactoberfest2023/build/Debug/outDebug", | ||
"MIMode": "gdb", | ||
"miDebuggerPath": "gdb", | ||
"setupCommands": [ | ||
{ | ||
"description": "Enable pretty-printing for gdb", | ||
"text": "-enable-pretty-printing", | ||
"ignoreFailures": true | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
int secondSmallest(int arr[],int n) | ||
{ | ||
if(n<2) | ||
return -1; | ||
int small = INT_MAX; | ||
int second_small = INT_MAX; | ||
int i; | ||
for(i = 0; i < n; i++) | ||
{ | ||
if(arr[i] < small) | ||
{ | ||
second_small = small; | ||
small = arr[i]; | ||
} | ||
else if(arr[i] < second_small && arr[i] != small) | ||
{ | ||
second_small = arr[i]; | ||
} | ||
} | ||
return second_small; | ||
} | ||
int secondLargest(int arr[],int n) | ||
{ | ||
if(n<2) | ||
return -1; | ||
int large=INT_MIN,second_large=INT_MIN; | ||
int i; | ||
for (i = 0; i < n; i++) | ||
{ | ||
if (arr[i] > large) | ||
{ | ||
second_large = large; | ||
large = arr[i]; | ||
} | ||
|
||
else if (arr[i] > second_large && arr[i] != large) | ||
{ | ||
second_large = arr[i]; | ||
} | ||
} | ||
return second_large; | ||
} | ||
|
||
int main() { | ||
int arr[]={1,2,4,7,7,5}; | ||
int n=sizeof(arr)/sizeof(arr[0]); | ||
int sS=secondSmallest(arr,n); | ||
int sL=secondLargest(arr,n); | ||
cout<<"Second smallest is "<<sS<<endl; | ||
cout<<"Second largest is "<<sL<<endl; | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int search(vector<int>& arr, int n, int k) { | ||
int low = 0, high = n - 1; | ||
while (low <= high) { | ||
int mid = (low + high) / 2; | ||
|
||
//if mid points the target | ||
if (arr[mid] == k) return mid; | ||
|
||
//if left part is sorted: | ||
if (arr[low] <= arr[mid]) { | ||
if (arr[low] <= k && k <= arr[mid]) { | ||
//element exists: | ||
high = mid - 1; | ||
} | ||
else { | ||
//element does not exist: | ||
low = mid + 1; | ||
} | ||
} | ||
else { //if right part is sorted: | ||
if (arr[mid] <= k && k <= arr[high]) { | ||
//element exists: | ||
low = mid + 1; | ||
} | ||
else { | ||
//element does not exist: | ||
high = mid - 1; | ||
} | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> arr = {7, 8, 9, 1, 2, 3, 4, 5, 6}; | ||
int n = 9, k = 1; | ||
int ans = search(arr, n, k); | ||
if (ans == -1) | ||
cout << "Target is not present.\n"; | ||
else | ||
cout << "The index is: " << ans << "\n"; | ||
return 0; | ||
} | ||
|