Skip to content

Commit

Permalink
Problems Added
Browse files Browse the repository at this point in the history
  • Loading branch information
aastha-b committed Oct 2, 2020
1 parent fb3e42d commit a9eeb6d
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Problem Solving/ArrayManipulation.java
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;
// }
}
32 changes: 32 additions & 0 deletions Problem Solving/NextGreaterElement.java
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) {

}
}
42 changes: 42 additions & 0 deletions Problem Solving/RainWaterTrapping.java
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);
}

}
32 changes: 32 additions & 0 deletions Problem Solving/bargraph.java
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();
}

}
}

0 comments on commit a9eeb6d

Please sign in to comment.