-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c11565
commit 9166b95
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |