forked from GDG-GTBIT/HacktoberFest-20-GTBIT-Competitive
-
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
Showing
4 changed files
with
201 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,95 @@ | ||
package assignment2; | ||
|
||
import java.util.Scanner; | ||
|
||
public class ArrayManipulation { | ||
public static void main(String[] args) { | ||
Scanner scn=new Scanner(System.in); | ||
int n=scn.nextInt(); | ||
int q=scn.nextInt(); | ||
int[][] queries=new int[q][3]; | ||
for(int i=0;i<q;i++) { | ||
for(int j=0;j<3;j++) { | ||
queries[i][j]=scn.nextInt(); | ||
} | ||
} | ||
System.out.print(arrayManipulation(n, queries)); | ||
} | ||
public static long arrayManipulation(int n, int[][] queries) { | ||
long[] ar=new long[n]; | ||
for(int i=0;i<queries.length;i++){ | ||
int a=queries[i][0],b=queries[i][1],k=queries[i][2]; | ||
for(int j=a;j<b;j++){ | ||
ar[j-1]+=k; | ||
} | ||
} | ||
long max=Integer.MIN_VALUE; | ||
for(int k=0;k<ar.length;k++){ | ||
if(ar[k]>max){ | ||
max=ar[k]; | ||
} | ||
} | ||
return max; | ||
} | ||
//THE EFFECTIVE SOLUTION TO THE PROBLEM | ||
//static long arrayManipulation(int n, int[][] queries) { | ||
// long max = 0L; | ||
// long[] arr = new long[n]; | ||
// for (int i = 0; i < n; i++) { | ||
// arr[i] = 0L; | ||
// } | ||
// for (int i = 0; i < queries.length; i++) { | ||
// int[] q = queries[i]; | ||
// int start = q[0] - 1; | ||
// int end = q[1] - 1; | ||
// int val = q[2]; | ||
// long tempMax = updateVal(start, end, val, arr); | ||
// if (tempMax > max) { | ||
// max = tempMax; | ||
// } | ||
// } | ||
// return max; | ||
// } | ||
// | ||
// | ||
// | ||
//static long updateVal(int start, int end, int val, long[] arr) { | ||
// long max = 0L; | ||
// for (int i = start; i <= end; i++) { | ||
// arr[i] = arr[i] + val; | ||
// if (arr[i] > max) { | ||
// max = arr[i]; | ||
// } | ||
// } | ||
|
||
//O(m+n) | ||
//static long arrayManipulation(int n, int[][] queries) { | ||
// long max = 0L; | ||
// long[] ar = new long[n+2]; | ||
// for (int i = 0; i < n; i++) { | ||
// ar[i] = 0L; | ||
// } | ||
// for (int i = 0; i < queries.length; i++) { | ||
// int[] q = queries[i]; | ||
// int start = q[0]; | ||
// int end = q[1]; | ||
// int val = q[2]; | ||
// ar[start]+=val; | ||
// ar[end+1]-=val; | ||
// } | ||
// max=updateVal(ar); | ||
// return max; | ||
// } | ||
// | ||
// | ||
// | ||
// static long updateVal(long[] arr) { | ||
// long max = 0L; | ||
// long sum=0; | ||
// for (int i = 0; i < arr.length; i++) { | ||
// sum+=arr[i]; | ||
// max=Math.max(max,sum); | ||
// } | ||
// return max; | ||
// } | ||
} |
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,32 @@ | ||
package assignment2; | ||
|
||
import java.util.Scanner; | ||
import java.util.Stack; | ||
|
||
public class NextGreaterElement { | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner scn = new Scanner(System.in); | ||
|
||
int t = scn.nextInt(); | ||
|
||
while (t > 0) { | ||
int n = scn.nextInt(); | ||
int[] arr = new int[n]; | ||
|
||
for (int i = 0; i < arr.length; i++) | ||
arr[i] = scn.nextInt(); | ||
|
||
nextLarger(arr); | ||
|
||
t--; | ||
} | ||
|
||
} | ||
|
||
// Function to print Next Greater Element for each element of the array | ||
public static void nextLarger(int[] arr,int idx,String ans) { | ||
|
||
} | ||
} |
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,42 @@ | ||
package assignment2; | ||
|
||
import java.util.Scanner; | ||
|
||
public class RainWaterTrapping { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
Scanner scn = new Scanner(System.in); | ||
int t = scn.nextInt(); | ||
while (t > 0) { | ||
int n = scn.nextInt(); | ||
int[] one = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
one[i] = scn.nextInt(); | ||
} | ||
rainWater(one); | ||
t--; | ||
} | ||
} | ||
|
||
public static void rainWater(int[] arr) { | ||
|
||
int MaxWaterTrapped = 0; | ||
for (int b = 0; b < arr.length; b++) { | ||
int left = Integer.MIN_VALUE; | ||
for (int l = b; l >= 0; l--) { | ||
if (arr[l] > left) | ||
left = arr[l]; | ||
} | ||
int right = Integer.MIN_VALUE; | ||
for (int r = b ; r < arr.length; r++) { | ||
if (arr[r] > right) | ||
right = arr[r]; | ||
} | ||
int WaterperBuilding = Math.min(left, right) - arr[b]; | ||
MaxWaterTrapped += WaterperBuilding; | ||
} | ||
System.out.println(MaxWaterTrapped); | ||
} | ||
|
||
} |
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,32 @@ | ||
package assignment2; | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class bargraph{ | ||
|
||
public static void main(String[] args) throws Exception { | ||
Scanner scn=new Scanner(System.in); | ||
int n=scn.nextInt(); | ||
int[] arr=new int[n]; | ||
int max=0; | ||
for(int i=0;i<n;i++){ | ||
arr[i]=scn.nextInt(); | ||
if(max<arr[i]){ | ||
max=arr[i]; | ||
} | ||
} | ||
int row=1; | ||
int rows=max; | ||
for (int r = 1; r <= max; r++) { | ||
for (int i = 0; i < arr.length; i++) { | ||
if (r <= max - arr[i]) | ||
System.out.print(" "); | ||
else | ||
System.out.print("*"); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
} | ||
} | ||
|