How can add append two array by method? - netbeans-8

public static void main(String[] args) {
// TODO code application logic here
int a[] = { 1, 3, 4 };
int b[] = { 5, 6, 7, 8, 9 };
int[]c = new int[a.length+b.length];
int count = 0;
System.out.println(result(c[a.length+b.length]));
}
public static int result () {
// TODO code application logic here
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + " ");
int count = 0;
return result ;
}
}

Related

What is the Time Complexity and Space complexity of the following codes?

Here are the codes attached below. I have done these problems in one of the FAANG companies. I am open to have a discussion on time complexity and space complexity of these codes.
Code1:
public static void main(String[] args) {
int[] arr = {4,5,6,7};
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < arr.length; i++) {
queue.add(arr[i]);
}
System.out.println(queue);
while (queue.size() > 2) {
int first = queue.poll();
for (int i = 0; i < queue.size(); i++) {
int second = queue.poll();
queue.add(first % 10 + second % 10);
first = second;
}
}
int first = queue.poll() % 10;
int second = queue.poll() % 10;
int res = first + second;
System.out.println(res);
}
Time Complexity: ?
Space Complexity: ?
and Code 2:
public static void main(String[] args) {
String input = "aabbcaac";
HashMap<Character, Integer> map1 = new HashMap<>();
HashMap<Character, Integer> map2 = new HashMap<>();
char[] ip = input.toCharArray();
for (int i = 0; i < ip.length; i++) {
map2.put(ip[i], map2.getOrDefault(ip[i] , 0)+1);
}
System.out.println (map2);
int currVal = 0;
int result = 0;
int k = 1;
for (char str : ip) {
map2.put(str, map2.get(str) - 1);
if (map2.get(str) > 0) {
currVal += 1;
}
if(map1.get(str) == null) {
map1.put(str, 0);
}
if (map1.get(str) > 0) {
currVal -= 1;
}
map1.put(str, map1.get(str) + 1);
if (currVal > k) {
result += 1;
}
System.out.println(currVal);
}
System.out.println(result);
}
Time Complexity: ?
Space Complexity: ?

How to get sum of a item inside recyclerview?

Suppose two Items 8 and 4 are there in the list. So the Sum I should get is 12. But I am getting result as 84 and not 12. I am a beginner So I don't have an idea what wrong I am doing here.
private void getCreditEntries() {
final String shift = kvName.getText().toString();
final String leaveType = selectLeaveType.getSelectedItem().toString();
final String employeeCode = empCode.getText().toString();
final String calendarYear = selectYear.getText().toString();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("LeaveDetails").child(shift)
.child("Credit").child(employeeCode).child(calendarYear);
DatabaseReference dbRef = reference.child(leaveType);
dbRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<>();
if (!dataSnapshot.exists()) {
creditEntryLayout.setVisibility(View.GONE);
} else {
creditEntryLayout.setVisibility(View.VISIBLE);
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
LeaveCreditData data = snapshot.getValue(LeaveCreditData.class);
list.add(data);
}
rvCreditEntry.setHasFixedSize(true);
rvCreditEntry.setLayoutManager(new LinearLayoutManager(LeaveDetails.this));
rvCreditEntry.setItemAnimator(new DefaultItemAnimator());
leaveCreditAdapter = new LeaveCreditAdapter(list, LeaveDetails.this);
rvCreditEntry.setAdapter(leaveCreditAdapter);
int total = 0;
for(int i = 0; i < list.size(); i++){
total = Integer.parseInt(total + list.get(i).getTotalLeaveCredit());
creditSum.setText(String.valueOf( total));
//Suppose two Items 8 and 4 are there in the list
// So the Sum I should get is 12.
// But I am getting result as 84 and not 12
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(LeaveDetails.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
I simply edited the code as below and achieved what I wanted.
int total = 0;
for(int i = 0; i < list.size(); i++){
total += Integer.parseInt(list.get(i).getTotalLeaveCredit());
creditSum.setText(String.valueOf( total));
}

Find the largest String in the list

I am required to find the largest string in the ArrayList, and the print it. My code is not currently working though.
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
strings = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
String n = reader.readLine();
strings.add(n);
}
String largestString = strings.get(0);
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).length() > largestString.length()) {
largestString = strings.get(i);
System.out.println(largestString);
}
}
}
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String value = null;
int length = 0;
for (int i = 0; i < 5; i++) {
String n = reader.nextLine();
if (n != null && n.length() > length) {
length = n.length();
value = n;
}
}
System.out.println("Largest String : " + value + " of length : " + length);
}

