From 9166b950ecb2a0fa2e5a65eee2fd945c241e4a0f Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Thu, 6 Jun 2024 18:17:03 +0530 Subject: [PATCH] Added codes for 6 June --- GeeksForGeeks/June/06-6-24/GFG.java | 47 ++++++++++++++++++++++++++++ GeeksForGeeks/June/06-6-24/README.md | 2 ++ LeetCode/June/06-6-24/README.md | 2 ++ LeetCode/June/06-6-24/Solution.java | 21 +++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 GeeksForGeeks/June/06-6-24/GFG.java create mode 100644 GeeksForGeeks/June/06-6-24/README.md create mode 100644 LeetCode/June/06-6-24/README.md create mode 100644 LeetCode/June/06-6-24/Solution.java 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; + } +}