First day we won the game we are in lead.

Now
Today problems learning Outcomes:

  1. now we know how to take input as given in question (from 1st question).
  2. We know how to store the taken input in arrays.
  3. We know how to use Integer.parseInt() function.
  4. We learned how to apply logic like adding elements and storing in new array.

we use Integer.parseInt(value); here value can be string or float or long any datatype.

we use Stirng[] arr= str.split(” “) function to split a string.

we use int[] arr=new int[]{31,2,31,23,12}; to create new arry of int.
or we use int[] arr=new int[size]; to declare array of size size;

Problem 1

 

Given an integer array A of size N, find sum of elements in it. Input: First line contains an integer denoting the test cases 'T'. T testcases follow. Each testcase contains two lines of input. First line contains N the size of the array A. The second line contains the elements of the array. Output: For each testcase, print the sum of all elements of the array in separate line.

Input Format

Input:
2
3
3 2 1
4
1 2 3 4

Constraints

Constraints:4
1 <= T <= 100
1<= N<= 100
1 <= Ai <= 100

Output Format

Output:
6
10

Solution

import java.util.Scanner;
public class validate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner iv=new Scanner(System.in);
int k=Integer.parseInt(iv.nextLine());
int result[]=new int[k];
for(int i=0;i<k;i++) {
   int temp=Integer.parseInt(iv.nextLine());
   String[] arr=iv.nextLine().split(” “);
   int total=0;
   for(int j=0;j<temp;j++) {
   total+=Integer.parseInt(arr[j]);
   }
   result[i]=total;
}
for(int i=0;i<k;i++) {
System.out.println(result[i]);
}
}

Problem 2: Triangles can be formed or not from a input array.

int[] canMakeTrinagle(int[] arr){
int[] finalop=new int[arr.length-2];
for(int i=0;i<arr.length-2;i++){

if(arr[i]+arr[i+1]>arr[i+2] && arr[i+2]+arr[i+1]>arr[i] && arr[i+2]+arr[i]>arr[i+1]){
finalop[i]=1;
}
else{
finalop[i]=0;
}
}
return finalop;
}

← Back

Thank you for your response. ✨

Leave a comment

Design a site like this with WordPress.com
Get started