Sie sind auf Seite 1von 10

A Onely sequence is a sequence consisting of 1 only.

For example : {1,1,1,1} is a onely sequence while {1,2,1} is not.

You are given with an array of 1s and 0s. And you are given with an integer M, which
signifies number of flips allowed.

Each flip can be used as follows :


- If an element is 1, it can be changed to 0.
- If an element in 0, it can be changed to 1.

You are to apply at most M flip operations to product the longest continuous sequence of 1s
in the array.

For this problem, return the indices of maximum continuous series of 1s in order.

Input Format

First line contains N and M, the size of array and number of flips allowed. Second line
contains N integers, the elements of array a.

Constraints

1 <= N,M <= 10^5

Output Format

Output two numbers, the starting and the ending index (1-indexed) of the longest Onely
sequence.

In case of multiple answers print the one with lower start index.

Sample Input 0

10 1
1 1 0 1 1 0 0 1 1 1

Sample Output 0

1 5
Given an array, Mister Arr the owner of the array wants to move each element k places to its
left. I.E. the array a[0],a[1],a[2],...a[n] becomes a[1],a[2]...a[n],a[0] after moving one place to
the left. Note that the first element becomes the last element after each rotation. Mister Arr
was up all night watching a football match and hence is sleeping now. He has given you the
task to rotate the entire array K places to its left.

Input Format

The first line contains two space-separated integers denoting the respective values of n (the
number of integers) and k (the number of left rotations you must perform). The second line
contains n space-separated integers describing the respective elements of the array's initial
state.

Constraints

1 <= n <= 10^5


1 <= k <= n
1 <= a[i] <= 10^6

Output Format

Print a single line of n space-separated integers denoting the final state of the array after
performing d left rotations.

Sample Input 0

5 4
1 2 3 4 5

Sample Output 0

5 1 2 3 4
Write a program to print 1-n^2 in a spiral in a spiral order in a square matrix.

Input Format

First line has an integer N.

Constraints

1 <= N <= 100

Output Format

Print the square matrix.

Sample Input 0

Sample Output 0

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

Sample Input 1

Sample Output 1

1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Circular_Matrix
import java.util.*;
class Circular_Matrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int n = sc.nextInt();

int A[][] = new int[n][n];


int k=1, c1=0, c2=n-1, r1=0, r2=n-1;

while(k<=n*n)
{
for(int i=c1;i<=c2;i++)
{
A[r1][i]=k++;
}

for(int j=r1+1;j<=r2;j++)
{
A[j][c2]=k++;
}

for(int i=c2-1;i>=c1;i--)
{
A[r2][i]=k++;
}

for(int j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k++;
}

c1++;
c2--;
r1++;
r2--;
}

/* Printing the Circular matrix */


System.out.println("The Circular Matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}
}

Football

import java.util.Scanner;

public class FootballMatch


{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Integer N, K, i = 0;
N = s.nextInt();
if(N >= 1 && N <= 100000)
{
Integer[] arr = new Integer[N];
Integer[] finalArr = new Integer[N];
K = s.nextInt();
if(K <= N)
{
for(int temp = 0; temp < N; temp++)
{
arr[temp] = s.nextInt();
if(temp > K-1)
{
finalArr[i] = arr[temp];
i++;
}
}
for(int temp = 0; temp < K; temp++)
{
finalArr[i] = arr[temp];
i++;
}
for(int temp = 0; temp < N; temp++)
{
System.out.print(finalArr[temp]+ " ");
}
}
}
}
}
Flip sum

import java.util.Scanner;

public class Flip

public static void main(String[] args){

Scanner s = new Scanner(System.in);

Integer N,M, i=0, j =0;

N = s.nextInt();

if(N >= 1)

Integer[] arr = new Integer[N];

Integer[] finalArr = new Integer[N];

M = s.nextInt();

if(M < N && M <= 100000)

for(int temp = 0; temp< N;temp++)

arr[temp] = s.nextInt();

if(arr[temp] == 0)

i++;

else

j++;
}

if(i > j)

finalArr = replaceOne(arr,M);

largestOccurence(finalArr, 0);

else

finalArr = replaceZero(arr,M);

largestOccurence(finalArr, 1);

public static Integer[] replaceZero(Integer[] a,Integer k)

int len = 0, len1 = 0;

Integer[] temp = a;

Integer[] temp1 = a;

for(int fast = 0, slow = 0; fast < a.length; ++fast)

temp = a;

if(a[fast] == 0)

--k;

for(slow = 0; k < 1; ++slow)

if(a[slow] == 0)
{

temp[slow] = 1;

++k;

len1 = len1 > (fast - slow + 1)? len1 :(fast - slow + 1);

if(len1 > len)

temp1 = temp;

len = len1;

return temp1;

public static Integer[] replaceOne(Integer[] a,Integer k)

int len = 0, len1 = 0;

Integer[] temp = a;

Integer[] temp1 = a;

for(int fast = 0, slow = 0; fast < a.length; ++fast)

if(a[fast] == 1)

--k;

for(; k < 1; ++slow)

if(a[slow] == 1)

temp[slow] = 0;

++k;

}
}

len1 = len1 > (fast - slow + 1)? len1 :(fast - slow + 1);

if(len1 > len)

temp1 = temp;

temp = a;

len = len1;

return temp1;

public static void largestOccurence(Integer[] a, int k)

int start = 0, len = 0, len1 = 0, startindex = 0, startindexF = 0, endindex = 0;

while(start < a.length)

if(len == 0)

startindex = start;

if(a[start] == k)

len++;

start++;

else

if(len > len1)

len1 = len;

endindex = start - 1;
startindexF = startindex;

start = start+1;

len = 0;

System.out.print(startindexF + " "+ endindex);

Das könnte Ihnen auch gefallen