Challenge

Project 1

Ans1=>

class EquilibriumIndex {
int equilibrium(int arr[], int n)
{
int i, j;
int leftsum, rightsum;

for (i = 0; i < n; ++i) {

leftsum = 0;
for (j = 0; j < i; j++)
leftsum += arr[j];

rightsum = 0;
for (j = i + 1; j < n; j++)
rightsum += arr[j];

if (leftsum == rightsum)
return i;
}

return -1;
}

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(equi.equilibrium(arr, arr_size));
}
}

Ans2=>

class LeadersInArray
{
void printLeaders(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
int j;
for (j = i + 1; j < size; j++)
{
if (arr[i] <=arr[j])
break;
}
if (j == size) 
System.out.print(arr[i] + " ");
}
}

public static void main(String[] args)
{
LeadersInArray lead = new LeadersInArray();
int arr[] = new int[]{16, 17, 4, 3, 5, 2};
int n = arr.length;
lead.printLeaders(arr, n);
}
}

Ans3=>

import java.io.*;

class rev {
public static String swap(String str, int i, int j)
{
char ch[] = str.toCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return new String(ch);
}

public static String reverse_words(String s)
{
int left = 0, i = 0, n = s.length();

while (s.charAt(i) == ' ')
i++;

left = i;

while (i < n) {
if (i + 1 == n || s.charAt(i) == ' ') {
int j = i - 1;
if (i + 1 == n)
j++;

while (left < j)
s = swap(s, left++, j--);

left = i + 1;
}
if (i > left && s.charAt(left) == ' ')
left = i;

i++;
}
s = new StringBuilder(s).reverse().toString();
return s;
}
public static void main(String[] args)
{
String str= "i like this program very much";
str = reverse_words(str);
System.out.println(str);
}
}

Ans4=>

import java.util.*;

class RemoveDuplicates
{

void removeDuplicates(String str)
{
LinkedHashSet lhs = new LinkedHashSet<>();
for(int i=0;i lhs.add(str.charAt(i));
for(Character ch : lhs)
System.out.print(ch);
}
public static void main(String args[])
{
String str = "skilllync";
RemoveDuplicates r = new RemoveDuplicates();
r.removeDuplicates(str);
}
}

Ans5=>

class midEle{

Node head;

class Node {
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}

public void pushNode(int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}

public void printNode()
{
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.print("Null"+"\n");
}

public int getLen()
{
int length = 0;
Node temp = head;
while (temp != null) {
length++;
temp = temp.next;
}
return length;
}

public void printMiddle()
{
if (head != null) {
int length = getLen();
Node temp = head;
int middleLength = length / 2;
while (middleLength != 0) {
temp = temp.next;
middleLength--;
}
System.out.print("The middle element is ["+ temp.data + "]");
System.out.println();
}
}

public static void main(String[] args)
{
midEle list = new midEle();
for (int i = 5; i >= 1; i--) {
list.pushNode(i);
list.printNode();
list.printMiddle();
}
}
}

Ans6=>

class LinkedList {

static Node head;

static class Node {

int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}

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;
}

void printList(Node node)
{
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}

public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(5);
list.head.next = new Node(1);
list.head.next.next = new Node(47);
list.head.next.next.next = new Node(69);

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);
}
}

Ans7=>

class LinkedList {
Node head; 
class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

void pairWiseSwap()
{
Node temp = head;

while (temp != null && temp.next != null) {

int k = temp.data;
temp.data = temp.next.data;
temp.next.data = k;
temp = temp.next.next;
}
}
public void push(int new_data)
{
Node new_node = new Node(new_data);

new_node.next = head;

head = new_node;
}

void printList()
{
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}

public static void main(String args[])
{
LinkedList llist = new LinkedList();

llist.push(55);
llist.push(44);
llist.push(39);
llist.push(12);
llist.push(15);

System.out.println("Linked List before calling pairWiseSwap() ");
llist.printList();

llist.pairWiseSwap();

System.out.println("Linked List after calling pairWiseSwap() ");
llist.printList();
}
}

Ans8=>

Ans9=>
class Node
{
int data;
Node left, right;

public Node(int data)
{
this.data = data;
left = right = null;
}
}

