Skip to content

Commit

Permalink
Solve find in mountain array
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Jun 14, 2023
1 parent 34a595d commit 2d8f912
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Binary file modified out/production/java-interview-questions/Main.class
Binary file not shown.
5 changes: 3 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public class Main {
public static void main(String[] args) {

int[] arr = {1, 2, 3, 1};
int result = Searching.peakMountain(arr);
System.out.println(result);
int[]aliceSizes = {2}, bobSizes = {1,3};
int[] result = Searching.fairCandySwap(aliceSizes, bobSizes);
System.out.println(Arrays.toString(result));
}


Expand Down
5 changes: 3 additions & 2 deletions src/intermediate/Searching.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public static int peakMountain(int[] nums) {
}

// https://leetcode.com/problems/fair-candy-swap/description/
public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes){
public static int[] fairCandySwap(int[] aliceSizes, int[] bobSizes){
int aliceTotal = 0, bobTotal = 0;

for (int aliceCandy: aliceSizes)
Expand All @@ -563,10 +563,11 @@ public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes){
bobCandySizes.add(bobCandy);
}

// Search for the pair of candies
int diffOfCandies=(bobTotal - aliceTotal) / 2;
for (int alice: aliceSizes){
if (bobCandySizes.contains(alice+diffOfCandies)){
return new int[]{alice, alice + diffOfCandies};
return new int[]{alice, alice+diffOfCandies};
}
}
return null;
Expand Down

0 comments on commit 2d8f912

Please sign in to comment.