Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new questions under DataStructures. #3

Merged
merged 2 commits into from
Sep 11, 2021
Merged
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
45 changes: 45 additions & 0 deletions Algorithms/Neeraj/dataStructures/arraysAndString/CandiesDam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Nehal wants to make a special space for candies on his bookshelf.Currently, it has N books of different heights and unit width.
Help him select 2 books such that he can store maximum candies between them by removing all the other books from between the selected books.
The task is to find out the area between 2 books that can hold the maximum candies without changing the original position of selected books.
Example:
INPUT: N =3
height[]={1,3,4}
OUTPUT:1
*/

import java.io.*;
import java.util.*;

class Solution
{
static int maxCandy(int height[], int n)
{
int max = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int current = (Math.min(height[i],height[j])* (j - i - 1));
max = Math.max(max, current);
}
}
return max;
}
}

// Driver Code
class CandiesDam{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int height[] = new int[n];
for (int i = 0; i < n; ++i)
{
height[i] = sc.nextInt();
}

Solution ob = new Solution();
System.out.println(ob.maxCandy(height,n));
}
}

43 changes: 43 additions & 0 deletions Algorithms/Neeraj/dataStructures/arraysAndString/KPrefix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Given an array A of size N.Find the Kth prefix of the array.
Kth prefix is defined as the prefix sum of (K-1)th prefix of an array.
Example 1:
Input: N=4 K=2
Array= {1,1,1,1}
Output: 1 3 6 10

Example 2:
Input: N=1 K=100
Array= {1}
Output: 1
*/

import java.util.*;
import java.lang.*;
import java.io.*;

class KPrefix {
public static void main (String[] args) {
//code
Scanner sc= new Scanner(System.in);
long M= 1000000007; //since the values can become pretty large
int n,k;

n=sc.nextInt();
k=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int i=0;i<k;i++){
for(int j=n-1;j>=0;j--){
for(int z=j-1;z>=0;z--){
a[j]+=a[z];
}
}
}
for(int i=0;i<n;i++){
System.out.print((a[i]%M)+" "); //printing the modulo
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Given a keypad as in old phones, and an N digit number which is represented by array a[ ],
the task is to list all words which are possible by pressing these numbers.
Example:
INPUT: N = 3, a[] = {2, 3, 4}
OUTPUT: adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
*/

import java.util.*;
import java.io.*;
import java.lang.*;

class PossibleWordsPhone
{
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();
ArrayList <String> res = new Solution().possibleWords(arr, n);
for (String i : res) System.out.print (i + " ");
System.out.println();
}
}
class Solution
{

static ArrayList <String> possibleWords(int a[], int N)
{
String code[]={"\0","\0","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
String s="";
for(int i=0;i<a.length;i++)
{
s=s+(char)a[i];
}
return helper(s,code);
}
static ArrayList <String> helper(String s,String[] code)
{
if(s.length()==0)
{
ArrayList<String> res=new ArrayList<String>();
res.add("");
return res;
}
char ch=s.charAt(0);
ArrayList<String> res1=helper(s.substring(1),code);
ArrayList<String> res3=new ArrayList<String>();
String str1=code[ch];
for(int i=0;i<str1.length();i++)
{
char ch1=str1.charAt(i);
for(String res2:res1)
{
res3.add(ch1+res2);
}
}
return res3;
}
}
50 changes: 50 additions & 0 deletions Algorithms/Neeraj/dataStructures/arraysAndString/PrefixMatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Given an array of strings arr[] of size n and given s a string str and an integer k.
The task is to find the count of strings in arr[] whose prefix of length k matches with the k length prefix of str.
Example:
INPUT:n = 6
arr[] = {“abba”, “abbb”, “abbc”, “abbd”, “abaa”, “abca”}
str = “abbg”
k = 3
OUTPUT: 4
*/

//Initial Template for Java

import java.io.*;
import java.util.*;

class PrefixMatch
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] arr = new String[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.next();
}

int k = Integer.parseInt(sc.next());
String str = sc.next();
Solution obj = new Solution();
int ans = obj.klengthpref(arr,n,k,str);
System.out.println(ans);
}
}
class Solution
{
public int klengthpref(String[] arr, int n, int k, String str)
{
// code inthere
int y=0;
str=str.substring(0,k);
for (int i=0;i<n;i++){
if (arr[i].startsWith(str)){
y++;
}
}
return y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Given a square matrix[][] of size N x N.
The task is to rotate it by 90 degrees in an anti-clockwise direction without using any extra space.
Example:
INPUT: N=3
Matrix[][]=[[1,2,3],[4,5,6],[7,8,9]]
OUTPUT: 3 6 9
2 5 8
1 4 7
*/

//Initial Template for Java

import java.util.*;
import java.io.*;
import java.lang.*;

class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][n];

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
arr[i][j] = sc.nextInt();

Solution sol = new Solution();
sol.rotate(arr);
printMatrix (arr);

}

static void printMatrix(int arr[][])
{
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println("");
}
}
}

class Solution
{
static void rotate(int matrix[][])
{

for(int i = 0; i < matrix.length; i++){
for(int j = i; j < matrix.length; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i = 0; i < matrix.length; i++){
int k = matrix.length-1;
for(int j = 0; j < k; j++){
int temp = matrix[j][i];
matrix[j][i] = matrix[k][i];
matrix[k][i] = temp;
k--;
}
}
}
}

63 changes: 63 additions & 0 deletions Algorithms/Neeraj/dataStructures/arraysAndString/SaveGotham.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Gotham has been attacked by Joker . Bruce Wayne has deployed an automatic machine gun at each tower of Gotham.
All the towers in Gotham are in a straight line.
You are given no of towers 'n' followed by the height of 'n' towers.
For every tower(p), find the height of the closest tower (towards the right), greater than the height of the tower(p).
Now, the Print sum of all such heights (mod 1000000001).
Example:
INPUT: n = 9
112 133 161 311 122 512 1212 0 19212
OUTPUT: 41265
*/

//Initial Template for Java

//Initial Template for Java

/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;


class Array {

// Driver code
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] element = line.trim().split("\\s+");
int sizeOfArray = Integer.parseInt(element[0]);

int arr [] = new int[sizeOfArray];

line = br.readLine();
String[] elements = line.trim().split("\\s+");
for(int i = 0;i<sizeOfArray;i++){
arr[i] = Integer.parseInt(elements[i]);
}
Solution obj = new Solution();
int ans = obj.save_gotham(arr, sizeOfArray);
System.out.println(ans);
}
}

class Solution{
public static int save_gotham (int arr[], int n)
{
int ans = 0 ;
for(int i = 0 ; i < arr.length ; i ++)
{
for(int j = i+1 ; j < arr.length ; j ++)
{
if(arr[i]<arr[j])
{
ans += arr[j];
break;
}
}
}
return ans;
}
}
Loading