-
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
a10e67e
commit 3c0a79f
Showing
4 changed files
with
160 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,110 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
|
||
class GFG | ||
{ | ||
public static void main(String[] args) throws IOException | ||
{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine().trim()); | ||
while(t-->0) | ||
{ | ||
int n = Integer.parseInt(br.readLine().trim()); | ||
int a[] = new int[(int)n]; | ||
String inputLine[] = br.readLine().trim().split(" "); | ||
for (int i = 0; i < n; i++) { | ||
a[i] = Integer.parseInt(inputLine[i]); | ||
} | ||
|
||
Solution obj = new Solution(); | ||
System.out.println(obj.countPairs(a, n)); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
|
||
class Solution | ||
{ | ||
static int mergeAndCount(int arr[], int low, int mid, int high) | ||
{ | ||
int swapCount = 0; | ||
int n1 = mid - low + 1; | ||
int n2 = high - mid; | ||
int temp1[] = new int[n1], temp2[] = new int[n2]; | ||
|
||
for(int m=0;m<n1;++m) | ||
{ | ||
temp1[m] = arr[low + m]; | ||
} | ||
for(int m=0;m<n2;++m) | ||
{ | ||
temp2[m] = arr[mid + 1 + m]; | ||
} | ||
|
||
int i = 0, j = 0, k = low; | ||
while(i < n1 && j < n2) | ||
{ | ||
if(temp1[i] <= temp2[j]) | ||
{ | ||
arr[k] = temp1[i]; | ||
++i; | ||
} | ||
else | ||
{ | ||
swapCount += (n1 - i); | ||
arr[k] = temp2[j]; | ||
++j; | ||
} | ||
++k; | ||
} | ||
while(i < n1) | ||
{ | ||
arr[k] = temp1[i]; | ||
++i; | ||
++k; | ||
} | ||
while(j < n2) | ||
{ | ||
arr[k] = temp2[j]; | ||
++j; | ||
++k; | ||
} | ||
|
||
return swapCount; | ||
} | ||
|
||
static int mergeSortAndCount(int arr[], int low, int high) | ||
{ | ||
int pairCount = 0; | ||
if(low < high) | ||
{ | ||
int mid = low + (high - low) / 2; | ||
pairCount += mergeSortAndCount(arr, low, mid); | ||
pairCount += mergeSortAndCount(arr, mid + 1, high); | ||
pairCount += mergeAndCount(arr, low, mid, high); | ||
} | ||
|
||
return pairCount; | ||
} | ||
|
||
static int countPairs(int arr[], int n) | ||
{ | ||
// Your code goes here | ||
for(int i=0;i<n;++i) | ||
{ | ||
arr[i] = i * arr[i]; | ||
} | ||
|
||
return mergeSortAndCount(arr, 0, n - 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(n*logn) | ||
Space complexity - O(n*logn) |
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*h) | ||
Space complexity - O(h) |
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,46 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public String smallestFromLeaf(TreeNode root) | ||
{ | ||
dfs(root, new StringBuilder()); | ||
return ans; | ||
} | ||
|
||
private String ans = null; | ||
|
||
private void dfs(TreeNode root, StringBuilder sb) | ||
{ | ||
if (root == null) | ||
return; | ||
|
||
sb.append((char) (root.val + 'a')); | ||
|
||
if (root.left == null && root.right == null) | ||
{ | ||
String path = sb.reverse().toString(); | ||
sb.reverse(); | ||
|
||
if (ans == null || ans.compareTo(path) > 0) | ||
ans = path; | ||
} | ||
|
||
dfs(root.left, sb); | ||
dfs(root.right, sb); | ||
sb.deleteCharAt(sb.length() - 1); | ||
} | ||
} |