Finding the power of a huge number in Java.

This solution is based on Factorial of a large number – GeeksforGeeks

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
*Ketan Ramteke
*/
import java.util.*;
import java.io.*; 
 
public class Solution{
 
static void sqr(int n,int pow)
{
    int[] result = new int[10000];
 
    result[0] = 1;
    int result_size = 1;
 
    
    for (int x=0; x<pow; x++){
        result_size = multiply(n, result, result_size);
 }
    
    for (int i=result_size-1; i>=0; i--){
        System.out.print(result[i]);
	}
}
 
 
static int multiply(int x, int result[], int result_size)
{
    int carry = 0;
 
        for (int i=0; i<result_size; i++)
    {
        int prod = result[i] * x + carry;
        result[i] = prod % 10;
        carry  = prod/10;
    }
 
 
    while (carry>0)
    {
        result[result_size] = carry%10;
        carry = carry/10;
        result_size++;
    }
    return result_size;
}
 
 
public static void main(String[] args)
{
    sqr(739364,100);
 
    }
     
}

With BigInteger

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 
*Ketan Ramteke
*/

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

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		
		BigInteger b = new BigInteger("73936466");
		int power = 100;
		BigInteger b3 = b;
		for(int i = 1; i<power;i++){
			b3 = b3.multiply(b); 
		}
		System.out.println(b3.toString());
	}
}

Maximum Vs Minimum [Techgig]

Maximum Vs Minimum
This challenge will help you in getting familiarity with arrays which will be helpful when you will solve further problems on Techgig.
Task:
For this challenge, you need to take number of elements as input on one line and array elements as an input on another line. You need to find the minimum number and
maximum number from the array and multiply them.
Input Format:
In this challenge, you will take number of elements as input on one line and array elements which are space separated as input on another line.
Output Format:
You will print the value after multiplication to the stdout.
Sample Test Case:

Sample Input:
5
23 99 24 13 77
Sample Output:
6799

Explanation:
Of all the given elements which are in the array, find the minimum number and the maximum number and multiply them

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* 
 * Ketan Ramteke
*/
import java.util.*;
import java.io.*;
public class CandidateCode {
    public static void main(String args[] ) throws Exception {
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] arr = new int[n];
    int min = Integer.MAX_VALUE,max = 0;
    for(int i = 0; i<n; i++){
        arr[i] = sc.nextInt();
        if(arr[i]<min){
            min = arr[i];
        }
        if(arr[i]>max){
            max = arr[i];
        }
        
    }
    int mult = min*max;
    System.out.println(mult);


   }
}

War between odd and even [Techgig]

War between odd and even
This challenge will help you in getting familiarity with arrays which will be helpful when you will solve further problems on Techgig.
Task:
For this challenge, you need to take a number of elements as input on one line and array elements as an input on another line. You need to find the numbers that are
present at odd index, add them. find the numbers that are present at even index, add them and then subtract the smallest of the two values from the larger one.
Note:
Array indexes always start from 0.
Input Format:
In this challenge, you will take a number of elements as input on one line and array elements which are space separated as input on another line.
Output Format:
You will print the value after subtraction to the stdout.
Sample Test Case:

Sample Input:
5
23 22 24 13 55
Sample Output:
67

Explanation:
Of all the given elements which are in the array, identify numbers that are present at odd index and add them. Identify even index numbers and add them. Subtract the
smaller vale from the larger one.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* 
 *Ketan Ramteke
*/
import java.util.*;
import java.io.*;
public class CandidateCode {
    public static void main(String args[] ) throws Exception {

    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int odd=0,even=0,sub=0;
    	int[] arr = new int[n];
    	for(int i = 0; i<n;i++){
    	    arr[i] = sc.nextInt();
    	    if(i%2==0){
    	        even+=arr[i];
    	    }
    	    else{
    	        odd+=arr[i];
    	    }
    	}
    	sub = Math.abs(even-odd);
    	System.out.println(sub);
    	

   }
}

Multiplication between odd and even [Techgig]

