Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Added solution for finding the single non-duplicate element in sorted…
Browse files Browse the repository at this point in the history
… array using binary search
  • Loading branch information
SaurabhKumar171 committed Oct 6, 2024
1 parent 9e266ea commit 86d3514
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "macos-clang-arm64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [
""
]
}
],
"version": 4
}
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "/Users/saurabh/Desktop/home/Hacktoberfest-Contributions",
"program": "/Users/saurabh/Desktop/home/Hacktoberfest-Contributions/build/Debug/outDebug"
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "clang",
"C_Cpp_Runner.cppCompilerPath": "clang++",
"C_Cpp_Runner.debuggerPath": "lldb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"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
}
34 changes: 34 additions & 0 deletions SingleNonDuplicateElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class SingleNonDuplicate {
public static int findSingleNonDuplicate(int[] nums) {
// Initialize two pointers for binary search
int low = 0;
int high = nums.length - 1;

// Binary search loop
while (low < high) {
int mid = low + (high - low) / 2;

// Check if mid is even
if (mid % 2 == 1) {
mid--; // Ensure mid is even so that pairs are aligned correctly
}

// Compare mid with the next element
if (nums[mid] == nums[mid + 1]) {
// If they are equal, the single element must be in the second half
low = mid + 2;
} else {
// Otherwise, the single element is in the first half
high = mid;
}
}

// After the loop, low will point to the single element
return nums[low];
}

public static void main(String[] args) {
int[] nums = {1, 1, 2, 3, 3, 4, 4, 5, 5};
System.out.println("Single non-duplicate element: " + findSingleNonDuplicate(nums));
}
}

0 comments on commit 86d3514

Please sign in to comment.