Project 1
1. Answer:
// Java program to find equilibrium
// index of an array
class EquilibriumIndex {
int equilibrium(int arr[], int n)
{
int sum = 0; // initialize sum of whole array
int leftsum = 0; // initialize leftsum
/* Find sum of the whole array */
for (int i = 0; i < n; ++i)
sum += arr[i];
for (int i = 0; i < n; ++i) {
sum -= arr[i]; // sum is now right sum for index i
if (leftsum == sum)
return i;
leftsum += arr[i];
}
/* If no equilibrium index found, then return 0 */
return -1;
}
// Driver code
public static void main(String[] args)
{
EquilibriumIndex equi = new EquilibriumIndex();
int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
int arr_size = arr.length;
System.out.println("First equilibrium index is " +
equi.equilibrium(arr, arr_size));
}
}
output:
First equilibrium index is 3
2.
Input:
n = 6
A[] = {16,17,4,3,5,2}
Output: 17 5 2
Explanation: The first leader is 17
as it is greater than all the elements
to its right. Similarly, the next
leader is 5. The right most element
is always a leader so it is also
included.
Example 2:
Input:
n = 5
A[] = {1,2,3,4,0}
Output: 4 0
code:
class Leaders
{
// function for finding leaders
public static void leaderprint(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (arr[i] < arr[j])
{
break;
}
if (j == n - 1)
System.out.println(arr[i]+" is a leader");
}
}
}
public static void main (String[] args)
{
int arr[] = { 7, 6, 4, 5, 0, 1 };
int n = arr.length;
// calling function
leaderprint(arr, n);}}
3. Java program to remove duplicate character
import java.util.*;
class GFG
{
static String removeDuplicate(char str[], int n)
{
// Used as index in the modified string
int index = 0;
// Traverse through all characters
for (int i = 0; i < n; i++)
{
// Check if str[i] is present before it
int j;
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break;
}
}
// If not present, then add it to
// result.
if (j == i)
{
str[index++] = str[i];
}
}
return String.valueOf(Arrays.copyOf(str, index));
}
// Driver code
public static void main(String[] args)
{
char str[] = "skilly lync cours ".toCharArray();
int n = str.length;
System.out.println(removeDuplicate(str, n));
}
}
output: skil ync ours
4. reverse a String
import java.util.*;
class GFG{
// Reverse the letters
// of the word
static void reverse(char str[],
int start,
int end)
{
// Temporary variable
// to store character
char temp;
while (start <= end)
{
// Swapping the first
// and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
// Function to reverse words
static char[] reverseWords(char []s)
{
// Reversing individual words as
// explained in the first step
int start = 0;
for (int end = 0; end < s.length; end++)
{
// If we see a space, we
// reverse the previous
// word (word between
// the indexes start and end-1
// i.e., s[start..end-1]
if (s[end] == ' ')
{
reverse(s, start, end);
start = end + 1;
}
}
// Reverse the last word
reverse(s, start, s.length - 1);
// Reverse the entire String
reverse(s, 0, s.length - 1);
return s;
}
// Driver Code
public static void main(String[] args)
{
String s = "i like this program very much ";
char []p = reverseWords(s.toCharArray());
System.out.print(p);
}
}
output:
much very program this like i
5.
// Java program to find middle of linked list
class LinkedList
{
Node head; // head of linked list
/* Linked list node */
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Function to print middle of linked list */
void printMiddle()
{
Node slow_ptr = head;
Node fast_ptr = head;
while (fast_ptr != null && fast_ptr.next != null)
{
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
System.out.println("The middle element is [" +
slow_ptr.data + "] \n");
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* This function prints contents of linked list
starting from the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+"->");
tnode = tnode.next;
}
System.out.println("NULL");
}
public static void main(String [] args)
{
LinkedList llist = new LinkedList();
for (int i=5; i>0; --i)
{
llist.push(i);
llist.printList();
llist.printMiddle();
}
}
}
output:
5->NULL The middle element is [5] 4->5->NULL The middle element is [5] 3->4->5->NULL The middle element is [4] 2->3->4->5->NULL The middle element is [4] 1->2->3->4->5->NULL The middle element is [3]
7. Java program for reversing the linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Function to reverse the linked list */
Node reverse(Node node)
{
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
// prints content of double linked list
void printList(Node node)
{
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
// Driver Code
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(20);
list.head.next = new Node(40);
list.head.next.next = new Node(60);
list.head.next.next.next = new Node(50);
list.head.next.next.next.next = new Node(70);
list.head.next.next.next.next.next = new Node(10);
System.out.println("Given Linked list");
list.printList(head);
head = list.reverse(head);
System.out.println("");
System.out.println("Reversed linked list ");
list.printList(head);
}
}
output: 10 70 50 60 40 20