Created
February 1, 2021 05:58
-
-
Save kshitijvarshne1/3cc903074b57693c7ed5820241129eb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Created by IntelliJ IDEA. | |
* Author: Kshitij Varshney (kshitijvarshne1) | |
* Date: 01-Feb-21 | |
* Time: 10:47 AM | |
* File: Two.java | |
*/ | |
package feb01_21; | |
import java.util.Arrays; | |
public class Two { | |
public static void main(String[] args) { | |
int index = 5; | |
int value = 55; | |
int[] a = {1, 2, 3, 4, 5}; | |
int[] temp = insert(a, index, value); | |
System.out.println(Arrays.toString(temp)); | |
} | |
public static int[] insert(int[] a, int index, int value) { | |
int temp[] = new int[a.length + 1]; | |
if (a.length <= index) { | |
for (int i = 0; i < a.length; i++) { | |
temp[i] = a[i]; | |
} | |
temp[a.length] = value; | |
return temp; | |
} else { | |
int i = 0; | |
int k = 0; | |
while (i <= a.length) { | |
if (i == index) { | |
temp[i] = value; | |
k -= 1; | |
} else { | |
temp[i] = a[k]; | |
} | |
i += 1; | |
k += 1; | |
} | |
} | |
return temp; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment