Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bin/cc/_01_ArrangingVowel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package day01;
import java.util.Scanner;
import java.util.Stack;

public class _01_ArrangingVowel{

public static int maxRectArea(int[] ht) {
Stack<Integer> stack = new Stack<>();
int[] lb = new int[ht.length];
stack.push(0);
for(int i=0;i<ht.length;i++){
while(!stack.isEmpty() && ht[stack.peek()] > ht[i]){

}
}
return 0;
}



public static void main(String[] args) {
Scanner scn = new Scanner(System.in);

int h=scn.nextInt();
int height[]=new int[h];
for(int i=0;i<h;i++){
height[i]=scn.nextInt();
}
System.out.println(maxRectArea(height));
}
}
50 changes: 50 additions & 0 deletions bin/cc/_02_MaximumAreaOfHistogram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package day01;

import java.util.Scanner;
import java.util.Stack;

public class _02_MaximumAreaOfHistogram {

public static int maxRectArea(int[] ht) {
int[] lb = new int[ht.length];
int[] rb = new int[ht.length];
int area = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<>();
for(int i=0;i<ht.length;i++){
while(stack.size() > 0 && ht[stack.peek()] > ht[i]){
rb[stack.peek()] = i;
stack.pop();
}
if(stack.size() == 0){
lb[i] = -1;
}
else{
lb[i] = stack.peek();
}
stack.push(i);
}
while(stack.size()>0){
rb[stack.pop()] = ht.length;
}
for(int i =0;i<ht.length;i++){
int temp = (rb[i]-lb[i]-1)*ht[i];
if(temp>area)
area = temp;
}
if(area==Integer.MIN_VALUE)
return 0;
return area;
}

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);

int h=scn.nextInt();
int height[]=new int[h];
for(int i=0;i<h;i++){
height[i]=scn.nextInt();
}
System.out.println(maxRectArea(height));
}

}
77 changes: 77 additions & 0 deletions bin/cc/_03_NextWarmerDay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package day01;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class _03_NextWarmerDay {
static class stack
{
int top;
int items[] = new int[100];

// Stack functions to be used by printNGE
void push(int x)
{
if (top == 99)
{
System.out.println("Stack full");
}
else
{
items[++top] = x;
}
}

int pop()
{
if (top == -1)
{
System.out.println("Underflow error");
return -1;
}
else
{
int element = items[top];
top--;
return element;
}
}

boolean isEmpty()
{
return (top == -1) ? true : false;
}
}

/* prints element and NGE pair for
all elements of arr[] of size n */
static void printNGE(int arr[], int n)
{
Stack<Integer> s = new Stack<>();
int[] res = new int[arr.length];
s.push(0);
for(int i =1;i<arr.length;i++){
while(!s.isEmpty() && arr[s.peek()] < arr[i]){
res[s.peek()] = i-s.peek();
System.out.print(arr[i]+" ");
s.pop();
}
s.push(i);
}
while(!s.isEmpty()){
res[s.pop()] = 0;
System.out.print(-1+" ");
}
System.out.println();
System.out.println(Arrays.toString(res));
}

public static void main(String[] args)
{ Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)arr[i]=sc.nextInt();
printNGE(arr, n);
}
}
47 changes: 47 additions & 0 deletions bin/cc/_04_CompareTwoStringsAfterDeletion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package day01;

import java.util.Scanner;
import java.util.Stack;

public class _04_CompareTwoStringsAfterDeletion {
public static boolean compareString(String S, String T) {
char[] s = S.toCharArray();
char[] t = T.toCharArray();
Stack<Character> stack1 = new Stack<>();
Stack<Character> stack2 = new Stack<>();
for(int i =0;i<s.length;i++){
if(s[i] == '#' && stack1.size() > 0){
stack1.pop();
}
else{
stack1.push(s[i]);
}
}
for(int i =0;i<t.length;i++){
if(t[i] == '#' && stack2.size() > 0){
stack2.pop();
}
else{
stack2.push(t[i]);
}
}
if(stack1.size() != stack2.size())
return false;
else{
while(stack1.size() >0 && stack2.size() > 0){
if(stack1.pop() != stack2.pop())
return false;
}
}
return true;
}

//Driver program
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String s1 = scn.next();
String s2 = scn.next();

System.out.println(compareString(s1, s2));
}
}
56 changes: 56 additions & 0 deletions bin/cc/_05_PrintAllBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package day01;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class _05_PrintAllBinary {

public static void getBinary(int n){
if(n==0){
System.out.print(0);
return;
}
boolean isGreater = false;
String d = decimalToBinary(n);
Queue<String> que = new LinkedList<>();
que.add(""+1);
while(!que.isEmpty()){
String rec = que.remove();
if(!isGreater && d.equals(rec+0)){
isGreater = true;
que.add(rec + 0);
}
if(!isGreater){
que.add(rec+0);
}
if(!isGreater && d.equals(rec+1)){
isGreater = true;
que.add(rec + 1);
}
if(!isGreater){
que.add(rec+1);
}
System.out.print(rec+" ");
}
}

private static String decimalToBinary(int n) {
String i = "";
while(n>0){
int rem = n%2;
i = rem+i;
n = n/2;
}
return i;
}

public static void main (String[] args) {

Scanner scn=new Scanner(System.in);
int n = scn.nextInt();
getBinary(n);


}
}
30 changes: 30 additions & 0 deletions bin/cc/_06_TinyBigTiny.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day01;

import java.util.Scanner;

public class _06_TinyBigTiny {

public static boolean find132pattern(int[] num) {
for(int i =0;i<num.length-1;i++){
if(i%2 == 0){
if(num[i+1]>num[i])
return false;
}
else{
if(num[i+1]<num[i])
return false;
}
}
return true;
}

//Dont make changes here
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] A=new int[n];
for(int i=0;i<n;i++)A[i]=sc.nextInt();
System.out.println(find132pattern(A));
}
}
80 changes: 80 additions & 0 deletions bin/cc/_07_OcuurenceOfaNumberLL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package day01;

import java.util.Scanner;

public class _07_OcuurenceOfaNumberLL {
public static int frequency(Node node, int search) {
int counter =0;
while(node!=null){
if(node.data == search)
counter++;
node = node.next;
}
return counter;
}

// -----------------------------------------------------

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int a1 = sc.nextInt();
head1 = insert(head1, a1);

for (int i = 1; i < n1; i++) {
int a = sc.nextInt();
head1 = insert(head1, a);
}

int k = sc.nextInt();
System.out.println(frequency(head1, k));

}

// Class declaration for a Node of the Linked List
static class Node {
int data;
Node next;

public Node(int data) {
this.data = data;
this.next = null;
}

}

static Node head1;

static Node head2;

static Node head3;

/*
* Input Parameters: head: head of the linked list in which a new node is to
* be inserted. data: the data value of the node which is to be inserted.
*
* Return Value: head of the linked list in which the node is inserted
*/
public static Node insert(Node head, int data) {

if (head == null) {
return new Node(data);
}

head.next = insert(head.next, data);
return head;
}

/*
* Input Parameters: head: head of the linked list in which is to be
* displayed.
*
* Return Value: null
*/
public static void display(Node head) {
for (Node node = head; node != null; node = node.next) {
System.out.print(node.data + " ");
}
System.out.println();
}
}
Loading