Array Java-01
Array Java-01
Structure
Discuss.......
IN JAVA
Java 1D Array
● Array is a simple data structure used to store a collection of data in a contiguous block of
memory. Each element in the collection is accessed using an index, and the elements are
easy to find because they're stored sequentially in memory.
Java 1D Array
● You are given a 2D array. An hourglass in an array is a portion shaped like this:
abc
d
efg
● For example, if we create an hourglass using the number 1 within an array full of zeros, it
may look like this:
Actually, there are many hourglasses in the array
111000
above. The three leftmost hourglasses are the
010000
following:
111000
000000 111 110 100
000000 1 0 0
000000 111 110 100
Java 2D Array
111000
010000 111 110 100
111000 1 0 0
000000 111 110 100
000000
000000
Java 2D Array
● In this problem you have to print the largest sum among all the hourglasses in the array.
● Input Format: There will be exactly 6 lines, each containing 6 integers seperated by
spaces. Each integer will be between -9 and 9 inclusive.
● Given an array of integers, find and print its number of negative subarrays on a new line.
Java Subarray
●
Java Subarray
● Sample Input:
1 -2 4 -5 1
● Sample Output:
9
Java Subarray
Java Subarray
for(i=0;i<n;i++) {
import java.util.*;
if(a[i]<0){
public class Main{
count++;
public static void main(String[] args) {
} }
Scanner scan = new Scanner(System.in);
for(i=0;i<n;i++) {
int n = scan.nextInt();
for(j=0;j<i;j++) {
int a[] = new int[n];
sum = a[i] + sum; b[j] = sum;
int b[] = new int[n];
if(b[j]<0){
int count=0, i,j,sum = 0;
count++;
a[i] = scan.nextInt();
} }
} System.out.println(count); }}
for(i=0;i<n;i++) {
Java 1D Array
A left rotation operation on an array of size shifts each of the array's elements unit to the
left. For
example, if left rotations are performed on array , then the array would become.
Given an array of integers and a number, , perform left rotations on the array.
Input Format
● The first line contains two space-separated integers denoting the respective values of
(the number ofintegers) and (the number of left rotations you must perform).
● The second line contains space-separated integers describing the respective elements
of the array's initialState.
Java 1D Array
Sample Input
54
12345
Sample Output
51234
Java 1D Array
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int d = scan.nextInt();
A)malloc
b)alloc
c)new
d) new malloc
Answer :
QUESTION
Answer :
QUESTION
Answer :
QUESTION
Answer :
QUESTION
Answer :
QUESTION
Answer :
QUESTION
•Linked List is a very commonly used linear data structure which consists of group of nodes in a
sequence.
•Each node holds its own data and the address of the next node hence forming a chain like
structure.
LINKED-LIST
Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which
points to the next node in the sequence of nodes.
The operations we can perform on singly linked lists are insertion, deletion and traversal
DOUBLY LINKED-LIST
● Doubly Linked List is a variation of the linked list. The linked list is a linear data
structure which can be described as the collection of nodes. Nodes are connected
through pointers
● Each node contains two fields: data and pointer to the next field
● The first node of the linked list is called the head, and the last node of the list is
Each node in a double linked list has a pointer the previous and next node, it is easy to implement skip
forward/backward functionality
The pointer to the next node also makes it quite easy to start the next track when a track is over.
When you add a new track to a playlist, you tack it on to the end
DOUBLY LINKED-LIST
A. Each node has only one pointer to traverse the list back and forth
B. The list has breakpoints for faster traversal
C. An auxiliary singly linked list acts as a helper list to traverse through the doubly
linked list
D. A doubly linked list that uses bitwise AND operator for storing addresses
Ans:
DOUBLY LINKEDLIST
Which of the following operations is performed more efficiently by doubly linked list
than by singly linked list?
Ans:
DOUBLY LINKED-LIST
In linked list each node contain minimum of two fields. One field is data field to store
the data second field is?
A. Pointer to character
B. Pointer to integer
C. Pointer to node
D. Node
Ans:
DOUBLY LINKED-LIST
Ans:
DOUBLY LINKED-LIST
How do you calculate the pointer difference in a memory efficient double linked list?
Answer:
Q :01 Split a list into two halves in Java
list.add("sam");
list.add("Practice");
list.add("Contribute");
list.add("IDE");
list.add("Callable");
System.out.println(lists[0]);
System.out.println(lists[1]);
}
}
QUESTION :01
import java.util.*;
List<String> list2 = new
public class Main LinkedList<>();
{ list2.add("mac");
public static void
main(String[] args) list1.removeAll(list2); A. Mac
{
List<String> list1 = new for (String temp : B. breath
LinkedList<>(); list1)
list1.add("mac"); System.out.printf(te C. Lead
list1.add("breath"); mp + " ");
list1.add("lead"); D. Breath lead queen
list1.add("queen"); System.out.println();
}
}
Answer : D
QUESTION :02
import java.util.*;
System.out.println();
}
}
QUESTION :03
import java.util.ArrayList;
class Demo {
public void show()
{ A. True
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> list1 = new ArrayList<Integer>(); B. False
boolean check = (list.getClass() ==
list1.getClass()); C. Compiler error
System.out.println(check);
} D. Run time error
} public class Main {
public static void main(String[] args)
{
Demo demo = new Demo(); Answer : A
demo.show();
}
}
QUESTION :05
import java.util.*;
class Output
{ A. 3
public static void main(String args[])
{ B. 2
ArrayList obj = new ArrayList();
obj.add("A"); C. 1
obj.ensureCapacity(3);
System.out.println(obj.size()); D. 4
}
}
Answer : C
QUESTION :07
import java.util.*;
class Main
{
public static void main(String[] args)
{ A. A
List < String > one = new ArrayList < String > ();
one.add("abc"); B. C
List < String > two = new Vector < String > ();
two.add("abc"); C. B
if(one == two) {
System.out.println("A");
D. None of the above
} else if(one.equals(two)) {
System.out.println("B");
} else {
System.out.println("C"); Answer : B
}
}
}
QUESTION : 09
import java.util.ArrayList;
class Demo {
public void show()
{
ArrayList<String> list = new A. ArrayIndexOutOfBoundEx
ArrayList<String>();
System.out.print(list.get(0)); B. IndexOutOfBoundExcept
} C. null
} public class Main {
public static void main(String[] args)
{
Demo demo = new Demo(); Answer : B
demo.show();
}
}
QUESTION :11
import java.util.ArrayList;
A. German
class Demo {
public void show() B. Grey
{ C. Compilation error
ArrayList<String> list = new ArrayList<String>(); D. Run time error
list.add("German"); // line 6
list.add("Grey");
System.out.print(list.getFirst()); // line 8
}
}
public class Main { Answer : C
public static void main(String[] args)
{
Demo demo = new Demo();
demo.show();
}
}
QUESTION : 12
import java.util.LinkedList;
class Demo {
public void show()
{ A. null
LinkedList<String> list = new LinkedList<String>();
System.out.print(list.getFirst()); B. IndexOutOfBoundExcept
}
} public class Main {
C. NoSuchElementExceptio
public static void main(String[] args)
{
Demo demo = new Demo(); Answer : C
demo.show();
}
}
LINKED LIST
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and
each of their nodes contain a single digit. Add the two numbers and return it as a linked list.