Skip to content

Commit

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

int[] arr = {10};
int[] arr = {};
int target = 2;
int result = CodingTests.smallestOfOddNumArray(arr);
int result = CodingTests.sumOfEvenOdd(arr);
System.out.println(result);
}

Expand Down
20 changes: 20 additions & 0 deletions src/maharishi/CodingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,24 @@ public static int smallestOfOddNumArray(int[] nums){
return 1;
}

// Find the difference of sum of even and odd numbers in an array
public static int sumOfEvenOdd(int[] nums){
// Check if the array is null, return 0
// Get the sum of even and odd
// return the difference between them
int i = 0;
int sumOfEven = 0, sumOfOdd = 0;
if (nums == null || nums.length == 0)
return 0;

while (i < nums.length){
if (nums[i] % 2 == 0)
sumOfEven +=nums[i];
else
sumOfOdd +=nums[i];
i++;
}
// odd = X, even = Y so, X - Y
return sumOfOdd - sumOfEven;
}
}

0 comments on commit 0cd4486

Please sign in to comment.