-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ab7181
commit 529db8c
Showing
4 changed files
with
106 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,81 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
class IntArray | ||
{ | ||
public static int[] input(BufferedReader br, int n) throws IOException | ||
{ | ||
String[] s = br.readLine().trim().split(" "); | ||
int[] a = new int[n]; | ||
for(int i = 0; i < n; i++) | ||
a[i] = Integer.parseInt(s[i]); | ||
|
||
return a; | ||
} | ||
|
||
public static void print(int[] a) | ||
{ | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
|
||
public static void print(ArrayList<Integer> a) | ||
{ | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t; | ||
t = Integer.parseInt(br.readLine()); | ||
while(t-- > 0){ | ||
|
||
int n; | ||
n = Integer.parseInt(br.readLine()); | ||
|
||
|
||
int k; | ||
k = Integer.parseInt(br.readLine()); | ||
|
||
|
||
int[] arr = IntArray.input(br, n); | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.findClosest(n, k, arr); | ||
|
||
System.out.println(res); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
public static int findClosest(int n, int k, int[] arr) | ||
{ | ||
// code here | ||
int r = arr[0]; | ||
int t = Math.abs(arr[0] - k); | ||
for (int x = 1; x < arr.length; x++) | ||
{ | ||
int p = Math.abs(arr[x] - k); | ||
if ((p < t) || (p == t && arr[x] > r)) | ||
{ | ||
t = p; | ||
r = arr[x]; | ||
} | ||
} | ||
return r; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,21 @@ | ||
class Solution | ||
{ | ||
public long maximumValueSum(int[] nums, int k, int[][] edges) | ||
{ | ||
long totalSum = 0; | ||
int modifiedCount = 0; | ||
int minModificationDifference = Integer.MAX_VALUE; | ||
|
||
for (int number : nums) | ||
{ | ||
totalSum += Math.max(number, number ^ k); | ||
modifiedCount += ((number ^ k) > number) ? 1 : 0; | ||
minModificationDifference = Math.min(minModificationDifference, Math.abs(number - (number ^ k))); | ||
} | ||
|
||
if (modifiedCount % 2 == 0) | ||
return totalSum; | ||
|
||
return totalSum - minModificationDifference; | ||
} | ||
} |