Skip to content

Commit

Permalink
Added codes for 19 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 19, 2024
1 parent 2ab7181 commit 529db8c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
81 changes: 81 additions & 0 deletions GeeksForGeeks/May/19-5-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/19-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/May/19-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
21 changes: 21 additions & 0 deletions LeetCode/May/19-5-24/Solution.java
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;
}
}

0 comments on commit 529db8c

Please sign in to comment.