Skip to content

Commit

Permalink
Added codes for 12 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 12, 2024
1 parent 91a5ef5 commit 5042fb4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
45 changes: 45 additions & 0 deletions GeeksForGeeks/April/12-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.util.*;

class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
long Arr[] = new long[N];
String[] S = read.readLine().split(" ");
for (int i = 0; i < N; i++) Arr[i] = Long.parseLong(S[i]);
Solution ob = new Solution();
System.out.println(ob.pairAndSum(N, Arr));
}
}
}
// } Driver Code Ends


// User function Template for Java

class Solution
{
static long pairAndSum(int n, long arr[])
{
// code here
long ans = 0;
for(int i = 0; i < 32; i++)
{
long count = 0;

for(int j = 0; j < n; j++)
count += (arr[j] >> i) & 1;

ans += (1L << i) * (count * (count - 1) / 2);
}

return ans;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/12-4-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/April/12-4-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)
30 changes: 30 additions & 0 deletions LeetCode/April/12-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution
{
public int trap(int[] height)
{
if (height.length == 0)
return 0;

int ans = 0;
int l = 0;
int r = height.length - 1;
int maxL = height[l];
int maxR = height[r];

while (l < r)
{
if (maxL < maxR)
{
ans += maxL - height[l];
maxL = Math.max(maxL, height[++l]);
}
else
{
ans += maxR - height[r];
maxR = Math.max(maxR, height[--r]);
}
}

return ans;
}
}

0 comments on commit 5042fb4

Please sign in to comment.