Skip to content

Commit

Permalink
Added codes for 6 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 6, 2024
1 parent 9c11565 commit 9166b95
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
47 changes: 47 additions & 0 deletions GeeksForGeeks/June/06-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//{ Driver Code Starts
import java.util.*;

class Maxsum_Among_All_Rotations_Array {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t > 0) {
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();

System.out.println(new Solution().max_sum(arr, n));

t--;
}
}
}

// } Driver Code Ends


class Solution
{
long max_sum(int a[], int n)
{
// Your code here
long totalSum = 0;
long currentConfigurationSum = 0;

for (int i = 0; i < n; i++)
{
totalSum += a[i];
currentConfigurationSum += i * (long) a[i];
}

long maxSum = currentConfigurationSum;

for (int i = 1; i < n; i++)
{
currentConfigurationSum = currentConfigurationSum - totalSum + a[i - 1] * (long) n;
maxSum = Math.max(maxSum, currentConfigurationSum);
}

return maxSum;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/06-6-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/June/06-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(mlogm + mk), where m = # of different nums
Space complexity - O(m)
21 changes: 21 additions & 0 deletions LeetCode/June/06-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution
{
public boolean isNStraightHand(int[] hand, int groupSize)
{
TreeMap<Integer, Integer> count = new TreeMap<>();

for (int card : hand)
count.merge(card, 1, Integer::sum);

for (int start : count.keySet())
{
int value = count.getOrDefault(start, 0);
if (value > 0)
for (int i = start; i < start + groupSize; ++i)
if (count.merge(i, -value, Integer::sum) < 0)
return false;
}

return true;
}
}

0 comments on commit 9166b95

Please sign in to comment.