Matrix Scalar Product

You are given a matrix A and and an integer B, you have to perform scalar multiplication of matrix A with an integer B.

Input Format –
First argument is vector of vector of integers A representing matrix.

Second argument is an integer B.

Output format –
You have to return a vector of vector of integers after doing required operations.

Input –
A = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
B = 2

Output –
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]

Explanation –
==> ( [[1, 2, 3],[4, 5, 6],[7, 8, 9]] ) * 2
==> [[2*1, 2*2, 2*3],[2*4, 2*5, 2*6],[2*7, 2*8, 2*9]]
==> [[2, 4, 6], [8, 10, 12], [14, 16, 18]]

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

/* Name of the class has to be “Main” only if the class is public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
ArrayList<ArrayList> arrList = new ArrayList<ArrayList>();
ArrayList<ArrayList> finalarr = new ArrayList<ArrayList>();

arrList.add(new ArrayList());
arrList.get(0).add(1);
arrList.get(0).add(2);
arrList.get(0).add(3);
arrList.add(new ArrayList());
arrList.get(1).add(4);
arrList.get(1).add(5);
arrList.get(1).add(6);
arrList.add(new ArrayList());
arrList.get(2).add(7);
arrList.get(2).add(8);
arrList.get(2).add(9);

finalarr = solve(arrList,2);
}

public static ArrayList<ArrayList> solve(ArrayList<ArrayList> A, int B) {
ArrayList<ArrayList> finalarr = new ArrayList<ArrayList>();

for (int i=0; i < A.size(); i++)
{
ArrayList arr = new ArrayList();
for (int j=0; j < A.get(0).size(); j++)
{
arr.add(A.get(i).get(j) * B);
}

finalarr.add(arr);
}

return finalarr;
}
}

Special Subsequences “AG”

You have given a string A having Uppercase English letters.

You have to find that how many times subsequence “AG” is there in the given string.

Input Format – First and only argument is a string A.
Output Format – Return an integer denoting the answer.

Input – “ABCGAG”
Output – 3
Example: Subsequence “AG” is 3 times in given string

public class Main
{
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String strLetter = sc.nextInt();

int res = solve(strLetter);

System.out.println(res);
}

static int solve(String A)
{
int ans = 0;
int count_a = 0;

for (int i = 0; i < A.length(); i++)
{
if (A.charAt(i) == ‘A’)
{
count_a++;
}

else if (A.charAt(i) == ‘G’)
{
ans = ans + count_a;
}
}

return ans;
}
}

Time to equality

Given an integer array A of size N. In one second you can increase the value of one element by 1.

Find the minimum time in seconds to make all elements of the array equal.

Input format – First argument is an integer array A.
Output format – Return an integer denoting the minimum time to make all elements equal.

example Input – A = [2, 4, 1, 3, 2]
Example output – 8

Example Explanation –
We can change the array A = [4, 4, 4, 4, 4]. The time required will be 8 seconds.

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

/* Name of the class has to be “Main” only if the class is public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
ArrayList arrList = new ArrayList();

arrList.add(2);
arrList.add(4);
arrList.add(1);
arrList.add(3);
arrList.add(2);

int res = solve(arrList);
}

static int solve(ArrayList A)
{
int cnt = 0;
int maxVal = Integer.MIN_VALUE;

//get Maximum value from the array element
for (int i = 0; i maxVal)
{
maxVal = A.get(i);
}
}

// increment the count if the array element is less than 4
for (int i = 0; i< A.size(); i++)
{
while (A.get(i) < maxVal)
{
A.set(i,A.get(i)+1);
cnt++;
}
}

return cnt;
}
}

Inverted Triangular pattern

Given an integer N, print the corresponding Inverted Half Pyramid pattern for N.

For example if N = 4 then pattern will be like:
****
***
**
*

Input format – First and only line of input contains a single integer N.
Output Format – Output the Inverted Half Pyramid pattern corresponding to the given N.

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

public class Main {
public static void main(String[] args) {
// DO NOT USE ARGUMENTS FOR INPUTS
// E.g. ‘Scanner’ for input & ‘System.out’ for output

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

pattern(N);
}

static void pattern(int n)
{
int stars = n;

for (int i = 0; i <=n ; i++)
{
for (int j=1; j<=stars; j++)
{
System.out.print(“*”);
}

System.out.println();
stars–;
}

}
}

Stair Pattern using star symbol

Given an integer N, print the corresponding stair pattern for N.

For example if N = 4 then stair pattern will be like:

*
**
***
****

Input –
3

output –
*
**
***

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

public class Main {
public static void main(String[] args) {
// E.g. ‘Scanner’ for input & ‘System.out’ for output
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

pattern(N);

}

static void pattern(int n)
{
for (int i = 1; i <=n; i++)
{
for (int j=1; j <=i; j++)
{
System.out.print(“*”);
}

System.out.println();
}
}
}