diff --git a/GeeksForGeeks/June/06-6-24/GFG.java b/GeeksForGeeks/June/06-6-24/GFG.java new file mode 100644 index 0000000..9671085 --- /dev/null +++ b/GeeksForGeeks/June/06-6-24/GFG.java @@ -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; + } +} diff --git a/GeeksForGeeks/June/06-6-24/README.md b/GeeksForGeeks/June/06-6-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/GeeksForGeeks/June/06-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/June/06-6-24/README.md b/LeetCode/June/06-6-24/README.md new file mode 100644 index 0000000..64875d8 --- /dev/null +++ b/LeetCode/June/06-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(mlogm + mk), where m = # of different nums +Space complexity - O(m) diff --git a/LeetCode/June/06-6-24/Solution.java b/LeetCode/June/06-6-24/Solution.java new file mode 100644 index 0000000..36dbe05 --- /dev/null +++ b/LeetCode/June/06-6-24/Solution.java @@ -0,0 +1,21 @@ +class Solution +{ + public boolean isNStraightHand(int[] hand, int groupSize) + { + TreeMap 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; + } +}