For example, if coins = [1, 2, 5] and N = 11, return true if coins = [3, 77] and N = 100, return

wait online。~
Given a number of coins with different denominations, e.g. [1, 2, 5] and test if they could be used to make up a certain amount (N), assuming you can use unlimited number of coins in each denomination. For example, if coins = [1, 2, 5] and N = 11, return true if coins = [3, 77] and N = 100, return
The idea is use a recursive function (here it will calculate with one first coin vs array of other coins then recursively reduce size of coin array ).
But sr I'm not familiar with Objective-C so I write one using C#. Use should convert it to Objective-C.
bool CanDo(int n, int [] arr)
{
if (arr.Length == 1)
{
if (n % arr[0] == 0)
{
return true;
}
}
else
{
var ls = new List<int>(arr);
ls.RemoveAt(0);
int [] newarr = ls.ToArray(); //Create New array by deleting first element(current calculated element) of old array
for(int i = 0; i <= n/arr[0]; i++)
{
int next_n = n - i * arr[0];
if (next_n == 0)
{
return true;
}
else if (next_n < 0)
{
break;
}
else if(next_n > 0)
{
if( CanDo(next_n, newarr) )
{
return true;
}
}
}
}
return false;
}
This is full code in C# that can print to console first found solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static List<string> resultString = new List<string>();
static bool CanDo(int n, int [] arr)
{
if (arr.Length == 1)
{
if (n % arr[0] == 0)
{
resultString.Add(n/ arr[0] + "*" + arr[0]);
return true;
}
}
else
{
var ls = new List<int>(arr);
ls.RemoveAt(0);
int [] newarr = ls.ToArray(); //Create New array by deleting first element of old array
for(int i = 0; i <= n/arr[0]; i++)
{
if (resultString.Count > 0)
{
resultString.RemoveAt(resultString.Count - 1);
}
int next_n = n - i * arr[0];
if (next_n == 0)
{
resultString.Add(i + "*" + arr[0]);
return true;
}
else if (next_n < 0)
{
break;
}
else if(next_n > 0)
{
if (i != 0)
{
resultString.Add(i + "*" + arr[0] + " + ");
}
if( CanDo(next_n, newarr) )
{
return true;
}
}
}
}
return false;
}
static void Main(string[] args)
{
try
{
int[] arr = { 3, 5, 7 };
int N = 20;
resultString = new List<string>();
if (CanDo(N, arr))
{
resultString.ForEach(Console.WriteLine);
Console.Read();
}
else
{
Console.Write("Can't do");
Console.Read();
}
}
catch (Exception ex)
{
//handle exception
}
}
}
}

arduino enter input from computer to LCD

I want to take input as a char and concatenate them and write LCD. However I can't it. Also, ı want to not show a symbol which is about enter on LCD.
In this code, input does not written by serial monitor.
#include < LiquidCrystal.h >
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char karakter;
int ksayi;
String yazi = "";
String kaydirilacak = "";
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.home();
lcd.print("Hello World");
delay(1000);
lcd.clear();
lcd.home();
}
void loop() {
ksayi = Serial.available();
if (ksayi > 0) {
while (Serial.available() > 0) {
karakter = Serial.read();
if (karakter != '/n') {
yazi += karakter;
}
else {
kaydirilacak = yazi;
lcd.clear();
lcd.write(Serial.read(); yazi = "";
}
}
}
Kaydirmaca(kaydirilacak);
}
void Kaydirmaca(String s) {
int i;
for (i = 0; i < 16; i++) {
lcd.scrollDisplayLeft();
delay(275);
}
}
You are printing out Serial.read(), which returns nothing because serial.available() is zero.
Also, your code does not compile. You are missing an end parenthesis.
else {
kaydirilacak=yazi;
lcd.clear();
lcd.write(Serial.read();
yazi="";
}