Challenge

Project 1

  • class equilibriumpoint {
        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){
            equilibriumpoint equi = new equilibriumpoint();
            int arr[] = { 1 , 3 , 5 , 2 , 2};
            int arr_size = arr.length;
            System.out.println(equi.equilibrium(arr, arr_size));
        }
    }
  • public 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);
          }
        }
 
  • class printstringinreverse {
        static void reverse(char str[], int start, int end){
        char temp;
        while (start <= end) {
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
          }
        }
        static char[] reverseWords(char[] s){
        int start = 0;
        for (int end = 0; end < s.length; end++) {
        if (s[end] == ' ') {
        reverse(s, start, end);
        start = end + 1;
            }
        }
        reverse(s, start, s.length - 1);
        reverse(s, 0, s.length - 1);
        return s;
        }
        public static void main(String[] args){
        String s = "pqr.mno ";
        char[] p = reverseWords(s.toCharArray());
        System.out.print(p);
          }
        }
  • import java.util.*;
        class removeduplicate{
        static String removeDuplicate(char str[], int n){
        int index = 0;
        for (int i = 0; i < n; i++){
        for (int j = 0; j < i; j++){
        if (str[i] == str[j]){
        break;
          }
        }
        if (j == i){
        str[index++] = str[i];
          }
        }
        return String.valueOf(Arrays.copyOf(str, index));
        }
        public static void main(String[] args){
        char str[] = "skilllync".toCharArray();
        int n = str.length;
        System.out.println(removeDuplicate(str, n));
         }
        }
  • import java.util.ArrayList;
        class enode {
        public int data;
        public enode next;
        }
        class nodeoperation {
        public void pushNode(enode[] headRef, int dataVal) {
        enode newNode = new enode();
        newNode.data = dataVal;
        newNode.next = headRef[0];
        headRef[0] = newNode;
          }
        }
        public class middlelementinll {
        public static void main(String[] args) {
        enode[] head = new enode[1];
        nodeoperation temp = new nodeoperation();
        for (int i = 5; i > 0; i--) {
        temp.pushNode(head, i);
        }
        ArrayList v = new ArrayList();
        enode curr = head[0];
        while (curr != null) {
        v.add(curr.data);
        curr = curr.next;
        }
        System.out.print("Middle Element Of Linked List is : ");
        System.out.println(v.get(v.size() / 2));
          }
        }
 
  • class reversell {
        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){
        reversell list = new reversell();
        head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        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);
          }
        }
  • class pairwiseswap {
        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[]){
        pairwiseswap llist = new pairwiseswap();
        llist.push(5);
        llist.push(4);
        llist.push(3);
        llist.push(2);
        llist.push(1);
        System.out.println("Linked List before calling pairWiseSwap() ");
        llist.printList();
        llist.pairWiseSwap();
        System.out.println("Linked List after calling pairWiseSwap() ");
        llist.printList();
          }
        }
 
  • public class addtonoinll{
        class node{
        int val;
        node next;
        public node(int val){
        this.val = val;
          }
        }
        void printlist(node head){
        while (head != null){
        System.out.print(head.val + " ");
        head = head.next;
          }
        }
        node head1, head2, result;
        int carry;
        void push(int val, int list){
        node newnode = new node(val);
        if (list == 1){
        newnode.next = head1;
        head1 = newnode;
        }
        else if (list == 2){
        newnode.next = head2;
        head2 = newnode;
        }
        else{
        newnode.next = result;
        result = newnode;
          }
        }
        void addsamesize(node n, node m){
        if (n == null)
        return;
        addsamesize(n.next, m.next);
        int sum = n.val + m.val + carry;
        carry = sum / 10;
        sum = sum % 10;
        push(sum, 3);
        }
        node cur;
        void propogatecarry(node head1){
        if (head1 != cur){
        propogatecarry(head1.next);
        int sum = carry + head1.val;
        carry = sum / 10;
        sum %= 10;
        push(sum, 3);
         }
        }
        int getsize(node head){
        int count = 0;
        while (head != null){
        count++;
        head = head.next;
        }
        return count;
        }
        void addlists(){
        if (head1 == null){
        result = head2;
        return;
        }
        if (head2 == null){
        result = head1;
        return;
        }
        int size1 = getsize(head1);
        int size2 = getsize(head2);
        if (size1 == size2){
        addsamesize(head1, head2);
        }
        else{
        if (size1 < size2){
        node temp = head1;
        head1 = head2;
        head2 = temp;
        }
        int diff = Math.abs(size1 - size2);
        node temp = head1;
        while (diff-- >= 0){
        cur = temp;
        temp = temp.next;
        }
        addsamesize(cur, head2);
        propogatecarry(head1);
        }
        if (carry > 0)
        push(carry, 3);
        }
        public static void main(String args[]){
        addtonoinll list = new addtonoinll();
        list.head1 = null;
        list.head2 = null;
        list.result = null;
        list.carry = 0;
        int arr1[] = { 4,5 };
        int arr2[] = { 3,4,5 };
        for (int i = arr1.length - 1; i >= 0; --i)
            list.push(arr1[i], 1);
        for (int i = arr2.length - 1; i >= 0; --i)
            list.push(arr2[i], 2);
        list.addlists();
        list.printlist(list.result);
          }
        }


  • class dnode{
        int data;
        dnode left, right;
        dnode(int item){
        data = item;
        left = right = null;
         }
       }
       class convertbttodll{
       dnode root;
       dnode bintree2listUtil(dnode node){
       if (node == null)
       return node;
       if (node.left != null){
       dnode left = bintree2listUtil(node.left);
       for (; left.right != null; left = left.right);
       left.right = node;
       node.left = left;
       }
       if (node.right != null){
       dnode right = bintree2listUtil(node.right);
       for (;right.left != null; right = right.left);
       right.left = node;
       node.right = right;
        }
        return node;
        }
        dnode bintree2list(dnode node){
        if (node == null)
        return node;
        node = bintree2listUtil(node);
        while (node.left != null)
        node = node.left;
        return node;
        }
        void printList(dnode node){
        while (node != null){
        System.out.print(node.data + " ");
        node = node.right;
          }
        }
        public static void main(String[] args){
        convertbttodll tree = new convertbttodll();
        tree.root = new dnode(10);
        tree.root.left = new dnode(12);
        tree.root.right = new dnode(15);
        tree.root.left.left = new dnode(25);
        tree.root.left.right = new dnode(30);
        tree.root.right.left = new dnode(36);
        dnode head = tree.bintree2list(tree.root);
        tree.printList(head);
          }
        }
       
   
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.