Multiplication between odd and even
This challenge will help you in getting familiarity with arrays which will be helpful when you will solve further problems on Techgig.
Task:
For this challenge, you need to take a number of elements as input on one line and array elements as an input on another line. You need to find the numbers that are odd,
add them. find the numbers that are even, add them and then multiply the two values that you get after addition of even numbers and that of the addition of odd numbers.
Input Format:
In this challenge, you will take a number of elements as input on one line and array elements which are space separated as input on another line.
Output Format:
You will print the value after multiplication to the stdout.

Sample Test Case:
Sample Input:
5
23 11 24 13 55
Sample Output:
2448

Explanation:
Of all the given elements which are in the array, identify numbers that are odd and add them. Identify even numbers and add them. Multiply two values that you get after
addition of odd numbers and even numbers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* 
 *Ketan Ramteke
*/
import java.util.*;

import java.io.*;
public class CandidateCode {
    public static void main(String args[] ) throws Exception {

    	//Write code here
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int[] arr = new int[n];
    	int odd = 0,even = 0;
    	for(int i = 0; i<n; i++){
    	    arr[i] = sc.nextInt();
    	    if(arr[i]%2==0){
    	        even +=arr[i];
    	    }
    	    else{
    	        odd +=arr[i];
    	    }
    	}
    	int sol = odd*even;
    	System.out.println(sol);

   }
}

Minimum effort – Maximum output [Techgig]

Minimum effort – Maximum output

This challenge will help you in getting familiarity with arrays which will be helpful when you will solve further problems on Techgig. Task: For this challenge, Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted. Input Format: On the first line, you need to take an integer input which will be the length of the array. Another line will have space separated integer values. Output Format: space separated integer values present in the subarray. Sample Test Case:

Sample Input: 

11 10 12 20 30 25 40 32 31 35 50 60 

Sample Output: 

30 25 40 32 31 35

Explanation: You are given an array and you need to find a subarray that if you sorted the whole array got sorted

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 
 *Ketan Ramteke
*/
import java.util.*;

import java.io.*;
public class CandidateCode {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int start = 0,end=n-1;
        String str = "";
        int[] arr = new int[n];
        for(int i = 0; i<n;i++){
            arr[i] = sc.nextInt();
        }
        int[] sorted = arr.clone();
        Arrays.sort(sorted);
        while(arr[start] == sorted[start]){
            start++;
        }
        while(arr[end] == sorted[end]){
            end--;
        }
        for(int i = start; i<=end; i++){
            str +=" "+arr[i];
        }
        System.out.println(str.trim());
   }
}

Smallest Common Multiple [Freecodercamp]

Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

e.g. for 1 and 3 – find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/*By Ketan Ramteke */
function smallestCommons(arr) {
	var min = arr[0]<arr[1]?arr[0]:arr[1];
	var max = arr[0]>arr[1]?arr[0]:arr[1];
	var m = max;
	var i = 0;
	while(i < max){
		m +=max; 
		for(i = min;;i++){
			if(m%i !== 0) break;
		}
	}
	return m;
}

smallestCommons([23, 18]);

Sum All Primes [Freecodercamp]

Sum all the prime numbers up to and including the provided number.

A prime number is defined as a number greater than one and having only two divisors, one and itself. For example, 2 is a prime number because it’s only divisible by one and two.

The provided number may not be a prime.

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function sumPrimes(num) {
  
var sum = 0;
var prime = [2,3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
 
 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 
 
151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
 
 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 
 
313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401,
 
 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 
 
491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 
 
599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677,
 
 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 
 
787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881,
 
 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];
 //var temp = 0;
 for(var i= 0; num>=prime[i];i++){
   sum +=prime[i];
 }
  
  return sum;
}

sumPrimes(977);

Sum All Odd Fibonacci Numbers [Freecodercamp]

Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.

The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.

For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than 10 are 1, 1, 3, and 5.

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function sumFibs(num) {
  var num1 = 0;
  var num2 = 1;
  var fib = 0;
  var sol = 0;
  if(num === 1) {return 1;}
  
  for(var i = 2;fib<=num;i++){
      fib = num1+num2;
      if(fib % 2 !==0 && fib<=num){
        sol +=fib;
      }
      
      num1 = num2;
      num2 = fib;
  }
  return sol+1;
}

sumFibs(10);