-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1D Array
203 lines (173 loc) · 4.7 KB
/
1D Array
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Largest Number At Least Twice of Others
You are given an integer array nums where the largest integer is unique.
Determine whether the largest element in the array is at least twice as much as every other number in the array.
If it is, print the index of the largest element, or print -1 otherwise. */
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[] = new int[n];
int max = Integer.MIN_VALUE;
int maxindex =0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]>max){
max =a[i];
maxindex =i;
}
}
boolean flag = false;
for(int i=0;i<n;i++){
if( i!=maxindex && max < 2*a[i]){
flag = true;
}
}
if(flag){
System.out.println(-1);
}
else{
System.out.println(maxindex);
}
}
}
/* PEAK ELEMENT
An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists). Given an array arr[] of size n, find the index of first peak element. If peak element does not exist print -1.
Input
line 1: contains an integer n denoting size of array.
line 2: contains n spaced integers denoting elements of array.
Output
Print a single integer denoting the index of first peak element in array. If no such element exists, print -1. */
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
if(n==1){
System.out.println(0);
return;
}
if(a[0]>a[1] ){
System.out.println(0);
return;
}
for(int i=1;i<n-1;i++){
if(a[i]>a[i+1] &&a[i]>a[i-1]){
System.out.println(i);
return;
}
}
if(a[n-1]>a[n-2]){
System.out.println(n-1);
return;
}
System.out.println(-1);
}
}
/* SUM OF ARRAY EXCEPT SELF */
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[] = new int[n];
int sum =0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
sum += a[i];
}
for(int i=0;i<n;i++){
System.out.print(sum-a[i] +" ");
}
}
}
// largest and second largest number in an array
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[] = new int[n];
int max =Integer.MIN_VALUE;
int secondmax = Integer.MIN_VALUE;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]>max){
secondmax = max;
max = a[i];
}
else if(a[i]>secondmax){
secondmax =a[i];
}
}
System.out.println(max);
System.out.println(secondmax);
}
}
/*Given a sorted array A and a target value B, return the index if the target is found. If not, print the index where
it would be if it were inserted in order.
First line given is an integer N, denoting the size of array A. Second line contains N integers - the elements of array A.
Third line given is integer B.
example 4 [3 7 12 44] 11 output 2(index number) */
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int k = sc.nextInt();
for(int i=0;i<n;i++){
if(a[i]>=k){
System.out.println(i);
break;
}
}
}
}
/* Find First and Last Position of Element in Sorted Array
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
If target is not found in the array, print[-1, -1]. */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int k =sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] =sc.nextInt();
}
int ans1 = -1;
for(int i=0;i<n;i++){
if(a[i]==k){
ans1 =i;
break;
}
}
System.out.print(ans1 +" ");
int ans2 =-1;
for(int i=n-1;i>=0;i--){
if(a[i]==k){
ans2 = i;
break;
}
}
System.out.print(ans2);
}
}