class BinaryTree
{
Node root;
Node head;
static Node prev = null;

void BinaryTree2DoubleLinkedList(Node root)
{
if (root == null)
return;

BinaryTree2DoubleLinkedList(root.left);

if (prev == null)
head = root;
else
{
root.left = prev;
prev.right = root;
}
prev = root;

BinaryTree2DoubleLinkedList(root.right);
}

void printList(Node node)
{
while (node != null)
{
System.out.print(node.data + " ");
node = node.right;
}
}

public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(10);
tree.root.left = new Node(12);
tree.root.right = new Node(15);
tree.root.left.left = new Node(25);
tree.root.left.right = new Node(30);
tree.root.right.left = new Node(36);

tree.BinaryTree2DoubleLinkedList(tree.root);

tree.printList(tree.head);

}
}

Ans10=>

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

class Graph {

class Edge {
int src, dest, weight;
Edge() { src = dest = weight = 0; }
};

int V, E;
Edge edge[];

Graph(int v, int e)
{
V = v;
E = e;
edge = new Edge[e];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}

void BellmanFord(Graph graph, int src)
{
int V = graph.V, E = graph.E;
int dist[] = new int[V];

for (int i = 0; i < V; ++i)
dist[i] = Integer.MAX_VALUE;
dist[src] = 0;

for (int i = 1; i < V; ++i) {
for (int j = 0; j < E; ++j) {
int u = graph.edge[j].src;
int v = graph.edge[j].dest;
int weight = graph.edge[j].weight;
if (dist[u] != Integer.MAX_VALUE
&& dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}

for (int j = 0; j < E; ++j) {
int u = graph.edge[j].src;
int v = graph.edge[j].dest;
int weight = graph.edge[j].weight;
if (dist[u] != Integer.MAX_VALUE
&& dist[u] + weight < dist[v]) {
System.out.println("Graph contains negative weight cycle");
return;
}
}
printArr(dist, V);
}

void printArr(int dist[], int V)
{
System.out.println("Vertex Distance from Source");
for (int i = 0; i < V; ++i)
System.out.println(i + "\t\t" + dist[i]);
}

public static void main(String[] args)
{
int V = 5;
int E = 8;

Graph graph = new Graph(V, E);

graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = -1;


graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 4;


graph.edge[2].src = 1;
graph.edge[2].dest = 2;
graph.edge[2].weight = 3;


graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 2;


graph.edge[4].src = 1;
graph.edge[4].dest = 4;
graph.edge[4].weight = 2;


graph.edge[5].src = 3;
graph.edge[5].dest = 2;
graph.edge[5].weight = 5;


graph.edge[6].src = 3;
graph.edge[6].dest = 1;
graph.edge[6].weight = 1;


graph.edge[7].src = 4;
graph.edge[7].dest = 3;
graph.edge[7].weight = -3;


graph.BellmanFord(graph, 0);
}
}

Ans11=>

import javafx.util.Pair;
import java.util.ArrayList;
import java.util.*;

class minSp
{
public static int minSwaps(int[] arr)
{
int n = arr.length; 
ArrayList > arrpos =
new ArrayList Integer> > ();
for (int i = 0; i < n; i++)
arrpos.add(new Pair Integer> (arr[i], i));

arrpos.sort(new Comparator Integer>>()
{
@Override
public int compare(Pair o1,
Pair o2)
{
if (o1.getKey() > o2.getKey())
return -1;

else if (o1.getKey().equals(o2.getKey()))
return 0;

else
return 1;
}
});

Boolean[] vis = new Boolean[n];
Arrays.fill(vis, false);

int ans = 0;

for (int i = 0; i < n; i++)
{
if (vis[i] || arrpos.get(i).getValue() == i)
int cycle_size = 0;
int j = i;
while (!vis[j])
{
vis[j] = true;

j = arrpos.get(j).getValue();
cycle_size++;
}

if(cycle_size > 0)
{
ans += (cycle_size - 1);
}
}

return ans;
}
}

class MinSwaps
{
public static void main(String[] args)
{
int []a = {1, 5, 4, 3, 2};
minSp ms = new minSp();
System.out.println(ms.minSwaps(a));
}
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.